• HttpSession is an interface available in the javax.servlet.http package.
• The subclass for the HttpSession interface is implemented by the container vendor.
• You can get the HttpSession object with the following methods of HttpServletRequest:
o HttpSession getSession()
o HttpSession getSession(boolean)
• You can store and access the client-specific data in the HttpSession object as an attribute with the following methods:
o Void setAttribute(String aName,Object val)
o Object getAttribute(String aName)
o Void removeAttribute(String aName)
o Enumeration getAttributeNames()
• You can also use the following methods to store the user data:
o Void putValue(String, Object)
o Object getValue(String)
o Void removeValue(String)
o String[] getValueNames()
• Note: These four methods are dependent on each other.
Method | Description |
---|---|
Public String getId() | Return the session ID for the client. |
Public long getCreationTime() | Return the session creation time in millisecond |
Public long getLastAccessedTime() | Return the time in milliseconds when the session accessed last time |
Public int getMaxInactiveInterval() | Return the time interval.If sessions will not be used within that interval,then the session will be destroyed. |
Public ServletContext getServletContext() | Return ServletContext object. |
Public void invalidate() | Destroyed the session immediately. |
Public boolean isNew() | Checks whether the session object is newly created object or existing object |
Public void setMaxInactiveInterval(int sec) | Set the session's inactive interval. |
File Required For Program
1. Index.jsp
2. Web.xml
3. TestServlet.java
1. Index.jsp
<%@page session="false" %>
<%@ page language="java" contentType="text/html;charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>
<br>Id:<%=session.getId()%>
<br>Id:<%=session.isNew()%>
<a href="test.jtc">TEST</a>
</h1>
</body>
</html>
2. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/java
ee http://java.sun.com/xml/ns/javaee/webapp_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Jtc_20</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>TestServlet</servlet-name>
<servletclass>com.jtcindia.servlet.TestServlet</servletclass>
</servlet>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/test.jtc</url-pattern>
</servlet-mapping>
</web-app>
3. TestServlet.java
package com.jtcindia.servlet;
import java.io.IOException;
import java.io.Writer;
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 TestServlet extends HttpServlet {
@Override
protected void
service(HttpServletRequest req,
HttpServletResponse res)
throws
ServletException, IOException {
HttpSession
session = req.getSession();
Writer out = res.getWriter();
out.write("<h1><br>ID:" + session.getId());
out.write("<br>isNew:" + session.isNew());
//true Session is created and defined name identify or not
}
}
• To explicitly destroy a session object, use the following method with it.
o Sess.invalidate();
• You can use the following method with session objects to destroy them automatically when they are not being used by clients for the specified interval.
o Sess.setMaxInactiveInterval(int intervalInSecond)
• You can use the following in the web.xml file to destroy the session object automatically when it is not being used by the client for the specified interval:
<session-config> <session-timeout>timeInMinut</session-timeout> </session-config>