JSP Impilicit Objects - session


JSP session -

session is an object of javax.servlet.http.HttpSession. In servlet we can get it by using getSession() method, whereas in JSP it is automatically available.

Example -

This basic example will show you how to use session in JSP file.

1- index.html or index.jsp - This will be your default page.
<!DOCTYPE html>
<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <a href="First.jsp">Click Here to show session information </a>
    </body>
</html>        

2- Create a JSP file named First, where we will use session implicit object to get id, creation time , last accessed time and expiry time of session.
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <title>JSP Page</title>
    </head>
    <body>
        <%
            String session_id = session.getId();
            long session_creation_time = session.getCreationTime();
            long session_last_accessed_time = session.getLastAccessedTime();
            int expiry_time = session.getMaxInactiveInterval();
            
            out.println("Session id is "+session_id);
            out.println("<br>");
            out.println("Session creation time "+ new java.util.Date(session_creation_time));
            out.println("<br>");
            out.println("Session last accessed time "+ new java.util.Date(session_last_accessed_time));
            out.println("<br>");
            out.println("Session expiry time "+expiry_time+ " seconds");
        %>
    </body>
</html>        

3- Now run the project and it will show you the default page as follows -



Click on the link and it will show you session information as follows -



Download this example (NetBeans implementation) - Click Here