Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help on question from j2eecertificate

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the following statements are correct for distributable web-applications?

1. You cannot rely on the ServletContext to share data.
2. You cannot rely on ServletConfig for distributed web-applications.
3. Each JVM has a separate instance of a servlet context.
4. HttpSessions can be shared across distributable web-applications.

as per me - the answers are - 3,4..! 1,2 would have been ok if they had a CAN in the statements

but as per j2eecertificate webiste this is the answer

Answer 1, 3 and 4 are correct.

You cannot rely on the ServletContext to share data, each JVM in the distributed web-application has it own instance of the ServletContext. Setting an attribute on one host will not be replicated on the other host(s).
The ServletConfig is a static class and can only be used to obtain parameters for the servlet in the servlet context. Even if many instances exists the data will be identical.
HttpSessions can be used in a distributed environment. HttpSessions can serialize their attributes to a file-system or datastore and can be used across different environments.

please help !
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each JVM in distributed environment has its own ServletContext object.
That means :-
Suppose our project is distributed to two JVMs (Say JVM1 and JVM2) and that has two Servlets called ServletA and ServletB.
1) A client request to ServletA.
2) The main container invokes ServletA of JVM1. ServletA adds some attributes to ServletContext object.
3) Another client request to ServletB.
4) The main container passivate the request to JVM2 container.
5) JVM2 container calls ServletB.
6) ServletB is trying to access attributes in the ServletContext object.

Now think !!

ServletB is having another context object, so the the attributes passed by the ServletA is not accessable to ServletB.

Simple
 
VijayKumar Sivagnanam
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

you can rely on ServletContext to share data if the app is in single JVM. If the app is distributed with 2 JVMs, then 2 ServletContext objects will be created and the attributes set in ServletContext in one JVM wont be available in the other. so we cant share the data in Servlet context across JVMs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic