banner



How To Create Session Object In Servlet

  • Updated date Sep 12, 2019
  • 10.5k
  • 4

In this article we will know how to use session servlet in Java.

Introduction

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

1. Public void setAttribute(string variable name,object value)
This creates a variable within the user's session

2. 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.

3. Public void invalidate();
It destroys all the variables present in the user's session

4. 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

  1. import  javax.servlet.*;
  2. import  javax.servlet.http.*;
  3. import  java.io.*;
  4. public class  summation extends  HttpServlet
  5. {
  6. public void  doGet(HttpServletRequest req, HttpServletResponse res) throws  IOException, ServletException
  7.  {
  8.   PrintWriter out = res.getWriter();
  9.   out.println("<html><body bgcolor='yellow'>" );
  10.   String s1 = req.getParameter("t1" );
  11.   out.println("<form >" );
  12.   out.println("<h1>Number<input type='text' name='t1'></h1>" );
  13.   out.println("<input type='submit' value='summation'>" );
  14.   out.println("</form>" );
  15. int  result = 0 ;
  16.   HttpSession ses = req.getSession();
  17.   Integer s2 = (Integer) ses.getAttribute("sum" );
  18. if  (s1 != null )
  19.   {
  20. if  (s2 == null )
  21.     result = Integer.parseInt(s1);
  22. else
  23.     result = Integer.parseInt(s1) + s2;
  24.    ses.setAttribute("sum" , result);
  25.    out.println("<h1>Summation is :"  + result + "</h1>" );
  26.   }
  27.   out.println("</body></html>" );
  28.  }
  29. }

web.xml settings

  1. <? xml version = "1.0" encoding = "ISO-8859-1" ?>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. < web-app xmlns = "http://java.sun.com/xml/ns/javaee"
  17. xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  18. xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  19. version = "2.5" >
  20. < servlet >
  21. < servlet-name > summation </ servlet-name >
  22. < servlet-class > summation </ servlet-class >
  23. </ servlet >
  24. < servlet-mapping >
  25. < servlet-name > summation </ servlet-name >
  26. < url-pattern > /summation </ url-pattern >
  27. </ servlet-mapping >
  28. </ 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 the 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

How To Create Session Object In Servlet

Source: https://www.c-sharpcorner.com/uploadfile/satyapriyanayak/sesion-servlet-in-java/

Posted by: simmonswrin1968.blogspot.com

0 Response to "How To Create Session Object In Servlet"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel