Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Session Last Accessed Time Example 
 

This example illustrates to find current access time of session and last access time of session.

 

Session Last Accessed Time Example

                         

This example illustrates to find current  access time of session  and last access time of session. Sessions are used to maintain state and user identity across multiple page requests. An implementation of HttpSession represents the server's view of the session. The server considers a session to be new until it has been joined by the client. Until the client joins the session, isNew() method returns true.

 

 

 

Here is the source code of LastAccessTime.java 

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.util.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LastAccessTime extends HttpServlet {
  public void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String head;
    Integer count = new Integer(0);
    if (session.isNew()) {
      head = "New Session Value ";
    else {
      head = "Old Session value";
      Integer oldcount =(Integer)session.getValue("count")
      if (oldcount != null) {
        count = new Integer(oldcount.intValue() 1);
      }
    }
    session.putValue("count", count)
    out.println("<HTML><BODY BGCOLOR=\"#FDF5E6\">\n" +
      "<H2 ALIGN=\"CENTER\">" + head + "</H2>\n" +
      "<H4 ALIGN=\"CENTER\">Session Access Time:</H4>\n" +
      "<TABLE BORDER=1 ALIGN=CENTER>\n" "<TR BGCOLOR=\"pink\">\n" +
      "  <TD>Session Creation Time\n" +"  <TD>" +
      new Date(session.getCreationTime()) "\n" +
      "<TR BGCOLOR=\"pink\">\n" +"  <TD>Last Session Access Time\n" +
      "  <TD>" new Date(session.getLastAccessedTime()) +
      "</TABLE>\n" +"</BODY></HTML>");
  }
  public void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
  }
}

Description of code: In  the above servlet,  isNew() method is used to find whether session is new or old. The getCreationTime() method is used to find  the time when session was created. The getLastAccessedTime() method is used to find when last time session was accessed by the user.

Here is the mapping of servlet ("LastAccessTime.java") in the web.xml file:

<servlet>
    <servlet-name>LastAccessTime</servlet-name>
    <servlet-class>LastAccessTime</servlet-class>
</servlet> 
<servlet-mapping>
    <servlet-name>LastAccessTime</servlet-name>
    <url-pattern>/LastAccessTime</url-pattern>
</servlet-mapping>

Running the servlet by this url: http://localhost:8080/CodingDiaryExample/LastAccessTime displays the output like below:

When user re-calls the servlet the creation time will be same but last accessed time will be changed as shown in the following figure: 

Download Source Code

                         

» View all related tutorials
Related Tags: c ide orm table data form io make type column field name columns change this id nsis war tab row

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.