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.
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,
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?).
Following are the methods of ServletConfig interface as it is given in Servlet API.
String getInitParameter(String name)
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
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.
Returns a reference to the ServletContext in which the caller is executing.
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?"