

{"id":81356,"date":"2020-09-01T09:00:35","date_gmt":"2020-09-01T03:30:35","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=81356"},"modified":"2021-08-25T13:47:23","modified_gmt":"2021-08-25T08:17:23","slug":"jsp-session-tracking-techniques","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/","title":{"rendered":"JSP Session Tracking Techniques"},"content":{"rendered":"<p>This article discusses the JSP session tracking and its techniques in detail. It highlights the need for session tracking. It also covers the tracking techniques with its advantages and disadvantages. Additionally, it shows the implementation of session tracking and management with detailed examples.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81446\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg\" alt=\"JSP Session Tracking\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h2>Introduction to JSP Session Tracking<\/h2>\n<p>While using a web application, a client may trigger multiple requests, or a server might be handling multiple requests from different clients at a given point of time. At times like these, a need for Session tracking arises so that the server identifies a client. It is an efficient way to maintain the state of requests that originate from the same browser for a given period of time. In order to do so, a unique session id is assigned to a user for the given session.<\/p>\n<h2>Need for Session Tracking in JSP<\/h2>\n<p>HTTP is a stateless protocol that implies that whenever a client makes a request, then for each request a new connection to the web server is established. As new connections are made every time, it becomes difficult for servers to identify that requests are coming from the same user or not. In addition to this, the server doesn\u2019t keep the information of the previous request, if a new request is made.<\/p>\n<p>For example, a client is using a shopping web application; a client may keep adding items to the cart. A server should know that if it is the same client or not. In scenarios like these session tracking is required.<\/p>\n<p>To solve such problems, whenever a user introduces itself to the server, it should possess and provide a unique identifier. This will help to maintain a complete conversation between client and server. There are four methods available to maintain a session between server and client.<\/p>\n<h2>JSP Session Tracking Techniques<\/h2>\n<h3>1. Cookies<\/h3>\n<p>Cookie is useful for Session management. The server may store a session id on the client-side as a cookie. When the method getSession() is called then this session ID can be sent by the client to the server to identify it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Cookie myCookie = new Cookie(\u201cSessionID\u201d, \u201dSession Value\u201d);\r\nmyCookie.setMaxAge(60*60*24)\r\nResponse.addCookie(myCookie);\r\n<\/pre>\n<p><strong>Advantage:<\/strong> A user can be identified using this method.<br \/>\n<strong>Disadvantage:<\/strong> This method is not recommended for session tracking as it fails for the users that keep their cookies blocked as no information can be saved on the client&#8217;s side.<\/p>\n<h3>2. Hidden Form Fields<\/h3>\n<p>HTML forms may have hidden fields like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;input type=\u201dhidden\u201d name=\u201dsessionId\u201d value=\u201dunique value\u201d\/&gt;\r\n<\/pre>\n<p>They are similar to the field input of a form. The only difference between them is that when we submit a form, the values in the field will pass through the specified method but will not be displayed on the screen to the user. It\u2019s value can be retrieved through request.getParameter(sessionId).<\/p>\n<p><strong>Advantage:<\/strong> The sessionId remains hidden and can be passed through simple HTML pages.<br \/>\n<strong>Disadvantage:<\/strong> However, we will need a logic that will dynamically generate unique values. So, this way of session tracking is not suitable for dynamic pages. Also, form submission can\u2019t occur by clicking on a simple hypertext link. Thus this way is not much suitable.<\/p>\n<h3>3. URL Rewriting<\/h3>\n<p>We can rewrite the URL by appending the sessionId at the end of the link as:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">http:\/\/localhost:8080\/jsp\/home.jsp?sessionid=\u201dABC\u201d<\/pre>\n<p>This will help the server identify the user with the help of a unique identifier and match it with data stored from the session. This is also called handling the browsers.<\/p>\n<p>HttpServletResponse.encodeUrl will associate session id with URL.<br \/>\nHttpServletResponse.encodeRedirectUrl will associate session id with the redirected URL in case the user is sent to a redirected page.<\/p>\n<p>Servlet engine gets and recognizes the Session ID once the user will click on the rewritten link. With this session ID, the httpSession method will get the Session ID.<\/p>\n<p><strong>Advantage:<\/strong> This technique is independent of the browser. Unlike the approaches above, even if the user has disabled cookies, this method is useful. We also need not pass hidden values.<br \/>\n<strong>Disadvantage:<\/strong> The client needs to keep a track of this unique identifier until the conversation between client and server completes. Thus the client needs to regenerate the URL and append the sessionId with each request made in that session.<\/p>\n<h3>4. Session object<\/h3>\n<p>Apart from the above methods, there is a method that is most reliable to use. A session begins when the client opens the browser and sends the first request to the server. The server generates a unique identifier that gets attached to the session. This sessionId and value are sent by the server to the client, and client(browser) sends it back with every request and gets uniquely identified.<\/p>\n<p><strong>Advantage:<\/strong> This method is the most reliable as each user possesses their unique session id. The session id will help the user to access the session data stored at the server side.<br \/>\n<strong>Disadvantage:<\/strong> This method is the most reliable method thus it has no such disadvantages.<\/p>\n<p>A session is always created until it is set false in the page directive as:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;%@ page session = \u201ctrue|false\u201d %&gt;<\/pre>\n<p>New session gets created with each new request using httpSession.<\/p>\n<h4>4.1 How to get Session object<\/h4>\n<p>We can access it using the implicit object of HttpServletRequest request along with the getSession() API of the HttpSession interface.<\/p>\n<p>a) HttpSession session = request.getSession()<br \/>\nb) HttpSession session = request.getSession(Boolean)<\/p>\n<h4>4.2 Methods of session object<\/h4>\n<p>Since session object is available to JSP programmers, these methods can be used with this object:<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>S.No.<\/b><\/td>\n<td><b>Method<\/b><\/td>\n<td><b>Description<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">1<\/span><\/td>\n<td><b>public Object getAttribute(String name)<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will return the object bound to the session whose name is specified. It returns null if no object is found.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">2<\/span><\/td>\n<td><b>public Enumeration getAttributeNames()<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will return an enumeration of all the bounded objects with the session.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">3<\/span><\/td>\n<td><b>public long getCreationTime()<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will return the creation time of the session.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">4<\/span><\/td>\n<td><b>public String getId()<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will return the unique identifier of the session.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">5<\/span><\/td>\n<td><b>public long getLastAccessedTime()<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will return the last time of access that is the last time the client sent a request for the session.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">6<\/span><\/td>\n<td><b>public int getMaxInactiveInterval()<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will return the time interval for which session will remain open between the client accesses.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">7<\/span><\/td>\n<td><b>public void invalidate()<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will invalidate the session and will free the objects that are bound to it.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">8<\/span><\/td>\n<td><b>public boolean isNew()<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will return true if:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Client doesn\u2019t know about the session<\/span><\/li>\n<li style=\"font-weight: 400\"><span style=\"font-weight: 400\">Or doesn\u2019t want to join the session.<\/span><\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">9<\/span><\/td>\n<td><b>public void removeAttribute(String name)<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will remove the object of the specified name bound to the session.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">10<\/span><\/td>\n<td><b>public void setAttribute(String name, Object value)<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will bind the object with the given name and value to the session.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">11<\/span><\/td>\n<td><b>public void setMaxInactiveInterval(int interval)<\/b><\/td>\n<td><span style=\"font-weight: 400\">This method will set the time interval between client accesses before the session gets invalidated.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>4.3 Session Tracking Example<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Session.jsp\r\n&lt;%@page import = \"java.io.*,java.util.*\"%&gt;\r\n&lt;%\r\n      \tDate creationTime = new Date(session.getCreationTime());\r\n      \tDate lastaccessTime = new Date(session.getLastAccessedTime());\r\n      \t\r\n      \tString title = \"My website\";\r\n      \tInteger visit = new Integer(0);\r\n      \tString visitCount = new String(\"visit\");\r\n      \tString userID = new String(\"XYZ\");\r\n      \tString userIDCount = new String(\"userID\");\r\n      \t\r\n      \tif (session.isNew())\r\n      \t{\r\n                         \ttitle = \"My website\";\r\n                         \tsession.setAttribute(userIDCount, userID);\r\n                         \tsession.setAttribute(visitCount, visit);\r\n      \t}\r\n      \tvisit = (Integer)session.getAttribute(visitCount);\r\n      \tvisit = visit+1;\r\n      \tuserID =(String)session.getAttribute(userIDCount);\r\n      \tsession.setAttribute(visitCount, visit);\r\n%&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;&lt;title&gt;Session&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h2&gt;Session tracking&lt;\/h2&gt;\r\n&lt;table width = \"75%\" border = \"2\" align = \"left\"&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;th&gt;SessionInfo&lt;\/th&gt;\r\n               \t&lt;th&gt;Value&lt;\/th&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;Session ID&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(session.getId());%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;Session Creation Time&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(creationTime);%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;Last Access Time&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(lastaccessTime);%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;User ID&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(userID);%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;No. of Visits&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(visit);%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Explanation:<\/strong> In the above example we are displaying the session information like sessionID, user ID, creation time, last access time and number of visits to a website using the session object of HttpSession Interface.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81448\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-5.png\" alt=\"Tracking sessions in JSP\" width=\"1366\" height=\"304\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-5.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-5-300x67.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-5-1024x228.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-5-150x33.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-5-768x171.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image5-5-520x116.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>When we load the page again the no. of visits increases by 1.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image2-7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81447\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image2-7.png\" alt=\"\" width=\"1366\" height=\"308\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image2-7.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image2-7-300x68.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image2-7-1024x231.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image2-7-150x34.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image2-7-768x173.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image2-7-520x117.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h3>4.4 Invalidate Example<\/h3>\n<p>For invalidating the session we append the above code as following:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;%@page import = \"java.io.*,java.util.*\"%&gt;\r\n&lt;%\r\n      \tDate creationTime = new Date(session.getCreationTime());\r\n      \t\r\n      \tDate lastaccessTime = new Date(session.getLastAccessedTime());\r\n      \t\r\n      \tString title = \"My website\";\r\n      \tInteger visit = new Integer(0);\r\n      \tString visitCount = new String(\"visit\");\r\n      \tString userID = new String(\"XYZ\");\r\n      \tString userIDCount = new String(\"userID\");\r\n      \t\r\n      \tif (session.isNew())\r\n      \t{\r\n                         \ttitle = \"My website\";\r\n                         \tsession.setAttribute(userIDCount, userID);\r\n                         \tsession.setAttribute(visitCount, visit);\r\n      \t}\r\n      \tvisit = (Integer)session.getAttribute(visitCount);\r\n      \tvisit = visit+1;\r\n      \tuserID =(String)session.getAttribute(userIDCount);\r\n      \tsession.setAttribute(visitCount, visit);\r\n%&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;&lt;title&gt;Session&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h2&gt;Session tracking&lt;\/h2&gt;\r\n&lt;table width = \"75%\" border = \"2\" align = \"left\"&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;th&gt;SessionInfo&lt;\/th&gt;\r\n               \t&lt;th&gt;Value&lt;\/th&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;Session ID&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(session.getId());%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;Session Creation Time&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(creationTime);%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;Last Access Time&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(lastaccessTime);%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;User ID&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(userID);%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n      \t&lt;tr&gt;\r\n               \t&lt;td&gt;No. of Visits&lt;\/td&gt;\r\n               \t&lt;td&gt;&lt;%out.print(visit);%&gt;&lt;\/td&gt;\r\n      \t&lt;\/tr&gt;\r\n&lt;\/table&gt;\r\n&lt;%session.invalidate();%&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Explanation:<\/strong> The current session gets invalidated and the number of visits gets decreased by 1. Even on refreshing the page the value does not increase as the session has been invalidated and objects are free.<br \/>\n<strong>Output:<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image6-3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81449\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image6-3.png\" alt=\"Invalidate JSP Sessions\" width=\"1366\" height=\"319\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image6-3.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image6-3-300x70.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image6-3-1024x239.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image6-3-150x35.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image6-3-768x179.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image6-3-520x121.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>4.5 Session Management Example<\/h4>\n<p>As one of the important purposes of session tracking is to remember a user, session management can increase the scope. Session management is done so that only the authenticated user can use that session. If the information given by the user matches the database then only he is allowed to go to the next page. Following is the Example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">index.jsp\r\n&lt;%@ page isErrorPage=\"true\" %&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;Session Management&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;form action=\"first.jsp\" method=\"post\"&gt;\r\nEnter your name:&lt;input type=\"text\" name=\"name\"&gt;&lt;br\/&gt;\r\nEnter your password:&lt;input type=\"password\" name=\"password\"&gt;\r\n&lt;br\/&gt;\r\n&lt;input type=\"submit\" name=\"submit\" value=\"submit\"&gt;\r\n&lt;\/form&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\nfirst.jsp\r\n&lt;%\r\nString name = request.getParameter(\"name\");\r\nString password = request.getParameter(\"password\");\r\nif (name.equals(\"dataflair\") &amp;&amp; password.equals(\"admin\")) {\r\nsession.setAttribute(\"username\", name);\r\nresponse.sendRedirect(\"second.jsp\");\r\n}\r\nelse {\r\n        response.sendRedirect(\"index.jsp\");\r\n  }\r\n%&gt;\r\nsecond.jsp\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;title&gt;Welcome to the session&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\nHello\r\n&lt;%= session.getAttribute(\"username\") %&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>Explanation:<\/strong> In this example, we can see the application of session management. We get the name and password and it is set using session.setAttribute( ). If the information matches then only the user gets prompted to the next page else the <strong>index page opens again.<\/strong><br \/>\n<strong>Output:<\/strong><br \/>\na. When the information is right, output is:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81451\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-5.png\" alt=\"JSP session management\" width=\"1366\" height=\"180\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-5.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-5-300x40.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-5-1024x135.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-5-150x20.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-5-768x101.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image4-5-520x69.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81452\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-6.png\" alt=\"JSP Session management\" width=\"1366\" height=\"728\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-6.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-6-300x160.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-6-1024x546.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-6-150x80.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-6-768x409.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image1-6-520x277.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>b. When the information is wrong, output is:<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81454\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-4.png\" alt=\"\" width=\"1366\" height=\"185\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-4.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-4-300x41.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-4-1024x139.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-4-150x20.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-4-768x104.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image7-4-520x70.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-81450\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-5.png\" alt=\"\" width=\"1366\" height=\"188\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-5.png 1366w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-5-300x41.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-5-1024x141.png 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-5-150x21.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-5-768x106.png 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/image3-5-520x72.png 520w\" sizes=\"auto, (max-width: 1366px) 100vw, 1366px\" \/><\/a><\/p>\n<h4>4.6 Deleting a JSP Session<\/h4>\n<p>There are various options to delete a session once you are done with it. They are:<\/p>\n<ul>\n<li><strong>Remove a particular attribute \u2212<\/strong> public void removeAttribute(String name) as discussed above in methods can remove an attribute from a session<\/li>\n<li><strong>Delete the whole session \u2212<\/strong> As the invalidate example shown above, you can call session.invalidate() method to invalidate a session. It will free all the objects bound to the session.<\/li>\n<li><strong>Setting Session timeout \u2212<\/strong> Using method public void setMaxInactiveInterval(int interval) we can set the timeout for a session.<\/li>\n<li><strong>Log the user out \u2212<\/strong> Logging out will invalidate all the sessions related to the user.<\/li>\n<li><strong>web.xml Configuration \u2212<\/strong> You can use config method to set the timeout for the session if working on the tomcat platform.<\/li>\n<\/ul>\n<h4>4.7 Browser session vs. Server session<\/h4>\n<p>As the session tracking at the browser side occurs with the help of cookies, the session information will get discarded once the user closes the browser.<\/p>\n<p>Unlike it, server session stores session information at the server side. This information will remain until the session invalidates, or the maximum inactive time gets over irrespective of the fact that the client has logged out of the browser or not.<\/p>\n<h4>4.8 Use session objects wisely<\/h4>\n<p>Use session objects wisely as it stores some important data.<\/p>\n<p>For example, you develop a banking system that will fetch balance and update balance. Say your first page fetches balance and stores it in a variable called acc_ balance. Then the second page update balance uses this variable acc_balance and updates it.<\/p>\n<p>Now let\u2019s say another developer also works on it and uses the same name variable acc_balance and initializes it with zero. If it happens that your acc_balance variable has value 1000 when the developer calls the first page. Then a program made by another developer gets called. Now the acc_balance=0 and then update balance runs. It uses the acc_balance variable and updates it with say 100. acc_balance should have 1100 as result but it now contains 100.<\/p>\n<p>Thus, use session objects wisely.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we learnt about JSP session tracking. We glanced upon some techniques to track sessions. Then we learnt about session objects and its methods. We also saw some detailed examples of session tracking and management under JSP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article discusses the JSP session tracking and its techniques in detail. It highlights the need for session tracking. It also covers the tracking techniques with its advantages and disadvantages. Additionally, it shows the&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":81446,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22403],"tags":[23103,23104],"class_list":["post-81356","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jsp","tag-jsp-session-tracking","tag-jsp-session-tracking-techniques"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>JSP Session Tracking Techniques - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn about JSP Session Tracking and techniques to track JSP sessions. Also learn about session objects, methods and management with examples\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JSP Session Tracking Techniques - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn about JSP Session Tracking and techniques to track JSP sessions. Also learn about session objects, methods and management with examples\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-01T03:30:35+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:17:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JSP Session Tracking Techniques - DataFlair","description":"Learn about JSP Session Tracking and techniques to track JSP sessions. Also learn about session objects, methods and management with examples","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/","og_locale":"en_US","og_type":"article","og_title":"JSP Session Tracking Techniques - DataFlair","og_description":"Learn about JSP Session Tracking and techniques to track JSP sessions. Also learn about session objects, methods and management with examples","og_url":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-09-01T03:30:35+00:00","article_modified_time":"2021-08-25T08:17:23+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"12 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"JSP Session Tracking Techniques","datePublished":"2020-09-01T03:30:35+00:00","dateModified":"2021-08-25T08:17:23+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/"},"wordCount":1706,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg","keywords":["JSP Session Tracking","JSP Session Tracking techniques"],"articleSection":["JSP Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/","url":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/","name":"JSP Session Tracking Techniques - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg","datePublished":"2020-09-01T03:30:35+00:00","dateModified":"2021-08-25T08:17:23+00:00","description":"Learn about JSP Session Tracking and techniques to track JSP sessions. Also learn about session objects, methods and management with examples","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/08\/JSP-Session-Tracking.jpg","width":1200,"height":628,"caption":"JSP Session Tracking"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/jsp-session-tracking-techniques\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"JSP Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/jsp\/"},{"@type":"ListItem","position":3,"name":"JSP Session Tracking Techniques"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/81356","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=81356"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/81356\/revisions"}],"predecessor-version":[{"id":81456,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/81356\/revisions\/81456"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/81446"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=81356"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=81356"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=81356"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}