Logo 
Search:

OOAD Articles

Submit Article
Home » Articles » OOAD » Servlet RSS Feeds

Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object

Posted By: Milind Mishra     Category: OOAD     Views: 2011

Write a program using session tracking capability of Servlets, which displays information
of current user session by HttpServletRequest object. It should also display all other
current sessions by showing their session Ids and number of times they have visited that
page.

Code for Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object in OOAD

// sessionTrack.html

<html>
<body>
<form action="http://localhost:8080/examples/servlet/sessionTrack" method="get">
<input type=submit value= submit>
</form>
</body>
</html>


// sessionTrack.java

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

publicclass sessionTrack extends HttpServlet 
{
  
    publicvoid doGet(HttpServletRequest req,
                      HttpServletResponse res)
        throws IOException, ServletException
    {

      try
     {
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
          HttpSession session = req.getSession();
          out.println("sessions.id" + " " + session.getId());
          out.println("<br>");
          out.println("sessions.created" + " ");
          out.println(new Date(session.getCreationTime()) + "<br>");
          out.println("sessions.lastaccessed" + " ");
          out.println(new Date(session.getLastAccessedTime()));
    }
      catch(Exception e)
     {
        System.out.println("Error:"+e);
     }
   }
}


// OUTPUT

sessions.id 473BF7EF17DBFC455CE7813797AD520F 
sessions.created Sun Dec 11 09:42:38 IST 2005
sessions.lastaccessed Sun Dec 11 09:43:31 IST 2005 
  
Share: 



Milind Mishra
Milind Mishra author of Program using session tracking capability of Servlets, which displays information of current user session by HttpServletRequest object is from India.
 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in OOAD:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!