• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session Attributes and ServletContext

 
Ravi Goli
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Iam reading the Head First Servlets and JSP by K&B&B.
In the Mock Test of the Session Management chapter, I see the following statement(Q-7) being stated as TRUE:

Attributes bound into a session are available to any other servlet that belongs to the same ServletContext.

I am trying to know:
How are the Session Attributes related to the Servlet Context?
so that they can be accessed by any Servlet.

Iam Confused.

Can somebody help me understand the concept in the above statement.
Thanks,
Ravi.
 
Sarath Mohan
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are the Session Attributes related to the Servlet Context?
so that they can be accessed by any Servlet.


Here the servlet context means the current web application. Don't try to realate session object with servletContext object.

What they mean is if two servlets are in the same web application (servletContext) session object is sharable among these servlets

Regards
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A ServletContext represents one web application.

A context is the root of the web application. In a url like http://domain ort/ctx, ctx is the context root of the application. The web application will usually be deployed as a war with the same name 'ctx' or in exploded directory format where the top level directory name is 'ctx'.

A j2ee web container may host several such web-applications. Each web application may have several servlets, jsps and several other static resources associated with it. All resources live under the root folder that's the context in a manner dictated by the j2ee spec. The container on initialization assigns a context to each web application that's deployed within it (as a war file or in exploded directory format).

A Session exists per Context. Once a session is created for a Context, this session can be accessed and modified by all other resources (Servlets, Jsps) that belong to this Context. A Servlet would belong to that context by virtue of
1. being registered in that web application's web.xml and/or
2. Its class files located under the root folder of that context. (in the WEB-INF/classes directory under the root folder that represents one context)
The same hold true for jsps too though the location that they are stored and the way they are registered (optional) differ from Servlets.

Thanks,
Ram.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Attributes bound into a session are available to any other servlet that belongs to the same ServletContext.


It should also say "and can be retrieved from any request that belongs to the same session".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic