Updated June 7, 2023
Introduction to ServletConfig
The web container for each servlet creates an object of ServletConfig. The web.xml file’s configuration information can be obtained using this object. We don’t need to replace the servlet if the configuration data is changed from the web.xml file. Therefore, managing the web application will be simpler if certain content is changed occasionally. When a servlet is first loaded, the servlet container creates an object called ServletConfig and passes it to the servlet with some initial parameters or information. Since it is specific to one servlet, it is recommended to keep servlet-specific data in web.xml and access it from there.
Key Takeaways
- Pass default information in the initial stage of the servlet life cycle.
- Use the web.xml file to get the parameter value on the servlet page.
- Provide configuration and initialization quickly.
- Communicate with servlet container from static information.
- Display data using the servlet init parameter in the XML file.
- Uses multiple methods for displaying and operating data.
What is ServletConfig?
javax.servlet. ServletConfig is a servlet API interface. The web container will produce one ServletConfig object for each Servlet class in our application, and this servlet object is passed as an argument to the web container. A configuration objects for servlets that a servlet container uses to send data to a servlet. The web container creates a ServletConfig object for each servlet, which is used to give data to a servlet during setup. This object can retrieve configuration data from an XML file on the web. The ServletConfig interface is used for specific servlets when we want to share information with servlet pages. The benefit of ServletConfig is that if the information is changed in the web.xml file, we don’t need to alter the servlet file.
How to Use ServletConfig Interface?
The servlet page and web.xml file syntaxes show the operation of the servlet config interface.
The following syntax uses in the servlet file to get the initialized data of the web.xml file.
ServletConfig sc_variable = getServletConfig();
out.print(sc_variable getInitParameter("Initial_parameter_name"));
The following syntax uses in the web.xml file:
Code:
<web-app>
<servlet>
......
<init-param>
<param-name>
Initial_parameter_name
</param-name>
<param-value>
Initial_parameter_value
</param-value>
</init-param>
......
</servlet>
</web-app>
The output shows Initial_parameter_value for all servlet pages.
ServletConfig of Methods
The ServletConfig interface contains four methods:
- The “public String getInitParameter(String name)” method
- The “public Enumeration getInitParameterNames()” method
- The “public String getServletName()” method
- The “public ServletContext getServletContext()” method
1. public String getInitParameter(String name)
We must give our servlet a logical name when configuring it in web.xml. We have provided a reasonable parameter name in <servlet-name>. This parameter name helps to return the value of the specific parameter.
2. public Enumeration getInitParameterNames()
The Enumeration returned by this method contains the names of all init parameter names. Here, we can get multiple parameter values using a single method.
3. public String getServletName()
Using init parameters is that we don’t require complex code values in the source code. We can alter values without touching the source code. We have to do is update the web.xml values and re-deploy the application. These initialization options only apply to the Servlet for which we have set them up. We may obtain the value of the init parameters in our Servlet using getInitParameter().
4. public ServletContext getServletContext()
This function returns a ServletContext Object. For each web application, the web container creates a single ServletContext object.
Examples of ServletConfig
The two methods, getInitParameter() and getInitParameterNames(), will be used to retrieve all the parameters from web.xml and its values. The servlet getInitParameterNames() method shows an enumeration of all parameter names, and we can obtain the matching parameter value from web.xml by giving those names when calling the getInitParameter() method.
Example #1
The ServletConfig uses a single parameter example and its output.
ServletconDemo.java:
Code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletconDemo extends HttpServlet {
public void doGet(HttpServletRequest reqt,
HttpServletResponse resps) throws ServletException, IOException {
resps.setContentType("text/html");
PrintWriter ptout = resps.getWriter();
ServletConfig configs = getServletConfig();
String day = configs.getInitParameter("weekend");
System.out.println("Servletconfig Example");
ptout.print("Holiday on: "+day);
ptout.close();
}
}
Web.xml:
Code:
<web-app>
<servlet>
<servlet-name>
ServletconDemo
</servlet-name>
<servlet-class>
ServletconDemo
</servlet-class>
<init-param>
<param-name>
weekend
</param-name>
<param-value>
Sunday
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>
ServletconDemo
</servlet-name>
<url-pattern>/firstservlet</url-pattern>
</servlet-mapping>
</web-app>
Output:
The following output image shows a single parameter value from XML file.
Example #2
The ServletConfig uses multiple parameter examples and their output.
ServletconDemo.java:
Code
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletconDemo extends HttpServlet {
public void doGet(HttpServletRequest reqt,
HttpServletResponse resps) throws ServletException, IOException {
resps.setContentType("text/html");
PrintWriter ptout = resps.getWriter();
ServletConfig configs = getServletConfig();
Enumeration<String> endata = config.getInitParameterNames();
String stringdata = "";
while(endata.hasMoreElements()){
stringdata = endata.nextElement();
ptout.print("<br>Information: "+ stringdata);
ptout.print(" Name: "+config.getInitParameter(stringdata));
}
ptout.close();
}
}
Web.xml:
Code:
<web-app>
<servlet>
<servlet-name>
ServletconDemo
</servlet-name>
<servlet-class>
ServletconDemo
</servlet-class>
<init-param>
<param-name>
Manger
</param-name>
<param-value>
Radhakrishnan
</param-value>
</init-param>
<init-param>
<param-name>
Employee
</param-name>
<param-value>
Ajay Sihna
</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>
ServletconDemo
</servlet-name>
<url-pattern>/firstservlet</url-pattern>
</servlet-mapping>
</web-app>
Output:
The following output image shows multiple parameter values.
FAQ
Given below are the FAQ mentioned:
Q1. What distinguishes ServletContext from ServletConfig?
Answer:
Other servlets are unaware of the ServletConfig parameters because they are supplied for a specific servlet. It serves as an initializing tool.
Outside of any servlet, the ServletContext parameters are provided for the entire application and are accessible to servlets inside the application. It is globally available across all pages because it is application scoped.
Q2. For the ServletConfig object, which method is used?
Answer:
To pass the ServletConfig object to a servlet, use the super. init(ServletConfig) method. Because it gives servlets access to the ServletContext object and startup options, it is essential.
Q3. For a web application, how many ServletConfig objects are created?
Answer:
Every web app component shares the same ServletContext, the only one available for the entire web app. However, the app’s servlets each have their ServletConfig. When a web application is deployed, then Container creates a ServletContext and makes it available to each Servlet and JSP in the web app.
Conclusion
The ServletConfig interface uses to get static data for a particular file. It helps to communicate with the servlet page and initialize the data using an XML file. It enables the developer to get data from one file and does not change each servlet file.
Recommended Articles
This is a guide to ServletConfig. Here we discuss the introduction, and how to use servletConfig interface, methods, examples, and FAQ. You may also have a look at the following articles to learn more –