• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Can servlets in individual contexts have access to the same session object?  RSS feed

 
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a simple program where i can put a HashMap on an application scoped session and get two applications/contexts deployed as two war files access the HashMap.

Servlet 1



Servlet 2



To test this i restarted Tomcat 6 then run access Servlet 1 first so that it initializes the Hashmap object. When i subsequently access Servlet 2 i get a NULL-POINTER error which is pointing at the line number where i tried to call newMap2.size()

What am i doing wrong?



 
Author and ninkuma
Marshal
Posts: 66797
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, you are not storing the object in session scope, you are storing it in application scope.

Secondly, each web context has its own application scope and other web app's cannot simply access it.
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any way i can one of the applications to see the data of the other application?
 
Sheriff
Posts: 21450
95
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you should ask yourself if these two web applications actually need to be separate applications in the first place. If they need each other's objects then they are tightly coupled and should perhaps be merged into one application.
 
Greenhorn
Posts: 18
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's better to save the content in a database like oracle from one servlet and access the content in that database from another servlet.
 
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While one application context cannot communicate internally with another, there are any number of ways two contexts could share information. A previous poster suggested a database for a persistence layer, which is a pretty common approach. Other options include reading / writing a local file both contexts would have access to, or even using HTTP to exchange data between the two contexts.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can share two applicatoin's context by corss context sharing. In tomcat you can do it by configuring crossContext attribute of Context tag.

If you are using tomcat, can Refer.
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html. search for crossContext.
 
lakshmana dhanimireddi
Greenhorn
Posts: 18
Java Netbeans IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we set the crossContext property to true, we can only access the resources such as servlets,jsps,other files in other web app. But we can't access session object in other context. We can write resource(servlet or jsp) which gives the info'n about the session object in other context.
 
Bharat Kasodariya
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can access and set data with set/get Attribute in context of other app.

This post might be helpful for you
http://www.coderanch.com/t/560259/JSP/java/passing-session-values-between-two

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!