How to create a session to enter and exit in java

This is the code I wrote on the login page

HttpSession session = request.getSession(true);
session.setAttribute("name", user1);        
String nme=(String) session.getAttribute("name");

And, This is the code for logout.jsp

<% request.getSession().invalidate();

OR

if(session!=null){
   session=null;
}

OR

 request.getSession().setAttribute("name", null); //it just assigns null to attribute

 response.sendRedirect("login.jsp");
 %>

the session is created, but it works after logging out ... I want this return button to not work.

+4
source share
3 answers

To exit or invalidate the current session, you have the correct code, as shown below.

request.getSession().invalidate();

Now, when you click the back button of the browser, it will load the page from the cache. Therefore, to take care of this situation, you can do less than two things.

  • API HTML 5 History, "" .

  • , , - -.

/ , , .

        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "-1");

, .

+6

, .

request.getSession.removeAttribute("name")

, :

if(request.getSession.getAttribute("name")==null){

}
0

, , ( "" )

localtion.href.replace .

localtion.href.replace (url): replace the current document with the provided URL. The difference from the assign () method is that after using replace () the current page will not be saved in the history session, that is, the user will not be able to use the "Back" button to navigate to it.

0
source

All Articles