• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question regarding Head First book chapter 7 mock question

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Head First Book Chapter 6 mock questions Question No. 7 (page 274 - if you search from PDF, it shows me as page 271). The question is to select "true" statements and the following is mentioned as true statement:

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



Can somebody help me understand how this could be true? Isn't any attribute bound to a session available to the only servlets that are accessed in the same session? How could it be available to all servlets in the web app (refer the phrase - same ServletContext? The answer mentions this as true statement.

Thanks and regards,
TV Padmanabhan
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That statement isn't true. Check the Errata.
 
Pad Ven
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abhimaran,
Thank you for the clarification. I was looking at this errata page http://oreilly.com/catalog/errata.csp?isbn=9780596516680 but could not locate it. Can you point out where it is or am I looking at some old version of the errata

Thanks,
Pad
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very sorry for that wrong information Pad Ven, Here is the servlet specification, have a look on that.


SRV.7.4 Binding Attributes into a Session
A servlet can bind an object attribute into an HttpSession implementation by name. Any object bound into a session is available to any other servlet that belongs to the same ServletContext and handles a request identified as being a part of the same session.

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So basically, Pad was right by saying:

Isn't any attribute bound to a session available to the only servlets that are accessed in the same session? How could it be available to all servlets in the web app (refer the phrase - same ServletContext?



For me, this question was also very awkward.
Session attributes are visible for the servlets which belongs to the same session.

By the way, there is a question: what should you understand by "same ServletContext".

Should you treat two identical applications in a cluster (working on different JVMs) as using the "same ServletContext"?
This is true if you understand it as a "logic context", but not when you treat it as a ServletContext object. This ServletContext object will be different on both JVM's but you can still access session attributes from both of them.

Please straighten me if I'm not right.

Cheers
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also very confused with this line:

Any object bound into a session is available to any other servlet that belongs to the same ServletContext and handles a request identified as being a part of the same session.

Is there any example for this??
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Isha - you can try it yourself.

Create a web application.
Create a Servlet1 class which will override the doGet(...) method and sets an attribute in session scope.
Create a Servlet2 class which will override the doGet(...) method and reads an attribute in session scope.

Now enter Servlet1 mapped URL and then enter Servlet2 mapped URL.

If Servlet2 prints the value set in Servlet1, well, this is basicaly what you were asking for:

Any object bound into a session is available to any other servlet that belongs to the same ServletContext and handles a request identified as being a part of the same session.



Cheers!
 
Isha Garg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for this example.

Ya,I agree that Servlet2 will print the value set in Servlet1,
what is confusing me is the text 'same ServletContext'

How can we say that Servlet1 and Servlet2 belong to same ServletContext? Is it something related to context path..? or does it mean like any servlet belonging to the same application..?
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you can say that the same ServletContext is the same as an application.

It's the yourApplication directly under i.e. tomcat_root/webapps/yourApplication

You can say that Servlet1 and Servlet2 belong to the same ServletContext (application) by taking look at the web.xml file under tomcat_root/webapps/yourApplication/WEB-INF/web.xml and also by noticing that *.class files are located under tomcat_root/webapps/yourApplication/WEB-INF/classes package structure.

But remember that (as mentioned in previous posts) that your single application can be deployed in clustered environment; then all copies of your application will have their own ServletContext (each per JVM).

HTH!
 
Isha Garg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya, Thanks for the reply. It makes sense to me now...

But again regarding clustered environment, as on page 257 of HeadFirst, it says only HTTP session objects move from one VM to another.
All other things are copied.

Even if wwe see in the figure at page 257, only session object is different for both VM1 and VM2, all other things like servletcontext, servlet config are copied on both the JVMs.

Does not it mean that applications in clustered environment also share the same servletContext?
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Isha - ServletConfig object is not shared in clustered environment - just take a look at "WATCH IT" on page 159 of K&B Head First book.
 
Isha Garg
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Pedro.
Ya, you are right even at p-257.. It is written that There is one ServletContext per VM. There is one ServletConfig per servlet, per VM.

Sorry for making the topic confusing...
reply
    Bookmark Topic Watch Topic
  • New Topic