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

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