aspose file tools*
The moose likes Servlets and the fly likes Session Attributes and ServletContext Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session Attributes and ServletContext" Watch "Session Attributes and ServletContext" New topic
Author

Session Attributes and ServletContext

Ravi Goli
Ranch Hand

Joined: Feb 13, 2005
Posts: 31
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

Joined: Mar 17, 2001
Posts: 213
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


Sarath Mohan
ramprasad madathil
Ranch Hand

Joined: Jan 24, 2005
Posts: 489

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

Joined: Dec 11, 2004
Posts: 13410

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".


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session Attributes and ServletContext
 
Similar Threads
Servlets/jsp
Doubt regarding session attributes
SessionAttributes Doubt?
HFS page274, question7
someone pls explain me this statement...