In this tutorial you will learn about the Servlet ServletConfig and its application with practical example.
Servlet ServletConfig Interface
During initialization a servlet container uses a servlet configuration object so that it can pass information. In other word this object can be used to get configuration information from web.xml file. Now suppose we have change the information in web.xml file so we don’t need to change the servlet.
Get Object of ServletConfig
The getServletConfig() method is used to returns the object of ServletConfig.
ServletConfig Interface Methods
public string getInitParameter(String name):- This method is used to get the value of initialization parameter with the given name.
public Enumeration getInitParameterNames():- This method is used to returns the names of servlet’s initialization parameter as an Enumeration of String object.
public String getServletName(): This method returns the name of Servlet.
public ServletContext getServletContext(): This method is used to returns a reference to the ServletContext in which the caller is executing.
Initializing parameter in web.xml file
Syntax:-
1 2 3 4 5 6 7 8 |
<web-app> <servlet> <init-param> <param-name>param_name</param-name> <param-value>param_value</param-value> </init-param> </servlet> </web-app> |
Example:-
Let’s understand this by an example. Create Servlet file as follow.
ServletConfiDemo.java :-
In this file we have created object of ServletConfig interface and with the help of this object we got the init parameter. Check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.io.IOException; import java.io.PrintWriter; 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 ServletConfiDemo extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); ServletConfig config=getServletConfig(); String strWebsite=config.getInitParameter("website"); out.print("Website is: "+strWebsite); out.close(); } } |
Web.xml :-
In the web.xml file we have set our init parameter . Follow below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <servlet> <servlet-name>ServletConfiDemo</servlet-name> <servlet-class>ServletConfiDemo</servlet-class> <init-param> <param-name>website</param-name> <param-value>www.w3adda.com</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletConfiDemo</servlet-name> <url-pattern>/ServletConfiDemo</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app> |
Output :-
Let’s see the output of this simple task. Check below image.