Thursday, January 9, 2014

To remove a session attribute/ To expire a session in JSP

Suppose you want to remove a session. Like you are creating a Log-Out page for your website and want the user session to expire , for that you need to follow the given code!

<%
            String res=(String)session.getAttribute("username");
            if(res==null)
            {
                out.println("Please Login First!!");
            }
            if(res!=null)
            {
                session.removeAttribute("username");
                out.println("You have successfully logged out!");
                out.println("To continue ...Please login");
            }
 %>

In this example when the user successfully log-ins we create a session Attribute and put his username in the session!
username=rs.getString("firstName");
//Here we get the name of user from database.
session.setAttribute("username",username);
//Put it in the session 

No comments:

Post a Comment