![]() |
VOOZH | about |
javax.servlet.ServletConfig is an interface as a part of servlet API. For every Servlet class in our application, the web container will create one ServletConfig object and the web container will pass this object as an argument to the public void init(ServletConfig config) method of our Servlet class object. Some of the important points on ServletConfig are:
Suppose one is building a job portal and desires to share different email IDs (which may get changed over time) with to recruiter and job applicant. So, he decided to write two servlets one for handling the recruiter’s request and another one for the job applicant.
Put email-id as a name-value pair for different servlets inside web.xml which can further be retrieved using getServletConfig().getInitParameter(“name”) in the servlet.
There are 4 Methods in the ServletConfig interface
While configuring Servlet in web.xml we have to give a logical name to our servlet. Let's say we have a Servlet class TestServlet and we want to configure it in web.xml.
In <servlet-name> we have given a logical name for our TestServlet class. So, if we use the method getServletName() it will return the logical name of Servlet and in this case, it will return "Test".
This method will simply return ServletContext Object. Web container creates one ServletContext object for every web application.
We can store init parameters as a part of web.xml.
The advantage of using init parameters is we do not need to hard code values in source code and if we are not hard coding values in source code, to change values we don't need to touch source code. We just need to change values in web.xml and re-deploy the application. These init parameters are specific to a Servlet for which you have configured them. By using getInitParameter() we can access the value of init parameters in our Servlet.
This method will return Enumeration having names of all init parameter names.