sessionstrutsstruts-1

How do I check whether the user is logged in on every page in Struts 1?


I am using the HTTP 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");