Pin It

How to do Session Management in JSP and Servlets using its methods and API ?.


A simple web application showing how to manage sessions through its methods in JSP and Servlets using Session Management API

1. style.css (not mandatory) just for look and feel


body {
 font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
 background-color: #FDF4DF;
 font-size: 14px;
 color: #FFFFFF;
}

td {
 font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
 font-size: 14px;
}

th {
 font-family: Verdana, Geneva, Arial, helvetica, sans-serif;
 font-size: 14px;
}

.black {
 color: #000000;
 font-weight: bold;
}



2. register.jsp



1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Registration</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>

 <form method="post" action="SessionManagement">
  <p>&nbsp;</p>
  <p align="center" class="black">Create a new Account :</p>
  <table align="center" bgcolor="#728DCF">
   <tr>
    <td>First Name</td>
    <td><input type="text" name=firstName size=30 class="smalltext"></td>
   </tr>
   <tr>
    <td>Last Name</td>
    <td><input type="text" name=lastName size=30 class="smalltext"></td>
   </tr>

   <tr>
    <td>Email Address</td>
    <td><input type="text" name=email size=40 class="smalltext"></td>
   </tr>

   <tr>
    <td>Password</td>
    <td><input type="password" name=password size=30
     class="smalltext"></td>
   </tr>
   <tr>
    <td colspan="2" align="center"><input type="submit"
     value="Register" /></td>
   </tr>
  </table>
 </form>

</body>
</html>


3. SessionManagement.java (Servlet)


import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class SessionManagement
 */
@WebServlet("/SessionManagement")
public class SessionManagement extends HttpServlet {

 @Override
 protected void doGet(
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {
  doPost(request, response);
 }

 @Override
 protected void doPost(
   HttpServletRequest request, 
   HttpServletResponse response
   ) throws ServletException, IOException {

  response.setContentType("text/html;charset=UTF-8");

  // Getting the first name of user from the request 

  String firstName = request.getParameter("firstName");

  // Creating PrintWriter from response for displaying
  // contents on the browser

  PrintWriter out = response.getWriter();

  // Creating a HttpSession Object for the request

  HttpSession newSession = request.getSession(true);

  // Setting the maximum inactive interval to be 1 min
  // after 1 min a new session will be created 
  newSession.setMaxInactiveInterval(60);

  String welcome;

  Integer counter = 0;

  try {

   // Checking whether session is new or not  

   if (newSession.isNew()) {

    // If user has visited this page for 1st time below msg is displayed
    welcome = "Hi "+ firstName +" !!!. It's first time you are visiting this website.";

   }

   else {
    // If user has already visited this page in session interval
    // than this msg is been displayed 
    welcome = "Hi "+ firstName +" !!!. Welcome back";

    // getting the value of counter from session for first
    // time it will be null 
    Integer count = (Integer)newSession.getAttribute("counter");

    if (count != null) {
     // incrementing the value of counter by one if its not null
     // that is user has visited the page for second time 
     counter = new Integer(count.intValue() + 1);

    }
   }
   // setting the value of counter into session after 1st visit as soon
   //  as soon as visitor comes back till session expiration this value will be displayed
   // with the help of session management ... After session expiration this value will be 
   // gone and reset to null
   newSession.setAttribute ("counter", counter);

   out.println("<html><head><title>Session Management</title></head><body bgcolor='#FDF4DF'>");

   out.println("<h3>" + welcome + "</h3><br>");
 
   // printing the session id coming in the request for the first time 
   out.println("<b>Session Id : </b>" + newSession.getId() + "<br>");
   
   // prints the creation time of servlet as a date 
   out.println("<b>Creation Time of the Session : </b>" + 
     new Date(newSession.getCreationTime()) + "<br>");
   
   // prints the last access of the page by the same user
   out.println("<b>Last access time : </b>" + 
     new Date(newSession.getLastAccessedTime()) + "<br><br>");
   
   // it displays the maximum inactive interval time
   out.println("<b>Maximum inactive interval time was : </b>" + 
     newSession.getMaxInactiveInterval() + "<br>");


   // printing the counter value
   out.println("You have visited this page " + (++counter));

   if(counter == 1) {

    out.println(" time ");

   }
   else {

    out.println(" times ");

   }

   out.println("</body></html>");

  } 
  finally {

   out.close();

  }
 }
}




Output of the program :


1. register.jsp screen view


















2. ServletManagement output screen view for first access 



















3. ServletManagement output screen view for second access within 1 min of first 


















4. ServletManagement output screen view access after 1 min of inactive time 
















Screen 2 and 4 are same but with different Session Id (Check) ....
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot