-
What is a Session?
HTTP protocol and Web Servers are stateless, what it means is that for web server every request is a new request to process and they can’t identify if it’s coming from client that has been sending request previously.
But sometimes in web applications, we should know who the client is and process the request accordingly. For example, a shopping cart application should know who is sending the request to add an item and in which cart the item has to be added or who is sending checkout request so that it can charge the amount to correct client.
Session is a conversional state between client and server and it can consists of multiple request and response between client and server. Since HTTP and Web Server both are stateless, the only way to maintain a session is when some unique information about the session (session id) is passed between server and client in every request and response.
There are several ways through which we can provide unique identifier in request and response.
- User Authentication – This is the very common way where we user can provide authentication credentials from the login page and then we can pass the authentication information between server and client to maintain the session. This is not very effective method because it wont work if the same user is logged in from different browsers.
- HTML Hidden Field – We can create a unique hidden field in the HTML and when user starts navigating, we can set its value unique to the user and keep track of the session. This method can’t be used with links because it needs the form to be submitted every time request is made from client to server with the hidden field. Also it’s not secure because we can get the hidden field value from the HTML source and use it to hack the session.
- URL Rewriting – We can append a session identifier parameter with every request and response to keep track of the session. This is very tedious because we need to keep track of this parameter in every response and make sure it’s not clashing with other parameters.
- Cookies – Cookies are small piece of information that is sent by web server in response header and gets stored in the browser cookies. When client make further request, it adds the cookie to the request header and we can utilize it to keep track of the session. We can maintain a session with cookies but if the client disables the cookies, then it won’t work.
- Session Management API – Session Management API is
built on top of above methods for session tracking. Some of the major
disadvantages of all the above methods are:
- Most of the time we don’t want to only track the session, we have to store some data into the session that we can use in future requests. This will require a lot of effort if we try to implement this.
- All the above methods are not complete in themselves, all of them won’t work in a particular scenario. So we need a solution that can utilize these methods of session tracking to provide session management in all cases.
-
Session Management using Cookies
Cookies are used a lot in web applications to personalize response based on your choice or to keep track of session. Before moving forward to the Servlet Session Management API, I would like to show how can we keep track of session with cookies through a small web application.
We will create a dynamic web application ServletCookieExample with project structure like below image.
Deployment descriptor of the web application is:
Welcome page of our application is login.html where we will get authentication details from user.web.xml 1234567<?xmlversion="1.0"encoding="UTF-8"?><web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"id="WebApp_ID"version="3.0"><display-name>ServletCookieExample</display-name><welcome-file-list><welcome-file>login.html</welcome-file></welcome-file-list></web-app>
Here is the LoginServlet that takes care of the login request.login.html 123456789101112131415161718<!DOCTYPE html><html><head><metacharset="US-ASCII"><title>Login Page</title></head><body><formaction="LoginServlet"method="post">Username: <inputtype="text"name="user"><br>Password: <inputtype="password"name="pwd"><br><inputtype="submit"value="Login"></form></body></html>
Notice the cookie that we are setting to the response and then forwarding it to LoginSuccess.jsp, this cookie will be used there to track the session. Also notice that cookie timeout is set to 30 minutes. Ideally there should be a complex logic to set the cookie value for session tracking so that it won’t collide with any other request.LoginServlet.java 123456789101112131415161718192021222324252627282930313233343536373839404142434445packagecom.journaldev.servlet.session;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.RequestDispatcher;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/*** Servlet implementation class LoginServlet*/@WebServlet("/LoginServlet")publicclassLoginServletextendsHttpServlet {privatestaticfinallongserialVersionUID = 1L;privatefinalString userID ="Pankaj";privatefinalString password ="journaldev";protectedvoiddoPost(HttpServletRequest request,HttpServletResponse response)throwsServletException, IOException {// get request parameters for userID and passwordString user = request.getParameter("user");String pwd = request.getParameter("pwd");if(userID.equals(user) && password.equals(pwd)){Cookie loginCookie =newCookie("user",user);//setting cookie to expiry in 30 minsloginCookie.setMaxAge(30*60);response.addCookie(loginCookie);response.sendRedirect("LoginSuccess.jsp");}else{RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");PrintWriter out= response.getWriter();out.println("<font color=red>Either user name or password is wrong.</font>");rd.include(request, response);}}}
Notice that if we try to access the JSP directly, it will forward us to the login page. When we will click on Logout button, we should make sure that cookie is removed from client browser.LoginSuccess.jsp 1234567891011121314151617181920212223242526<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=US-ASCII"><title>Login Success Page</title></head><body><%String userName = null;Cookie[] cookies = request.getCookies();if(cookies !=null){for(Cookie cookie : cookies){if(cookie.getName().equals("user")) userName = cookie.getValue();}}if(userName == null) response.sendRedirect("login.html");%><h3>Hi <%=userName %>, Login successful.</h3><br><formaction="LogoutServlet"method="post"><inputtype="submit"value="Logout"></form></body></html>
There is no method to remove the cookie but we can set the maximum age to 0 so that it will be deleted from client browser immediately.LogoutServlet.java 123456789101112131415161718192021222324252627282930313233343536373839packagecom.journaldev.servlet.session;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/*** Servlet implementation class LogoutServlet*/@WebServlet("/LogoutServlet")publicclassLogoutServletextendsHttpServlet {privatestaticfinallongserialVersionUID = 1L;protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {response.setContentType("text/html");Cookie loginCookie =null;Cookie[] cookies = request.getCookies();if(cookies !=null){for(Cookie cookie : cookies){if(cookie.getName().equals("user")){loginCookie = cookie;break;}}}if(loginCookie !=null){loginCookie.setMaxAge(0);response.addCookie(loginCookie);}response.sendRedirect("login.html");}}
When we run above application, we get response like below images.
-
Session Management with HttpSession
Servlet API provides Session management throughHttpSessioninterface. We can get session from HttpServletRequest object using following methods. HttpSession allows us to set objects as attributes that can be retrieved in future requests.
- HttpSession getSession() – This method always returns a HttpSession object. It returns the session object attached with the request, if the request has no session attached, then it creates a new session and return it.
- HttpSession getSession(boolean flag) – This method returns HttpSession object if request has session else it returns null.
- String getId() – Returns a string containing the unique identifier assigned to this session.
- Object getAttribute(String name) – Returns the
object bound with the specified name in this session, or null if no
object is bound under the name. Some other methods to work with Session
attributes are
getAttributeNames(),removeAttribute(String name)andsetAttribute(String name, Object value). - long getCreationTime() – Returns the time when this
session was created, measured in milliseconds since midnight January 1,
1970 GMT. We can get last accessed time with
getLastAccessedTime()method. - setMaxInactiveInterval(int interval) – Specifies the time, in
seconds, between client requests before the servlet container will
invalidate this session. We can get session timeout value from
getMaxInactiveInterval()method. - ServletContext getServletContext() – Returns ServletContext object for the application.
- boolean isNew() – Returns true if the client does not yet know about the session or if the client chooses not to join the session.
- void invalidate() – Invalidates this session then unbinds any objects bound to it.
Understanding JSESSIONID Cookie
When we use HttpServletRequest getSession() method and it creates a new request, it creates the new HttpSession object and also add a Cookie to the response object with name JSESSIONID and value as session id. This cookie is used to identify the HttpSession object in further requests from client. If the cookies are disabled at client side and we are using URL rewriting then this method uses the jsessionid value from the request URL to find the corresponding session. JSESSIONID cookie is used for session tracking, so we should not use it for our application purposes to avoid any session related issues.
Let’s see example of session management using HttpSession object. We will create a dynamic web project in Eclipse with servlet context as ServletHttpSessionExample. The project structure will look like below image.
login.html is same like earlier example and defined as welcome page for the application in web.xml
LoginServlet servlet will create the session and set attributes that we can use in other resources or in future requests.
Our LoginSuccess.jsp code is given below.LoginServlet.java 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849packagecom.journaldev.servlet.session;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.RequestDispatcher;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/*** Servlet implementation class LoginServlet*/@WebServlet("/LoginServlet")publicclassLoginServletextendsHttpServlet {privatestaticfinallongserialVersionUID = 1L;privatefinalString userID ="admin";privatefinalString password ="password";protectedvoiddoPost(HttpServletRequest request,HttpServletResponse response)throwsServletException, IOException {// get request parameters for userID and passwordString user = request.getParameter("user");String pwd = request.getParameter("pwd");if(userID.equals(user) && password.equals(pwd)){HttpSession session = request.getSession();session.setAttribute("user","Pankaj");//setting session to expiry in 30 minssession.setMaxInactiveInterval(30*60);Cookie userName =newCookie("user", user);userName.setMaxAge(30*60);response.addCookie(userName);response.sendRedirect("LoginSuccess.jsp");}else{RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");PrintWriter out= response.getWriter();out.println("<font color=red>Either user name or password is wrong.</font>");rd.include(request, response);}}}
When a JSP resource is used, container automatically creates a session for it, so we can’t check if session is null to make sure if user has come through login page, so we are using session attribute to validate request.LoginSuccess.jsp 1234567891011121314151617181920212223242526272829303132333435<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=US-ASCII"><title>Login Success Page</title></head><body><%//allow access only if session existsString user = null;if(session.getAttribute("user") == null){response.sendRedirect("login.html");}else user = (String) session.getAttribute("user");String userName = null;String sessionID = null;Cookie[] cookies = request.getCookies();if(cookies !=null){for(Cookie cookie : cookies){if(cookie.getName().equals("user")) userName = cookie.getValue();if(cookie.getName().equals("JSESSIONID")) sessionID = cookie.getValue();}}%><h3>Hi <%=userName %>, Login successful. Your Session ID=<%=sessionID %></h3><br>User=<%=user %><br><ahref="CheckoutPage.jsp">Checkout Page</a><formaction="LogoutServlet"method="post"><inputtype="submit"value="Logout"></form></body></html>
CheckoutPage.jsp is another page and it’s code is given below.
Our LogoutServlet code is given below.CheckoutPage.jsp 123456789101112131415161718192021222324252627282930<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=US-ASCII"><title>Login Success Page</title></head><body><%//allow access only if session existsif(session.getAttribute("user") == null){response.sendRedirect("login.html");}String userName = null;String sessionID = null;Cookie[] cookies = request.getCookies();if(cookies !=null){for(Cookie cookie : cookies){if(cookie.getName().equals("user")) userName = cookie.getValue();}}%><h3>Hi <%=userName %>, do the checkout.</h3><br><formaction="LogoutServlet"method="post"><inputtype="submit"value="Logout"></form></body></html>
Notice that I am printing JSESSIONID cookie value in logs, you can check server log where it will be printing the same value as Session Id in LoginSuccess.jspLogoutServlet.java 12345678910111213141516171819202122232425262728293031323334353637383940packagecom.journaldev.servlet.session;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/*** Servlet implementation class LogoutServlet*/@WebServlet("/LogoutServlet")publicclassLogoutServletextendsHttpServlet {privatestaticfinallongserialVersionUID = 1L;protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {response.setContentType("text/html");Cookie[] cookies = request.getCookies();if(cookies !=null){for(Cookie cookie : cookies){if(cookie.getName().equals("JSESSIONID")){System.out.println("JSESSIONID="+cookie.getValue());break;}}}//invalidate the session if existsHttpSession session = request.getSession(false);System.out.println("User="+session.getAttribute("user"));if(session !=null){session.invalidate();}response.sendRedirect("login.html");}}
Below images shows the execution of our web application.
-
URL Rewriting
As we saw in last section that we can manage a session with HttpSession but if we disable the cookies in browser, it won’t work because server will not receive the JSESSIONID cookie from client. Servlet API provides support for URL rewriting that we can use to manage session in this case.
The best part is that from coding point of view, it’s very easy to use and involves one step – encoding the URL. Another good thing with Servlet URL Encoding is that it’s a fallback approach and it kicks in only if browser cookies are disabled.
We can encode URL with HttpServletResponseencodeURL()method and if we have to redirect the request to another resource and we want to provide session information, we can useencodeRedirectURL()method.
We will create a similar project like above except that we will use URL rewriting methods to make sure session management works fine even if cookies are disabled in browser.
ServletSessionURLRewriting project structure in eclipse looks like below image.
LoginServlet.java 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950packagecom.journaldev.servlet.session;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.RequestDispatcher;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/*** Servlet implementation class LoginServlet*/@WebServlet("/LoginServlet")publicclassLoginServletextendsHttpServlet {privatestaticfinallongserialVersionUID = 1L;privatefinalString userID ="admin";privatefinalString password ="password";protectedvoiddoPost(HttpServletRequest request,HttpServletResponse response)throwsServletException, IOException {// get request parameters for userID and passwordString user = request.getParameter("user");String pwd = request.getParameter("pwd");if(userID.equals(user) && password.equals(pwd)){HttpSession session = request.getSession();session.setAttribute("user","Pankaj");//setting session to expiry in 30 minssession.setMaxInactiveInterval(30*60);Cookie userName =newCookie("user", user);response.addCookie(userName);//Get the encoded URL stringString encodedURL = response.encodeRedirectURL("LoginSuccess.jsp");response.sendRedirect(encodedURL);}else{RequestDispatcher rd = getServletContext().getRequestDispatcher("/login.html");PrintWriter out= response.getWriter();out.println("<font color=red>Either user name or password is wrong.</font>");rd.include(request, response);}}}LoginSuccess.jsp 1234567891011121314151617181920212223242526272829303132333435363738<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=US-ASCII"><title>Login Success Page</title></head><body><%//allow access only if session existsString user = null;if(session.getAttribute("user") == null){response.sendRedirect("login.html");}else user = (String) session.getAttribute("user");String userName = null;String sessionID = null;Cookie[] cookies = request.getCookies();if(cookies !=null){for(Cookie cookie : cookies){if(cookie.getName().equals("user")) userName = cookie.getValue();if(cookie.getName().equals("JSESSIONID")) sessionID = cookie.getValue();}}else{sessionID = session.getId();}%><h3>Hi <%=userName %>, Login successful. Your Session ID=<%=sessionID %></h3><br>User=<%=user %><br><!-- need to encode all the URLs where we want session information to be passed --><ahref="<%=response.encodeURL("CheckoutPage.jsp") %>">Checkout Page</a><formaction="<%=response.encodeURL("LogoutServlet") %>" method="post"><inputtype="submit"value="Logout"></form></body></html>CheckoutPage.jsp 123456789101112131415161718192021222324252627282930<%@ page language="java" contentType="text/html; charset=US-ASCII"pageEncoding="US-ASCII"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><metahttp-equiv="Content-Type"content="text/html; charset=US-ASCII"><title>Login Success Page</title></head><body><%String userName = null;//allow access only if session existsif(session.getAttribute("user") == null){response.sendRedirect("login.html");}else userName = (String) session.getAttribute("user");String sessionID = null;Cookie[] cookies = request.getCookies();if(cookies !=null){for(Cookie cookie : cookies){if(cookie.getName().equals("user")) userName = cookie.getValue();}}%><h3>Hi <%=userName %>, do the checkout.</h3><br><formaction="<%=response.encodeURL("LogoutServlet") %>" method="post"><inputtype="submit"value="Logout"></form></body></html>When we run this project keeping cookies disabled in the browser, below images shows the response pages, notice the jsessionid in URL of browser address bar. Also notice that on LoginSuccess page, user name is null because browser is not sending the cookie send in the last response.LogoutServlet.java 123456789101112131415161718192021222324252627282930313233343536373839404142packagecom.journaldev.servlet.session;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;/*** Servlet implementation class LogoutServlet*/@WebServlet("/LogoutServlet")publicclassLogoutServletextendsHttpServlet {privatestaticfinallongserialVersionUID = 1L;protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {response.setContentType("text/html");Cookie[] cookies = request.getCookies();if(cookies !=null){for(Cookie cookie : cookies){if(cookie.getName().equals("JSESSIONID")){System.out.println("JSESSIONID="+cookie.getValue());}cookie.setMaxAge(0);response.addCookie(cookie);}}//invalidate the session if existsHttpSession session = request.getSession(false);System.out.println("User="+session.getAttribute("user"));if(session !=null){session.invalidate();}//no encoding because we have invalidated the sessionresponse.sendRedirect("login.html");}}
If cookies are not disabled, you won’t see jsessionid in the URL because Servlet Session API will use cookies in that case.
Learn ALL ABOUT JAVA
Learn java in simple and easy language . Java can be learn by anyone. information all about java,java web services how to use it,java database connectivity(JDBC),servlet,tomcat
Friday, 13 December 2013
Java Servlet Session Management Tutorial with Examples of Cookies, HttpSession and URL Rewriting
Subscribe to:
Posts (Atom)