Tuesday 3 September 2013

Servlet Config and Servlet Context objects

Servlet Config and Servlet Context


Fig: ServletConfig
In the init() method of the servlet class, the servlet has got access the two special objects called ServletConfig, ServletContext.

ServletContext

The ServletContext is an object , it is a java object basically as well as ServletConfig. The ServletContext object is only one for web application and ServletConfig is one for each servlet or JSP's.
so all the servlets in the application access the same ServletContext object but each servlet has got its own ServletConfig object. This is one important thing you have to remember. If you are accessing something ServletContext object you know that each and every servlet has got access to ServletContext object.
But Servlet2 does not have access to ServletConfig object of Servlet 1, Servlet2 is only got access to its own ServletConfig object.

ServletContext 
·         It is a interface is used to communicate with the servlet container. \
·         There is only one ServletContext for the entire web application and the components of the web application can share it. ServletContext object is created automatically by the Container as soon as you deployed the application
·         The information in the ServletContext will be common to all the components.
·         Remember that each servlet will have its own ServletConfig. The ServetContext is created by the container when the web application is deployed and after that only the context is available to each servlet in the web application.
When should we use ServletContext:
·         For example we have an Web application with two servlets, here one servlets needs to get some value (technical) from web.xml
·         That is all servlets in the web application can access these context values from the web.xml file
·         In case of ServletConfig , only particular servlet can access the values from the web.xml
Web application initialization:
  1. First of all the web container reads the deployment descriptor file and then creates a name/value pair for each <context-param> tag.
  2. After creating the name/value pair it creates a new instance of ServletContext.
  3. Its the responsibility of the Container to give the reference of the ServletContext to the context init parameters.
  4. The servlet and jsp which are part of the same web application can have the access of the ServletContext.
The Context init parameters are available to the entire web application not just to the single servlet like servlet init parameters.
How to get the ServletContext object into our servlet class:
·         We can access the ServletContext object in 3 ways
In servlet programming we have 3 approaches for obtaining an object of ServletContext interface
1 st method:
ServeltConfig cf=getServletConfig();
ServletContext context = cf.getServletContext();
·         First obtain an object of ServletConfig interface
·         ServletConfig interface contain direct method to get Context object, getServletContext();
2nd method:
Direct approach, just call getServletContext() method available in GenericServlet [pre-defined].  In general we are extending our class with HttpServlet, but we know HttpServlet is the sub class of GenericServlet.
Public class Demo extends HttpServlet
{
Public void doGet/doPost(----))
{
//….
}
ServletContext ctx = getServletContext();
}
3rd method
We can get the object of ServletContext by making use of HttpServletRequest object, we have direct method in HttpServletRequest interface.
public class Demo extends HttpServlet
{
public void doGet/doPost(HttpServletRequest req,-)
{
ServletContext ctx = req.getServletContext();
}
}

How to Retrieve Data from ServletConfig interface object
ServletContext provide these 2 methods, In order to retrieve the data from the web..xml [ In web.xml we have write <context-param>tag to provide the values, and this <context-param> should write outside of <servlet> tag as context should be accessed by all servlet classes ].
In general database related properties will be written in this type of situation, where every servlet should access the same data.
·         public String getInitParameter(“param name”);

·         public Enumeration getInitParameterNames();



What is the use of ServletConfig and ServletContext objects?
Ans:
One thing you can definately do is fetch your initialization parameters.
So what are init parameters?which seen previously in the web.xml that you have a "webapp" element and within that you have number of servlet and servlet mapping elements. i told you that apart from servlet and servlet mapping elements you will have a number of other elements in web.xml within the webapp folder. So one of those are the init parameter elements  so init parameter elements basically whatever initialization parameters you want to put in your servlets or  your apps specifically you can put that initialization parameters values in the web.xml in the init parameter tag .

For example: you want to store the path to the data source URL , you can put that data source URL init parameter element. So you can put init parameter and you can give the parameter name data source. So its start the data source tag put the value for the data source and you close the data source tag. So we have two types of init parameters here Context init parameters here and Servlet init parameters.

Differece beteen Context init parameters and Servlet init parameters?

Ans:
1.  we have seen that Context is one per application whereas servlet is one per servlet  scope for Context init parameters tipically becomes to the whole web container where as the scope for the init parameter is specific to the Servlet or JSP whichever servlet within you write the init parameter so basically when you write the init parameters for a Context init parameter you put it under main webapp element and you do not put inside the any servlet element tag but when you are writing the init parameter servlet you have to put it within the servlet element tag.
How do you get the init parameter value in your servlet code or your init() method so the code you write to get the init parameter is getServletContext() so you basically call these methods getServletContext() and again call the method on so this is going to return the context object and on that Context object you say Context object . get init parameter valueget similarly for servlet you say getServletConfig() so this will give you the instance of the Config object on which you can call the init parameter.

next the deployment descriptor: As I have told you it  goes directly within the webapp element but not within a specific servlet  element but where as Servlet Init parameters the entry goes to the servlet element so whatever servlet you want init parameter to be available you put this init parameter entry in that particular servlet element.

Context init parameters Servlet init parameters
Scope Scope is Web Container Specific to Servlet or JSP
Servlet Code getServletContext() getServletConfig()
deployment Descriptor within the webapp element but not  within a specific <servlet> element within the <servlet> element for each specific servlet

No comments:

Post a Comment