Search code examples
servletslogoutback-button

Login with html using java


I have made a dynamic Web application using eclipse. I have a static html page (login page) that leads to the application using servlets. Then if you logout and then press the browser back button it let's you go back. How do I prevent this from happening? I think it's called session management


Solution

  • This is the required code to manage session using cookies

    if(userID.equals(user) && password.equals(pwd)){
                Cookie loginCookie = new Cookie("user",user);
                //setting cookie to expiry in 30 mins
                loginCookie.setMaxAge(30*60);
                response.addCookie(loginCookie);
                response.sendRedirect("LoginSuccess.jsp");
            }else{
                RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");
                PrintWriter out= response.getWriter();
                out.println("<font color=red>Either user name or password is wrong.</font>");
                rd.include(request, response);
            }
    

    Use it according to your web application project.