java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to write and read object from HTTP Session?

<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Session Read & Write</title></head>
<body>
<%
    //
    // Creates a session attribute named login-date to store a java.util.Date.
    //
    session.setAttribute("login-date", new Date());

    //
    // Read back the java.util.Date object from the session attribute.
    //
    Date loginDate = (Date) session.getAttribute("login-date");
%>

Login Date: <%= loginDate %>
</body>
</html>