Narendra Naidu
A lot of books and examples mention that it"s necessary to include a call to super.init in the init method of our servlets. This means that, your Servlet code looks like this:
class MyServlet extends HttpServlet
{
public void init(ServletConfig config)
{
super.init(config);
//your code goes here.
}
}
It is important to understand that the "init(ServletConfig)" method of the Servlet is called by the Servlet Container when the servlet is initialized. It is the Servlet Container that passes the ServletConfig object as a parameter to the init method.
GenericServlet is the super-class of HttpServlet. When you call "super.init(config)" in the init method as shown above, you are actually passing the config object to the constructor of the GenericServlet. What does GenericServlet do with the ServletConfig object? Simple. It just stores the ServletConfig object reference as a member variable, so that you can access the ServletConfig object whenever you want it.
The code for GenericServlet is given below:
public class GenericServlet
{
ServletConfig config = null;
public void init(ServletConfig config) throws ServletException
{
this.config = config;
}
public ServletConfig getServletConfig()
{
return config;
}
//..........
}
So whenever you need the ServletConfig object in the Servlet, you can obtain it as shown below:
ServletConfig config = getServletConfig();
What is the significance of this ServletConfig object? Why go through all the hassle of storing its reference?
The ServletConfig object is the interface through which servlets can communicate with the container in which they are running. The ServletConfig object contains a member variable of type ServletContext, so with the help of the ServletConfig object we can obtain the ServletContext object.The ServletContext object is also called the WebContext object.
You retrieve the Web context with the getServletContext() method of the ServletConfig. The Web context is useful because it provides methods for accessing:
- Initialization parameters ( Access init parameters )
- Resources associated with the Web context: getRequestDispatcher(java.lang.String path)
- Object-valued attributes (Store and retrieve attributes from a ServletContext. Can be shared across all servlets)
- Logging capabilities (log to the container log file using log() method)
You can include the following code in your servlet:
ServletContext context = getServletConfig().getServletContext();
and then use this ServletContext object for the above mentioned tasks.
But often developers used to forget calling "super.init(ServletConfig config)" in the init of their Servlets and also it was a inconvenient hassle. In the later version of the Servlet API, a very clever change was made in the code of Generic Servlet. Now the Generic servlet contains 2 init methods. One which takes a config parameter and the other is a no-parameter init.
Have a look at the source code snippet of Generic Servlet in the new Servlet API
public class GenericServlet
{
public void init() throws ServletException
{
}
public void init(ServletConfig config) throws ServletException
{
this.config = config;
log("init");
init();
}
//.....
}
The first init() method was added as a convenience method which can be overridden so that there is no need to call super.init(config). Instead of overriding "init(ServletConfig)", simply override the "init()" method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().
So now you just have to override the init() method ( and not the init(ServletConfig config) ) method in our servlets.
And you may obtain the reference of ServletConfig and ServletContext by calling the 2 convenient methods of Generic Servlet: getServletConfig() and getServletContext()
Naren
(BornToCodeInJava)
Narendra is an architect specializing in Enterprise Applications using J2EE and .NET technologies. He is a Java evangelist and also a SUN certified Java Programmer. Currently working in Wipro Technologies and involved in the implementation of J2EE/.NET projects. Other areas of interest include Web Services, Design Patterns, J2ME . Been a active freelance Java trainer for corporates and training institutes. Can be reached at naren@sulekha.com