{"id":13850,"date":"2015-11-23T12:22:20","date_gmt":"2015-11-23T12:22:20","guid":{"rendered":"https:\/\/www.whizlabs.com\/?p=13850"},"modified":"2020-09-01T07:04:19","modified_gmt":"2020-09-01T07:04:19","slug":"session-management","status":"publish","type":"post","link":"https:\/\/www.whizlabs.com\/blog\/session-management\/","title":{"rendered":"Session Management"},"content":{"rendered":"<p style=\"text-align: justify\">Knowingly or unknowingly \u2018sessions\u2019 are part of our daily digital life and this\u00a0post seeks to explain the concepts related to \u2018session management\u2019 in Java.\u00a0\u2018Session Management\u2019 is an interesting topic and it is also one of the topics for\u00a0the \u2018Web component developer 6\u2019 exam.<\/p>\n<h4 style=\"text-align: justify\">Introduction:<\/h4>\n<p style=\"text-align: justify\">This post assumes understanding of servlets, web container and other basic\u00a0terms. To put it briefly, a \u2018servlet\u2019 is a small program that runs inside a web\u00a0server. A \u2018servlet container\u2019 aids in the management of loading and running\u00a0servlets.<\/p>\n<h4 style=\"text-align: justify\">HTTP:<\/h4>\n<p style=\"text-align: justify\">Unlike other protocols like FTP or TCP, <strong>HTTP<\/strong> is stateless protocol. A\u00a0\u2018stateless protocol\u2019 is one in which the client and server do not remember each\u00a0other and there is no persistent connection. The Web container thinks of each\u00a0request by the client as a new one. This prevents conversations between the\u00a0client and server which is an absolute necessity in today\u2019s online environment.<\/p>\n<p style=\"text-align: justify\">\u2018Sessions\u2019 enable conversations between the client and server by means of\u00a0\u2018cookies\u2019 or URL rewriting.<\/p>\n<h4 style=\"text-align: justify\">Sessions:<\/h4>\n<p style=\"text-align: justify\">\u2018Sessions\u2019 always makes one conjure thoughts related to shopping baskets and\u00a0online shopping and rightly so. \u2018Session management\u2019 deals with the servlet\u00a0container creating a session with a client and interacting with the user over\u00a0multiple requests. Once the interaction is over, the session is invalidated.<\/p>\n<p style=\"text-align: justify\">In order to work with sessions, the \u2018HttpSession\u2019 interface must be imported. It\u00a0is present in the javax.servlet.http package.<\/p>\n<p style=\"text-align: justify\">Let us first see how session management works with \u2018cookies\u2019. The client\u00a0initiates a conversation with the server and the container responds with a unique\u00a0session id that will be used for future conversations.<img decoding=\"async\" class=\"aligncenter size-full wp-image-13851\" src=\"https:\/\/www.whizlabs.com\/wp-content\/uploads\/2015\/11\/session-tracking.png\" alt=\"session tracking\" width=\"601\" height=\"394\" \/><\/p>\n<p style=\"text-align: justify\">This is illustrated in the figure above. During each request, the client and server\u00a0use the assigned session id which will enable it to have a smooth interaction.\u00a0Once the interaction is completed, the session is invalidated.<\/p>\n<h4 style=\"text-align: justify\">Methods in Http Session interface:<\/h4>\n<p style=\"text-align: justify\">These are some of the important methods relating to HttpSession interface.\u00a0The most important method which deals with creating sessions is present in\u00a0\u2018HttpServletRequest\u2019 interface which is:<\/p>\n<p style=\"text-align: justify\">HttpSession getSession();<\/p>\n<p style=\"text-align: justify\">This method returns an existing session if one exists or creates a new one if it is\u00a0not present.<\/p>\n<p style=\"text-align: justify\">Hence this code,<\/p>\n<p style=\"text-align: justify\">HttpSession session=request.getSession();<\/p>\n<p style=\"text-align: justify\">will create a new session if it is not present where \u2018request\u2019 is<\/p>\n<p style=\"text-align: justify\">\u2018HttpServletRequest\u2019 interface.<\/p>\n<p style=\"text-align: justify\">This code:<\/p>\n<p style=\"text-align: justify\">HttpSession session= request.getSession(boolean var);<\/p>\n<p style=\"text-align: justify\">returns a pre-existing session. However, if the session does not exist and the\u00a0value of the boolean variable \u2018var\u2019 is \u2018false\u2019, it returns \u2018null\u2019.<\/p>\n<p style=\"text-align: justify\">Again, if the session does not exist and if the value of the boolean variable \u2018var\u2019\u00a0is \u2018true\u2019, it creates a new session.<\/p>\n<p style=\"text-align: justify\">\u2018request\u2019 is again of type \u2018HttpServletRequest\u2019 interface.<\/p>\n<p style=\"text-align: justify\">The other methods are:<\/p>\n<p style=\"text-align: justify\">1. void invalidate();<\/p>\n<p style=\"text-align: justify\">This invalidates the session.<\/p>\n<p style=\"text-align: justify\">2. boolean isNew();<\/p>\n<p style=\"text-align: justify\">This method is used to determine whether the client has chosen to join a\u00a0session created by the server. This method returns \u2018true\u2019 if the client\u00a0chooses not to join a conversation.<\/p>\n<p style=\"text-align: justify\">3. Object getAttribute(String);<\/p>\n<p style=\"text-align: justify\">4. void setAttribute(String, Object);<\/p>\n<p style=\"text-align: justify\">5. void removeAttribute(String);<\/p>\n<p style=\"text-align: justify\">The getAttribute(), setAttribute() and removeAttribute() method are used\u00a0to retrieve attributes, set attributes and remove attributes in session scope.<\/p>\n<p style=\"text-align: justify\">This post dealt with handling \u2018session management\u2019 and \u2018cookies\u2019. The next\u00a0post will deal with session management and URL rewriting.<\/p>\n<p style=\"text-align: justify\"><strong>Image Credits:\u00a0<\/strong>Google<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Knowingly or unknowingly \u2018sessions\u2019 are part of our daily digital life and this\u00a0post seeks to explain the concepts related to \u2018session management\u2019 in Java.\u00a0\u2018Session Management\u2019 is an interesting topic and it is also one of the topics for\u00a0the \u2018Web component developer 6\u2019 exam. Introduction: This post assumes understanding of servlets, web container and other basic\u00a0terms. To put it briefly, a \u2018servlet\u2019 is a small program that runs inside a web\u00a0server. A \u2018servlet container\u2019 aids in the management of loading and running\u00a0servlets. HTTP: Unlike other protocols like FTP or TCP, HTTP is stateless protocol. A\u00a0\u2018stateless protocol\u2019 is one in which the [&hellip;]<\/p>\n","protected":false},"author":220,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[31,910,1456,1457,1504],"class_list":["post-13850","post","type-post","status-publish","format-standard","hentry","category-java","tag-scwcdocwcd","tag-http","tag-session-management","tag-sessions","tag-sunoracle-web-component-developer-certification"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"profile_24":false,"profile_48":false,"profile_96":false,"profile_150":false,"profile_300":false,"tptn_thumbnail":false,"web-stories-poster-portrait":false,"web-stories-publisher-logo":false,"web-stories-thumbnail":false},"uagb_author_info":{"display_name":"Aditi Malhotra","author_link":"https:\/\/www.whizlabs.com\/blog\/author\/aditi\/"},"uagb_comment_info":0,"uagb_excerpt":"Knowingly or unknowingly \u2018sessions\u2019 are part of our daily digital life and this\u00a0post seeks to explain the concepts related to \u2018session management\u2019 in Java.\u00a0\u2018Session Management\u2019 is an interesting topic and it is also one of the topics for\u00a0the \u2018Web component developer 6\u2019 exam. Introduction: This post assumes understanding of servlets, web container and other basic\u00a0terms.&hellip;","_links":{"self":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/13850","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/users\/220"}],"replies":[{"embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/comments?post=13850"}],"version-history":[{"count":1,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/13850\/revisions"}],"predecessor-version":[{"id":75985,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/posts\/13850\/revisions\/75985"}],"wp:attachment":[{"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/media?parent=13850"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/categories?post=13850"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.whizlabs.com\/blog\/wp-json\/wp\/v2\/tags?post=13850"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}