What is ServletConfig interface?


The needs of server-side programming are well thought and architectured by Servlet Designers. If a specific Servlet should be started with some default properties well-suited for certain job, the question is where the properties should be assigned. The properties may change seldom and each time the Servlet should be compiled and deployed is also another problem to be taken care of. Keeping in mind all these, Designers introduced ServletConfig interface.

What is ServletConfig?

ServletConfig interface is used to assign properties to a Servlet at the time of Servlet object creation itself (known as Servlet Initialization Parameters) by the container; exactly the way you use constructor in case of applications. Now let us come into the subject.

web.xml is known as deployment descriptor, where in, the deployment particulars (that also include initialization parameters) of a Servlet are written by the Programmer. The particulars are declared in <init-param> tag in web.xml file. To read this, <init-param> tag data, ServletConfig interface is used.

Infact, an object of ServletConfig is created by the Container itself at the time it creates a Servlet object. For every object of Servlet, the container creates, also an object of ServletConfig. This ServletConfig object is used to communicate with the Servlet under execution. Eventhough, the contianer creates the ServletConfig object implicitly for its purpose, the Programmer can make use of the object in his code to read initialization parameters into the Servlet from web.xml file.

If the parameter values change, the web.xml file need not be compiled. At the time reading, whatever values exists in the web.xml file, with those values or parameters only the Servlet is initialized.

Some points to be known with ServletConfig interface (What is ServletConfig?).

  1. For each Servlet under execution, a ServletConfig object is created by the Servlet container and is used by the Programmer to read the Servlet specific data declared (written) in web.xml in the form of tags.
  2. ServletConfig is an interface from javax.servlet package.
  3. The getServletConfig() method of GenericServlet returns an object of ServletConfig.
  4. The ServletConfig object created by the Web container for a specific Servlet cannot read other servlet’s init-param data.
  5. ServletConfig object is specific for a Servlet. Other way to say, if 100 servlet objects are being executed in the container, 100 ServletConfig objects are also created implicitly used by the container to communicate with the Servlet.
  6. When the container destroys the Servlet object, along with it its corresponding ServletConfig object also destroyed.
  7. Programmer can make use ServletConfig object to read tags of the Servlet.
  8. <init-param> tag data is known as initialization parameters and are used to initialize the Servlet with some special properties.
  9. The method of ServletConfig object used to read is "String getInitParameter(String)".
  10. The data, to be read by ServletConfig, is written within <servlet< tag.

Following are the methods of ServletConfig interface as it is given in Servlet API.

Sl.No. Method Sumamry
1 String getInitParameter(String name) Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
2 Enumeration getInitParameterNames() Returns the names of the servlet’s initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
3 ServletContext getServletContext() Returns a reference to the ServletContext in which the caller is executing.
4 String getServletName() Returns the name of this servlet instance.

A full program with explanation and output screen is available at init param Example using ServletConfig.

Pass your comments and suggestions on this tutorial "What is ServletConfig?"

Leave a Comment

Your email address will not be published.