ServletConfig in Java
Using Servlet Config in Java
ServletsHome
ServletConfig in Java:
Sets initialization parameters for specific servlet in web.xml
ServletConfigDemo.java
package codingzon;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletConfigDemo extends HttpServlet {
public void init(ServletConfig config)
throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//ServletConfig config=;
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
try {
pw.println(getInitParameter("Address"));
} finally {
pw.close();
}
}
}
In web.xml, along with servlet, add the parameter details as shown in below given XML code.
<servlet>
<servlet-name>ServletConfigDemo</servlet-name>
<servlet-class>ServletConfigDemo</servlet-class>
<init-param>
<param-name>Address</param-name>
<param-value>Delhi</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletConfigDemo</servlet-name>
<url-pattern>/ServletConfigDemo</url-pattern>
</servlet-mapping>
...
</web-app>
Output:
Comments
Post a Comment