Program to demonstrate session management
using HttpSession
// Session
management
import
javax.servlet.http.*;
import
javax.servlet.*;
import
java.io.*;
import
java.util.*;
public
class SessionMgt extends HttpServlet
{
private int counter = 0;
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
//setting the content type
res.setContentType("text/html");
//create the stream to write the
data
PrintWriter
pw=res.getWriter();
counter++;
//writing html in the stream
pw.println("<html>");
pw.println("<head>");
pw.println("<title>Session</title>");
pw.println("</head>");
pw.println("<body><h3>");
pw.println("<center>");
pw.println("<h2>Servlet Session Management</h2><hr/><br/>");
HttpSession hs = req.getSession(true);
// Display date/time of last access.
Date dt = (Date)hs.getAttribute("date");
if(dt != null)
{
pw.println("Last
access: " + dt + "<br/><br/>");
}
// Display current date/time
dt
= new Date();
hs.setAttribute("date", dt);
pw.println("Current date: " + dt+
"<br/><br/>");
pw.println("<hr/>Hit Count =
"+counter);
pw.println("</center>");
pw.println("</h3></body></html>");
//closing the stream
pw.close();
}
}
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu