A ServletConfig is generated by the Web Container for each and every servlet. This Object can be made use to get Configuration data from web.xml file.
If the configuration information is changed from the web.xml file, we don’t require to change the servlet. So, it is effortless to manage the web application if any defined content is amended from time to time.
The most important advantage of servletconfig is that there is no need to edit the file if information is changed from the web.xml file.
public ServletConfig getServletConfig();
ServletConfig config=getServletConfig(); //Now we can call the methods of ServletConfig interface
The init param sub-element of the servlet is used to define the initialization parameter of the servlet.
<web-app> <servlet> ...... <init-param> <param-name>parametername</param-name> <param-value>parametervalue</param-value> </init-param> ...... </servlet> </web-app>
We are getting the one initialization parameter from the web.xml file and printing this information in the servlet. In the below given example let’s see how.
DemoServlet.java
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class DemoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); ServletConfig config=getServletConfig(); String driver=config.getInitParameter("driver"); out.print("Driver is: "+driver); out.close(); } }
Web.xml
<web-app> <servlet> <servlet-name>DemoServlet</servlet-name> <servlet-class>DemoServlet</servlet-class> <init-param> <param-name>driver</param-name> <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DemoServlet</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> </web-app>
Example of servletconfig to get all the initialization parameters:
We are Getting all the initialization parameters from the web.xml file and printing this information in Servlet, In the below given example.
DemoServlet.java
import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class DemoServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); ServletConfig config=getServletConfig(); Enumeration<String> e=config.getInitParameterNames(); String str=""; while(e.hasMoreElements()){ str=e.nextElement(); out.print("<br>Name: "+str); out.print(" value: "+config.getInitParameter(str)); } out.close(); } }
Web.xml
<web-app> <servlet> <servlet-name>DemoServlet</servlet-name> <servlet-class>DemoServlet</servlet-class> <init-param> <param-name>username</param-name> <param-value>system</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>oracle</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DemoServlet</servlet-name> <url-pattern>/servlet1</url-pattern> </servlet-mapping> </web-app>