HttpSession Interface in Servlets

The HttpSession object is utilized for session management. A session contains data particular to a specific client over the entire application. At the point when a client goes into a site (or an online application) surprisingly, the client is given an exceptional ID to recognize his session by. This one of a unique ID can be put away into a cookie or in a solicitation parameter.

HttpSession can be used to perform two tasks:

  1. bind objects
  2. view and manipulate information about a session, such as the session identifier, creation time, and last accessed time.
crc

How to get the HttpSession object?

The HttpServletRequest interface provides two methods to get the object of HttpSession:

  • public HttpSession getSession():Returns the present session connected with this solicitation, or if the solicitation does not have a session, makes one.
  • public HttpSession getSession(boolean create):Returns the present HttpSession connected with this solicitation or, if there is no present session and make is genuine, gives back another session.

image

Methods Description
long getCreationTime() returns the time when the session was created, measured in milliseconds since midnight January 1, 1970 GMT.
String getId() returns a string containing the unique identifier assigned to the session.
long getLastAccessedTime()/td> returns the last time the client sent a request associated with the session
int getMaxInactiveInterval() returns the maximum time interval, in seconds.
void invalidate() destroy the session
boolean isNew() returns true if the session is new else false
void setMaxInactiveInterval(int interval) Specifies the time, in seconds,after servlet container will invalidate the session.

Complete Example usage of HttpSession

index.html

<form action="servlet1">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>

Validate.java

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

public class Validate extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        
        String name = request.getParameter("user");
        String pass = request.getParameter("pass");
        
        if(pass.equals("123"))
        {
            //creating a session
            HttpSession session = request.getSession();
            session.setAttribute("user", name);
            response.sendRedirect("Welcome");
        }
    }
}

Web.xml

		<web-app>

<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>

</web-app>

Welcome.java


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

public class Welcome extends HttpServlet {
 
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String user = (String)session.getAttribute("user");
        out.println("Hello "+user);
    }
}