.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Session in servlet

Posted By:satyapriyanayak       Posted Date: May 30, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

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

Session is a period of users 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 as session id. The session of the user remains active till 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 as default inactive interval. The server can change the period for default inactive interval. If the user closes the browser then also the session gets destroyed. The server can destroy session of the user explicitly. Servlet can use session of user to create some variables with value. 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 create a variable with in the users session

Public object getAttribute(string variable name)
This returns value of the specified session variable. If the specified variable dose not exits 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 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)

 

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

 

 


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend