{"id":117,"date":"2017-03-02T07:34:20","date_gmt":"2017-03-02T07:34:20","guid":{"rendered":"http:\/\/codeinsightacademy.com\/blog\/?p=117"},"modified":"2017-05-11T15:34:23","modified_gmt":"2017-05-11T15:34:23","slug":"session-management-in-java","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/java\/session-management-in-java\/","title":{"rendered":"Session Management in Java"},"content":{"rendered":"<p>List of Files<\/p>\n<ol>\n<li>header.jsp<\/li>\n<li>login.jsp<\/li>\n<li>home.jsp<\/li>\n<li>logout.jsp<\/li>\n<\/ol>\n<hr \/>\n<p>header.jsp<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;a href=\"login.jsp\"&gt;Login&lt;\/a&gt;\r\n&lt;a href=\"home.jsp\"&gt;Home&lt;\/a&gt;\r\n&lt;a href=\"logout.jsp\"&gt;Logout&lt;\/a&gt;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>login.jsp<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;%\r\n    HttpSession sess = request.getSession();\r\n\r\n    if (sess.getAttribute(\"sess_username\") != null) { \/\/ check is session variable exist in session pool or not\r\n        response.sendRedirect(\"home.jsp\"); \/\/ if session variable is already set then redirect it to home page else stay on login page\r\n    }\r\n%&gt;\r\n\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n    &lt;head&gt;\r\n        &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"&gt;\r\n        &lt;title&gt;JSP Page&lt;\/title&gt;\r\n    &lt;\/head&gt;\r\n    &lt;body&gt;\r\n        &lt;%@include file=\"header.jsp\" %&gt;\r\n        &lt;h1&gt;Login&lt;\/h1&gt;\r\n        &lt;form action=\"home.jsp\" method=\"post\"&gt;\r\n            Username &lt;input type=\"text\" name=\"username\"\/&gt;&lt;br\/&gt;\r\n            Password &lt;input type=\"password\" name=\"password\"\/&gt;&lt;br\/&gt;\r\n            &lt;input type=\"submit\" value=\"Login\"\/&gt;\r\n        &lt;\/form&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>home.jsp<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;%\r\n    HttpSession sess = request.getSession(); \/\/get the session object from getSession() method of request object\r\n\r\n    if (sess.getAttribute(\"sess_username\") == null) { \/\/ check whether the session variable is exist in session pool or not\r\n        sess.setAttribute(\"sess_username\", request.getParameter(\"username\")); \/\/ create session variable if it is not present in session pool\r\n    }\r\n%&gt;\r\n\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n    &lt;head&gt;\r\n        &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"&gt;\r\n        &lt;title&gt;JSP Page&lt;\/title&gt;\r\n    &lt;\/head&gt;\r\n    &lt;body&gt;\r\n        &lt;%@include file=\"header.jsp\" %&gt;\r\n        &lt;h1&gt;Home&lt;\/h1&gt;\r\n        &lt;h2&gt;Hello &lt;%=sess.getAttribute(\"sess_username\")%&gt; &lt;\/h2&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>logout.jsp<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;%\r\n    HttpSession sess = request.getSession();\r\n    sess.invalidate(); \/\/ this will terminate the session and destroy all variables stored in session pool\r\n%&gt;\r\n\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n    &lt;head&gt;\r\n        &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"&gt;\r\n        &lt;title&gt;JSP Page&lt;\/title&gt;\r\n    &lt;\/head&gt;\r\n    &lt;body&gt;\r\n        &lt;%@include file=\"header.jsp\" %&gt;\r\n        &lt;h1&gt;Logout&lt;\/h1&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<p>Display Session variables from pool<\/p>\n<p>Console o\/p:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">Enumeration attributeNames = sess.getAttributeNames();\r\n\r\nwhile (attributeNames.hasMoreElements()) {\r\n\r\n    String name = (String) attributeNames.nextElement();\r\n\r\n    String value = (String) sess.getAttribute(name);\r\n\r\n    out.println(name + \"=\" + value);\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>JSTL(<span class=\"_Tgc\">Java Server Pages Standard Tag Library<\/span>) o\/p:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;c:forEach var=\"session\" items=\"${sessionScope}\"&gt;\r\n\r\n    ${session.key} = ${session.value}\r\n\r\n&lt;\/c:forEach&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>List of Files header.jsp login.jsp home.jsp logout.jsp header.jsp &lt;a href=&#8221;login.jsp&#8221;&gt;Login&lt;\/a&gt; &lt;a href=&#8221;home.jsp&#8221;&gt;Home&lt;\/a&gt; &lt;a href=&#8221;logout.jsp&#8221;&gt;Logout&lt;\/a&gt; &nbsp; login.jsp &nbsp; &lt;% HttpSession sess = request.getSession(); if (sess.getAttribute(&#8220;sess_username&#8221;) != null) { \/\/ check is session variable exist in session pool or not response.sendRedirect(&#8220;home.jsp&#8221;); \/\/ if session variable is already set then redirect it to home page else stay on [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/117"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=117"}],"version-history":[{"count":12,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/117\/revisions"}],"predecessor-version":[{"id":251,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/117\/revisions\/251"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}