Inside servlet container, the object of javax.servlet.ServletConfig is used to represents the servlet configurations. The configuration information conatains the servlet ‘s initialization parameters, the name of the servlet and servlet contaxt which gives the information about container. The servletinitialization parameter and the servlet name can be spacified in the deployment descriptor (web.xml).
For example
<web-app><servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
<init-param>
<param-name>p1</param-name>
<param-value>bye user</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/s1</url-pattern>
</servlet-mapping>
</web-app>
Following example will retrieves parameter from init-param tag of the servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class FirstServlet extends HttpServlet{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
ServletConfig config=getServletConfig();
String s=config.getInitParameter("p1");
pw.write(s);
}
}
Comments