Search code examples
javajspsessionstruts

Struts 1.3 Session Management


I am using the session object to store the logged in user id in signUpAction.java

HttpSession session = request.getSession();
if(login>0&&candy>0){                
    session.setAttribute("username", val.getUsername());
    result = "success";    
}

then i forward the user to a jsp page corresponding to his/her id.

User name is `<bean:write name="signUp" property="username"/>`

The problem is that when the user clicks the back button, he/she logs back out(without clicking any logout button). I want a session to be maintained until the user clicks on the logout button. And how do i check whether the user is logged in on every page? I am using Struts 1.3 and I'm new to it. Please help me. I'm trying to develop a Job Portal web app as a project.


Solution

  • Here is how I did it. In my validateAction class file

    HttpSession session = request.getSession();
    session.setAttribute("username", val.getUsername());
    

    After that on every JSP page i wrote

    <c:choose>
            <c:when test="${not empty sessionScope.username}">
                // all the codes goes here
            </c:when>
            <c:otherwise>
                <h1>Please Login First.</h1>
            </c:otherwise>
    </c:choose>
    

    And in the master page I included

    <body onload="window.history.forward(1);">
    

    so even when the user clicks the back button the browser won't load the last cached page.

    and when the user clicks the logout button, I call my invalidateAction class file

    HttpSession session = request.getSession();
    session.removeAttribute("username");