File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Session" Watch "Session" New topic
Author

Session

Kieth Lyman
Greenhorn

Joined: Feb 11, 2009
Posts: 10
Scenario:
We have an inhouse application(http://www.a.com) that 'posts' to a different java application's servlet (http://www.b.com/GetAppServlet?token=1234)
In this servlet, I store a parameter sent over into the session.
I display a jsp page (At this time the session is still the same as prior)
Click a button, posts to another servlet (http://www.b.com/SetAppServlet?...) in the same application.... this is creating a new session.


why?
Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

Hi Kieth,
Please post some code snippets for the same. i think you are creating a new session in your another servlet. please check.



[Vipassana] - It is seeing the reality as it is, And not as you want it to be.!!!
SCJP1.5.
Kieth Lyman
Greenhorn

Joined: Feb 11, 2009
Posts: 10
Hi Pravin,

First Servlet:
HttpSession session = request.getSession( true);
session.setAttribute("x", request.getParameter("x"));

Render jsp page:
...servletcontext.getRequestDispatcher(page.jsp.forward(req,res);

JSP:
<a href="/SetServlet?a=1&b=2"

SetServlet:
HttpSession session = request.getSession(true);
session.getAttribute("x") - THIS IS NULL BECAUSE IT'S A DIFF SESSION (VERIFIED BY DISPLAYING SESSIONID)
Kieth Lyman
Greenhorn

Joined: Feb 11, 2009
Posts: 10
Would it matter if this first application 'a' does a 'response.sendRedirect' vs setting the action of a form and submitting that form
'
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

Kieth Lyman wrote:Would it matter if this first application 'a' does a 'response.sendRedirect' vs setting the action of a form and submitting that form

No. They are identical.
Shefali Naik
Greenhorn

Joined: Dec 09, 2009
Posts: 2
Kieth Lyman wrote:Hi Pravin,

First Servlet:
HttpSession session = request.getSession( true);
session.setAttribute("x", request.getParameter("x"));

Render jsp page:
...servletcontext.getRequestDispatcher(page.jsp.forward(req,res);

JSP:
<a href="/SetServlet?a=1&b=2"

SetServlet:
HttpSession session = request.getSession(true);
session.getAttribute("x") - THIS IS NULL BECAUSE IT'S A DIFF SESSION (VERIFIED BY DISPLAYING SESSIONID)




If I am not mistaken, <a href> tag will not forward the session related data that you created in the previous jsp. Try using c:url JSTL tag instead.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

Shefali Naik wrote:If I am not mistake....

You are mistaken. An anchor link will have no affect on sessions within the same web applications. And, of course, sessions aren't shared across web applications. The use of <c:url> or not is irrelevant.
Kieth Lyman
Greenhorn

Joined: Feb 11, 2009
Posts: 10
response.sendRedirect created a new session on the second webapp from the first servlet...that did work. This is different than the 'submit'.
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

Kieth Lyman wrote:response.sendRedirect created a new session on the second webapp from the first servlet...that did work. This is different than the 'submit'.
Hum... I was checking the API and there is nothing about it. They don't metion a new session: http://download.oracle.com/javaee/1.4/api/javax/servlet/http/HttpServletResponse.html#sendRedirect%28java.lang.String%29
Do you have any link about it?

I was thinking and maybe it's because the redirect method it's sending the user to another JVM.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
Kieth Lyman
Greenhorn

Joined: Feb 11, 2009
Posts: 10
Bear, did you mean 'context' is not shared between apps.
Session should not be a problem as it is based on the session id of the client which is passed in a cookie in the request.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, he meant session. (Which is a context, btw.)

Passing a session ID from one webapp into another doesn't mean they're sharing sessions.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61417
    
  67

You can't share sessions between web apps. Period.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Session