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.


0 comments:

Post a Comment

Ads 468x60px

.

Ads

.

Featured Posts

Popular Posts

Like Us On FaceBook

Total Pageviews

Online Members

Live Traffic