0

I am doing a multi user login application where I use session to store the user object.

HttpSession session = request.getSession(true);
  session.setAttribute("user",user);

In every page I am checking using JSTL whether the user object is present in the session.

<c:choose>
   <c:when test="${not empty sessionScope.user}"> 
    //jsp code
    </c:when>
   <c:otherwise> 
    <logic:redirect forward="welcome"/>
  </c:otherwise>
</c:choose>

My problem is that if the user clicks on a href link in the application the user changes to Previous user in the session. i.e. the it is loading the user from cache. If I refresh the page it will load the correct user.

How could I fix it?

TanNM
  • 1
  • 1

2 Answers2

1

If you are using Struts2 it might make more sense to have that check in an Interceptor instead of repeating the code in every JSP.

http://struts.apache.org/2.0.11/docs/interceptors.html

Nate
  • 2,407
  • 22
  • 22
  • Please help me, because Interceptor not fix my error! Previous user still in the session Interceptor. – TanNM Apr 14 '11 at 05:08
0

Nate is right, this is best handled in an Interceptor. This answer may be of use to you.

Edit

It sounds like this might be a page caching issue, then. Try setting a breakpoint in your Java code so that you can inspect what the user object in the session is. Assuming that the user object is always correct and it is just the page showing incorrect information, then try setting no-cache headers as detailed here.

Community
  • 1
  • 1
Steven Benitez
  • 10,936
  • 3
  • 39
  • 50
  • Thanks for reply. I used a Interceptor same in you link, but i want get user in jsp page, solution for it? – TanNM Apr 14 '11 at 03:35
  • The ${sessionScope.user} should work assuming that the user object is in session as "user". The logic tag wont work, that's from Struts 1. – Steven Benitez Apr 14 '11 at 04:10
  • Please help me, because Interceptor not fix my error! Previous user still in the session Interceptor. – TanNM Apr 14 '11 at 05:07
  • Hi Steven Benitez! Thanks for reply I add into my jsp page code belower but not right! ` <% response.setHeader("Cache-Control","no-cache"); //HTTP 1.1 response.setHeader("Pragma","no-cache"); //HTTP 1.0 response.setDateHeader ("Expires", 0); //prevents caching at the proxy server %>` – TanNM Apr 19 '11 at 03:05
  • Please revise your question to add the code that you have tried. Also, did you set a breakpoint in the Java and see what the actual value in the session is? – Steven Benitez Apr 19 '11 at 04:32