ServletContext object is common to all the servlet but ServletConfig object is different for all the servlets and must be created at the time of container start-up for every Servlet. ServletConfig object of one servlet is abstract from ServletConfig object of another servlet.

Important points regarding ServletConfig object

1. It is once created for every servlet designed in the Application at container start-up.
2. init() life cycle method is responsible for initializing ServletConfig object.
3. Container is responsible for calling init() and is the first life cycle method to be called at the container start-up.
4. Initializing value must be mapped in the web.xml under particular < servlet > tag with < init-param > tag.
5. < param-name > and < param-value > tag is used to provide initializing value for ServletConfig instance.
6. We can have more than one servletConfig object in a single web Application.

How to get the ServletConfig Object ?
It is possible with the help of getServletConfig() method which returns ServletConfig object.

How do we get control over ServletConfig object in our Servlet class?
This can be possible in one ways :

1. With current servlet class Object : By calling getServletConfig() method.

ServletConfig config = this.getServletConfig() ; 

Note : getServlteConfig() method is defined in the abstract class GenericServlet which is extended by HttpServlet. And our Servlet class also extends HttpServlet that means our servlet class is indirect sub-class of GenericServlet class and hence its method can be accessible with its Object.

How to initialize ServletConfig object with some values?
It should be done at web.xml file under <  init-param > tag using < param-name > and < param-value > and shouble be mapped with particular servlet  under < servlet > tag and hence will be specif to that servlet only.

How to get the < init-param > value ?
We can get through getInitParameter() method which is defined in the ServletConfig interface so can be called by its sub-class object.

Note : Not recommended to override init(ServletConfig config) method because it is internal call by the container to initialize ServletConfig instance with the mapped value in the web.xml under < init-param > tag.


Related Posts:

  • ServletConfig Object in Servlet ServletContext object is common to all the servlet but ServletConfig object is different for all the servlets and must be created at the time of container start-up for every Servlet. ServletConfig object of one servlet is a… Read More
  • HttpSession Object in Servlet In servlet we are getting the session Object in terms of HttpSession by calling getSession() method with the HttpServletRequest object. What is a session ? Session is a object which uniquely identifies the active user so t… Read More
  • RequestDispatcher Interface in Servlet RequestDispatcher is an interface which has two important abstract methods defined. 1. include() 2.  forward() Both the methods are used to delegates the control from one Servlet to another Servlet / JSP or from one JS… Read More
  • ServletContext Object in Servlet ServletContext is only a single object to a whole Servlet Application. It is once created and initialized by the web container at the container start-up. A Web application has many Servlet classes and for every servlet… Read More
  • RequestDispatcher with ServletContext We can get the RequestDispatcher object with the ServletContext simpley by calling getRequestDispatcher(String url) with the ServletContext object which finally returns the RequestDispatcher object. We have two possible opt… Read More

0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

201976

Online Members

Live Traffic