Reading:  

Introduction to Servlet technology


Working with session

Servlets - Session Tracking

 

HTTP is a "stateless" protocol meaning when client retrieves a Web page, client opens a new separate connection to Web server and it does not keep any record of previous client request.

The HttpSession Object

The servlet container have interface for creating session between HTTP client and HTTP server. The session persists for specified time period across more than one connection or page request from user.

We can access HttpSession object by calling public method getSession() of HttpServletRequest as below:

HttpSession session = request.getSession();

Below are the few methods used for accessing the session objects :

Method & Description

public Object getAttribute(String name)


The method returns object bound with specified name in session

public Enumeration getAttributeNames()


The method returns an Enumeration of String objects containing names of all objects bound to session.

public long getCreationTime()


The method returns time when session was created, measured in milliseconds

public String getId()


The method returns a string containing unique identifier assigned to session.

public long getLastAccessedTime()


The method returns last time client sent a request associated with session.

public int getMaxInactiveInterval()


The method returns maximum time interval, in seconds

public void invalidate()


The method invalidates session and unbinds any objects.

public boolean isNew()


The method returns true if client does not know about session

public void removeAttribute(String name)


The method removes object bound with specified name from session.

public void setAttribute(String name, Object value) 


The method binds an object to session, specified name used.

public void setMaxInactiveInterval(int interval)


The method specifies time, in seconds, between client requests before servlet container.

public Object getAttribute(String name)


The method returns object bound with specified name of session

public Enumeration getAttributeNames()


The method return an Enumeration of String objects containing names of all objects bound to session.

public long getCreationTime()


The method returns time when session was created.

public String getId()


The method returns string containing unique identifier assigned to session.

public long getLastAccessedTime()


The method returns last time client sent a request associated with  session

public int getMaxInactiveInterval()


The method returns maximum time interval, in seconds

Below is the example of managing session in servlets.

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

public class SessionExample extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(true);

        // print session info

        Date created = new Date(session.getCreationTime());
        Date accessed = new Date(session.getLastAccessedTime());
        out.println("ID " + session.getId());
        out.println("Created: " + created);
        out.println("Last Accessed: " + accessed);
    }
}
 

Here is what we get:

Create session servlets

 

Deleting Session Data

The Session data can be removed as shown below:

  • Remove particular attribute: public void removeAttribute(String name) method used to remove session.
  • Delete whole session: public void invalidate() method can be used to delete entire session.
  • Setting Session timeout: public void setMaxInactiveInterval(int interval) method will set timeout to session individually.
  • web.xml Configuration: session time out can be set in web.xml as follows.
      <session-config>
        <session-timeout>15</session-timeout>
      </session-config>



In the next part of this quick introduction guide we will explore how to connect to a database.

 

Description

This guide introduces Servlet technology and we will cover below topics

  1. What is a Servlet?
  2. Initial Setup
  3. Life Cycle
  4. Various Examples
  5. Client Request
  6. Server Response
  7. Http Status Codes
  8. Writing Filters
  9. Exception Handling
  10. Cookies Handling
  11. Session Tracking
  12. Database Access
  13. Packaging
  14. Internationalization

This is to the point introduction to the topic to get you started

 



Prerequisites

Working knowledge of Core Java is essential

Audience

Beginner to Java Servlet technology or students looking to brush up their skills quickly

Learning Objectives

Learn Java Servlets

Author: Subject Coach
Added on: 2nd May 2015

You must be logged in as Student to ask a Question.

None just yet!