Sesion Servlet in Java


A Session is a period of a user's interaction with the server. Whenever a user accesses any page of the server, then the server creates session for the user. The server provides a unique id to each session called session id. The session of the user remains active while the user keeps on interacting with the server. If the user does not interact with the server for a period of 30 min then the session gets destroyed. This period is called the default inactive interval. The server can change the period for the default inactive interval. If the user closes the browser then the session also gets destroyed. The server can destroy the session of the user explicitly. A servlet can use the session of user to create some variables. These variables occupy server memory. Users cannot deny creation of session variables. One servlet can create session variables and other servlets can fetch or change the value of session variables.

Creation and reading process of session

  1. Servlet must be a sub class of HttpServlet.
  2. Create the reference of javax.servlet.http.httpsession by using getSession() method of HttpservletRequest
  3. Use set attribute method of Httpsession to create session variables. Use getAttribute() of Httpsession to find value of session variables.

Method of Http session

Public void setAttribute(string variable name,object value)

This creates a variable within the users session

Public object getAttribute(string variable name)

This returns the value of the specified session variable. If the specified variable dose not exist in the user session then it returns null.

Public void invalidate();

It destroys all the variables present in users session

Public void setMaxInactiveInterval(long seconds)

It changes the default period of the Inactive interval.

Example: - This servlet will accept a set of numbers one by one infinitely and display the summation result below to the form after each submission of the number

Summation.java file

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

public class summation extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws IOException,ServletException
{
PrintWriter out=res.getWriter();
out.println("<html><body bgcolor='yellow'>");
String s1=req.getParameter("t1");
out.println("<form >");
out.println("<h1>Number<input type='text' name='t1'></h1>");
out.println("<input type='submit' value='summation'>");
out.println("</form>");

int result=0;
HttpSession ses=req.getSession();
Integer s2=(Integer)ses.getAttribute("sum");
if(s1 != null)
{
if(s2 ==null)
result=Integer.parseInt(s1);
else
result=Integer.parseInt(s1)+s2;
ses.setAttribute("sum",result);
out.println("<h1>Summation is :"+result+"</h1>");
}
out.println("</body></html>");
}

}

web.xml settings

<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<servlet>
<servlet-name>summation</servlet-name>
<servlet-class>summation</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>summation</servlet-name>
<url-pattern>/summation</url-pattern>
</servlet-mapping>

</web-app>

Compile

javac -cp servlet-api.jar summation.java (for tomcat 6.0)

SsnSerlt1.gif

Output

Run the tomcat then write the below line in the URL

Here test is the Context path, which we mentioned in the server.xml file, which is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf) directory.

http://localhost:8081/test/summation

SsnSerlt2.gif

SsnSerlt3.gif

Thanks for reading

Up Next
    Ebook Download
    View all
    Learn
    View all