mercredi 5 avril 2017

Java EE don't update after login

I have a Website with Java EE Backend. I just coded the login system and it's working fine.

I have an included header in all the pages that have some options (Links) for non logged users and should show other options if an user is logged in. However, after I log in, I have a dispatcher to the Account page that is only accesible is you're log in.

The problem is that when I first log in the Header updates and shows the user the other options, but when I click in a link of the header and change the page ex: to Index. The page is shown but the header is like I'm not logged in. It also shows me the login page, that I should be able to se because I'm in.

I have a login action that handles the result of the login form:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        HttpSession session = request.getSession(true);
        RequestDispatcher dispatcher;
        boolean exists = false;
        if(action!=null){
            switch (action){
            case "login":
                DAOLogin dl = new DAOLogin();
                exists = dl.authenticate(email, password);
                if(exists){
                    DAOUser du = new DAOUser();
                    int id = du.getUserId(email);
                    String name = du.getName(email);
                    String country = du.getCountry(email);
                    session.setAttribute("isLoged",true);
                    session.setAttribute("userid", id);
                    session.setAttribute("name", name);
                    session.setAttribute("email", email);
                    session.setAttribute("country", country);
                    request.setAttribute("message", "Successfully loged!");
                    //response.sendRedirect("/Account");
                    dispatcher = getServletContext().getRequestDispatcher("/Account");
                    dispatcher.forward(request, response);  
                }else{
                    session.setAttribute("isLoged", false);
                    request.setAttribute("error", "Wrong email or password.");
                    dispatcher = getServletContext().getRequestDispatcher("/Login");
                    dispatcher.forward(request, response);  
                }
                break;
            case "logout":
            default:
                session.invalidate();
                request.setAttribute("message", "You loged out.");
                response.sendRedirect("/");
            }
        }else{
            response.sendRedirect("/Login");
        }
    }

I also have in the JSP pages a check to see if the user is logged in (Login Page):

<c:choose>
    <c:when test="${sessionScope.isLoged}">
        <div id="affordable">
            <div class="container">
                <div class="row">
                    <div class="col-md-8 col-md-offset-2 text-center heading-section animate-box">
                        <h3 style="color: white;">YOU ARE ALREADY LOGGED IN</h3>
                            <p style="color: white;">
                                Go to <a href="/Account">Account Panel</a>.
                            </p>
                    </div>
                </div>
            </div>
        </div>
    </c:when>
    <c:otherwise>
        <-- SHOW LOGIN PAGE -->
    </c:otherwise>
</c:choose>

Hope someone can help me.

Thanks!

Aucun commentaire:

Enregistrer un commentaire