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 there is common object called as ServletContext. So if we have the requirement to have some common service between those servlets then we can be able to provide by initializing ServletContext object with < context-param > tag in the web.xml and hence can be accessed with any of the Servlet instance or ServeletConfig object.

About ServletContext interface

ServletContext is a interface defined in the servlet-api which has many methods defined and can give any required informations about a Servlet Application.

Context object is created by the web container so how we can get this object in our servlet class?
By calling getServletContext() method by our own defined servlet object or ServletConfig object.

1. With ServletConfig Object -->  Our Servlet class extends HttpServlet class which internally extends GenericServlet which implements Servlet and ServeletConfig  interfaces. And ServletConfig has getServletContext() method.

ServletConfig config = this.getServletConfig();
ServletContext ctx = config.getServletContext();

2.  With our own Servlet Object --> This can be achieved by calling getServletContext() method with " this " keyword.

ServletContext ctx = this.getServletContext();

3. With HttpSession object --> This can be done by calling getServletContext() with HttpSession object.

HttpSession session = request.getSession();
ServletContext ctx = session.getServletContext();


How to initialize ServletContext with some values ?
It should be done at the web.xml file under < context-param > tag using  < param-name > and < param-value > but should not be under < web-app > because it is common to all the servlet so must be mapped globally. 

<context-param>
    <param-name>   </param-name>
    <param-value>  </param-value>
</context-param>

< param-name > contain any logical name. 
< param-value > contains value initialized to ServlteContext Object.  




Related Posts:

  • URL Re-writing in Session Handling URL Re-writing is a technique by which session data can be handled within the server side only.Client is not responsible for keeping any information regarding session. In this process at every request the required session da… Read More
  • Cookies in Session handling Cookies is the concept of storing the client's information in the client machine itself but in the form of a file called as Cookies. So it contains all the information sent by the Web server with the response header. previ… Read More
  • Hidden Form field in Session handling One of the major disadvantage with the URL Rewriting is that passing session data with the URL makes visible to every one who are requesting for the resource. To over come this problem the concept of hidden form field is i… Read More
  • Handling Session in Servlet Handling session defines how session should be managed internally. Session is meant for keeping track of user's information which is required further for the next proceeding requests. Scope of a session is alive from its cr… Read More
  • include() in servlet include() is a method defined in the RequestDispatcher interface with ServletRequest and ServletResponse as a parameter. It is same as forward with slight difference of storing the content of both source and target resourc… 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