Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • 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?



 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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 VI Editor Debian
  • 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: 21999
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Netbeans IDE Tomcat Server Java
  • 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
Eclipse IDE Tomcat Server Debian
  • 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
Netbeans IDE Tomcat Server Java
  • 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
https://coderanch.com/t/560259/JSP/java/passing-session-values-between-two

 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic