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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Display session value Using Servlet 
 

Sometime while developing web application it is necessary to interact with the different values of the Session object.

 

Display session value Using Servlet

                         

Sometime while developing web application it is necessary to interact with the different values of the Session object. In this example we will explore the different values of the Session object and then learn how to use it in our programming code.

You will learn how to find all the session related information like:

  • getId. This method is used to find the identifier of the session which is unique.
  • isNew. This method is used when find, whether session is newly created or preexisted. If session has never seen by user then this method return "true" but if session is preexisted then it return "false".
  • getCreationTime. This method is used to find the creation time of session. To use of this method we can find the following details about session i.e. day, month, date, time, GMT(Greenwich Mean Time) and year will be displayed.
  • getLastAccessedTime. This method is used to find the last accessed time of session. It returns the time, in milliseconds.
  • getMaxInactiveInterval. This method returns the total time, in seconds, during which session remains active if user does not accesses the session for this maximum time interval. After this time the session will be invalidated automatically. A negative value indicates that the session should never timeout.

Here is the sample code for HttpSessionDisplay.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 HttpSessionDisplay extends HttpServlet {
  String head;
  public void doGet(HttpServletRequest request,HttpServletResponse response)
    throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    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=\"pink\">\n" +
      "<H2 ALIGN=\"CENTER\">" + head + "</H2>\n" +  
      "<H3 ALIGN=\"CENTER\">Description about Session:</H3>\n" +
      "<TABLE BORDER=1 ALIGN=CENTER>\n" "<TR BGCOLOR=\"voilet\">\n" +
      "  <TH>Information Type<TH>Session Value\n" +"<TR>\n" +"  <TD>ID\n" +
      "<TD>" + session.getId() "\n" +
      "<TR>\n" +  " <TD>Session Creation Time\n" +
      " <TD>" new Date(session.getCreationTime()) "\n" +
      "<TR>\n" +"  <TD>Last Session Access Time\n" +"  <TD>" +
      new Date(session.getLastAccessedTime()) "\n" +
      "<TR>\n" +"  <TD>Number of Previous Session Accesses\n" +
      "<TD>" + count + "\n" +
      "</TABLE>\n" +"</BODY></HTML>");
  }
  public void doPost(HttpServletRequest request,HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
  }
}

Here is the mapping of class file in web.xml.

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

Run this example by this url: http://localhost:8080/CodingDiaryExample/HttpSessionDisplay

Output:

In case of first time accessing of servlet the following session value will be displayed

In case of preexist session if user accessed the servlet  then the following session value will be displayed.

Download Source Code

                         

» View all related tutorials
Related Tags: java c database web jsp file ide browser data redirect servlet view user link page vi new this id create

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
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
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.