Two Laptop Bag*
The moose likes Servlets and the fly likes How to pass Session object between two web apps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to pass Session object between two web apps" Watch "How to pass Session object between two web apps" New topic
Author

How to pass Session object between two web apps

Srikanth Reddy
Greenhorn

Joined: Jun 05, 2004
Posts: 10
I have two web applications. webapp1 and webapp2. When a user is logged in webapp1, a session is created and some values are stored in the session. By clicking a link in webapp1 user navigates to webapp2. How to pass the session object created in webapp1 to webapp2, if webapp1 and webapp2 are on the same server and if they are on different servers ?


Srikanth<br />SCJP 1.4
Natarajan Shankar
Ranch Hand

Joined: Jun 10, 2004
Posts: 53
Hi Reddy,
If webapp1 Aand Webapp2 is in same application server then you can pass session objects through servlet-context objects, which is common place holder for all servlet in the same application server.

Shankar
SCJP, SCWCD


Best Regards<br />N.Shankar<br />SCJP, SCWCD
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
The servlet specification does deliberately NOT allow passing sessions between applications. This is for security reasons.

The ServletContext is specific per web application, not per JVM.

From the HttpSession API docs

Session information is scoped only to the current web application (ServletContext), so information stored in one context will not be directly visible in another.


42
Srikanth Reddy
Greenhorn

Joined: Jun 05, 2004
Posts: 10
Originally posted by Natarajan Shanka:
Hi Reddy,
If webapp1 Aand Webapp2 is in same application server then you can pass session objects through servlet-context objects, which is common place holder for all servlet in the same application server.

Shankar
SCJP, SCWCD
Srikanth Reddy
Greenhorn

Joined: Jun 05, 2004
Posts: 10
Originally posted by Natarajan Shanka:
Hi Reddy,
If webapp1 Aand Webapp2 is in same application server then you can pass session objects through servlet-context objects, which is common place holder for all servlet in the same application server.

Shankar
SCJP, SCWCD


Hi Natarajan,

Could you please give some sample code for how to pass session object between servlet contexts ? Thanks in advance.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Originally posted by Kanna Reddy:


Hi Natarajan,

Could you please give some sample code for how to pass session object between servlet contexts ? Thanks in advance.


Like I said, you can't.
The servletcontext is a shared area between session inside the same application, it is not shared between applications.
zamies banno
Greenhorn

Joined: Jun 17, 2004
Posts: 3
Yes its in the context of one webapplication not in the context of the servletcontainer. To share data try using a database or flat file.
Carlos A. Perez
Greenhorn

Joined: Apr 09, 2004
Posts: 27
try this:

In WebApp1 create a servlet X that redirects to Servlet Y in WebApp2 and attach the session parameters as an URL:

sendRedirect ("ServletY?paramSession1=param1¶mSession2=param2");

in this way you dont pass the object but can all the parameters of the session

In Servlet Y in doGet method:

session.setAttribute("paramFromWebApp1", request.getParameter("paramSession"));

and if you want to get back to the WebApp1 redirect again:

sendRedirect("ServletX"); //note that that yo need to put the complete path not just "ServletX"

maybe you'll need a flag to check if the call for ServletX comes form a browser client or from WebApp2

Sorry if my english is bad
Carlos A. Perez
Greenhorn

Joined: Apr 09, 2004
Posts: 27
i forgot something: the url length must not be over 2k if it lengths more than 2k the server may not find the specified url or get an exception (i have recibed both)
Larry Cryderm
Greenhorn

Joined: Mar 23, 2004
Posts: 9
consider doing an HTTP forward to SERVLET2
...in SERVLET1 try:

public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

try{
if(logged_in){
RequestDispatcher rd = request.getRequestDispatcher("SERVLET2_url");
rd.forward(request, response);
}
}
catch(Exception e){

e.printStackTrace();

}

}// end doPost


I shoulda been a rock star.
Tim Hannaford
Greenhorn

Joined: Jun 16, 2004
Posts: 6
I am not sure if thats allowed. I don't think the dispatcher will be able to find that forwarding addresses unless its apart of the same application or else anyone could access anyone elses servlets/jsps.

Each web app is going to need its own session, if they are running as seperate processes. If your using an application server, like JBoss you could consider using a session EJB, or if your not you could serialize the object and write it to file or database, and have the other web app read it from file, or database or whatever storage system your using. Again if your using an application server you could clone the session object and send it through the JMS to the second web app. Your going to need to synchronize the process. The way I would go is using a session EJB, unless you don't have a application server. Then store the session object somewhere and have the second web app read it from memory.
chandana sapparapu
Ranch Hand

Joined: Sep 28, 2002
Posts: 63
Tim is right.

SRV 8.1

The getRequestDispatcher method takes a String argument describing a
path within the scope of the ServletContext. This path must be relative to the root
of the ServletContext and begin with a �/�.[


So, you can not get a reqDispatcher for a servlet in some other webapp.
Erik Brakkee
Ranch Hand

Joined: Jun 21, 2004
Posts: 40
> So, you can not get a reqDispatcher for a servlet in some other webapp.

Actually, you can, using ServletContext.getContext(String uripath).
From there you get the request dispatcher.


SCJP, SCJD, SCWCD, SCBCD
chandana sapparapu
Ranch Hand

Joined: Sep 28, 2002
Posts: 63
Good observation. I've read about this getContext() method, but forgot about it completely. However, one can not rely on this method, for the reason highlighted below. Even if a servlet container returns the context, porting this application needs changes to the pages or servlets.

ServletContext.getContext(String)
public ServletContext getContext(java.lang.String uripath)

Returns a ServletContext object that corresponds to a specified URL on the
server.This method allows servlets to gain access to the context for various parts of the server, and as needed obtain RequestDispatcher objects from the context. The given path must be begin with �/�, is interpreted relative to the server�s document root and is matched against the context roots of other web applications hosted on this container.
In a security conscious environment, the servlet container may return null
 
Consider Paul's rocket mass heater.
 
subject: How to pass Session object between two web apps
 
Similar Threads
Sharing of session objects across two web applications running on same server instance
URL rewriting is not working
Topic: How to get another web app's SessionFactory?
Sharing HttpSession across multiple web applications
How to get another webapp's jndi object from local?