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



