1. Create Login.html
  2. Create SessionServlet1 class
  3. Create SessionServlet2 class
  4. Configure SessionServlet1 & SessionServlet2 in web.xml
  5. Deploy & Run the application.

Create Login.html

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Form</title>
</head>
<body>
	<form action="/SessionDemo/test1">
		<h2>Enter Session Information</h2>
		Name:<input type="text" name="uname"><br> Value:<input
			type="text" name="uvalue"><br> <input type="submit">
	</form>
	<a href="/SessionDemo/test2">Session Information</a>
</body>
</html>

Create SessionServlet1 class

package com.java2learn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionServlet1 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		if (session.isNew()) {
			out.println("<h2>A New Session got created with session id:"
					+ session.getId() + "</h2>");

		} else {
			out.println("<h2>Existing session only using with session ID:"
					+ session.getId() + "</h2>");
		}
		String name = request.getParameter("uname");
		String value = request.getParameter("uvalue");
		session.setAttribute(name, value);
		RequestDispatcher rd = request.getRequestDispatcher("Login.html");
		rd.include(request, response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

Create SessionServlet2 class

package com.java2learn.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class SessionServlet2 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession(false);
		if (session==null) {
			out.println("<h2>No session Information is available</h2>");

		} else {
			Enumeration e=session.getAttributeNames();
			while (e.hasMoreElements()) {
				String name = (String) e.nextElement();
				String value=(String)session.getAttribute(name);
				out.println(name+"-----"+value);
				
			}
			long l1=session.getCreationTime();
			long l2=session.getLastAccessedTime();
			long l3=session.getMaxInactiveInterval();
			out.println("<h3> Creation Time:"+new Date(l1)+"</h3>");
			out.println("<h3> Last Accessed Time:"+new Date(l2)+"</h3>");
			out.println("<h3> Max Inactive interval:"+l3+"</h3>");
		}//else
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

Configure SessionServlet1 & SessionServlet2 in web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>SessionDemo</display-name>
	
	<servlet>
		<description></description>
		<display-name>SessionServlet1</display-name>
		<servlet-name>SessionServlet1</servlet-name>
		<servlet-class>com.java2learn.servlet.SessionServlet1</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SessionServlet1</servlet-name>
		<url-pattern>/test1</url-pattern>
	</servlet-mapping>
	<servlet>
		<description></description>
		<display-name>SessionServlet2</display-name>
		<servlet-name>SessionServlet2</servlet-name>
		<servlet-class>com.java2learn.servlet.SessionServlet2</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SessionServlet2</servlet-name>
		<url-pattern>/test2</url-pattern>
	</servlet-mapping>
	<session-config>
		<session-timeout>2</session-timeout>
	</session-config>
</web-app>

Deploy & Run the application

Input
Enter inputs
Enterinputs
Click on Submit button
Output3
Click on sessionInformation link
clickonsessioninfo


How the session Id is exchanging between client and server

  1. When ever a browser sends a request,server will creates a session object and sends the corresponding session id as the part of response.For this web server uses “set-cookie” response header.
  2. Browser will retrieve that session Id and sends it to the server with every consecutive request.For this web browser always uses Cookie request header.

SessionId

Note:

  1. If the required session information is very less, then creating a separate session object and maintaining that object at server side is not recommended because it may effect performance of the server.
  2. To resolve this ,we should go for Cookie concept where the required session information is maintained at client side and server is no longer responsible to hold session information