Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass Session object between two web apps

 
Srikanth Reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Natarajan Shankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srikanth Reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Hannaford
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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.
 
chandana sapparapu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic