<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">The <i><b>HttpSession </b></i>object represents a user session. A user session contains information about the user across multiple HTTP requests.When a user enters your site for the first time, the user is given a unique ID to identify his session by. This ID is typically stored in a cookie or in a request parameter.</p>
<h2><b>Here is how you access the session object:</b></h2>
<ul style="text-align: left;">
<li><i><b>public HttpSession getSession():</b></i>Returns the current session associated with this request, or if the request does not have a session, creates one.</li>
<li><i><b>public HttpSession getSession(boolean create):</b></i>Returns the current HttpSession associated with this request or, if there is no current session and create is true, returns a new session.</li>
</ul>
<p> ;</p>
<pre class="highlight">protected void doPost(HttpServletRequest request, 
 HttpServletResponse response) 
 throws ServletException, IOException { 
 
 HttpSession session = request.getSession(); 
} 
</pre>
<p>You can store values in the session object, and retrieve them later. First, let&#8217;s see how you can store values in the session object:</p>
<pre class="highlight">session.setAttribute("userName", "theUserName"); 
</pre>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<h3>
<b>To read the value again, you do this:</b></h3>
<pre class="highlight">String userName = (String) session.getAttribute("userName"); 
</pre>
<p>Values stored in the session object are stored in the memory of the servlet container.</p>
<h3><b>An object of <i>HttpSession </i>can be used to perform two tasks:</b></h3>
<ol style="text-align: left;">
<li>bind objects</li>
<li>view and manipulate information about a session, such as the session identifier, creation time, and last accessed time.</li>
</ol>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/12/httpsession.jpg" border="0" /></div>
<h3><b>Commonly used methods of <i>HttpSession </i>interface</b></h3>
<ol style="text-align: left;">
<li><i><b>public String getId(): </b></i>Returns a string containing the unique identifier value.</li>
<li><i><b>public long getCreationTime():</b></i> Returns the time when this session was created, measured in milliseconds since midnight January 1, 1970 GMT.</li>
<li><i><b>public long getLastAccessedTime(): </b></i>Returns the last time the client sent a request associated with this session, as the number of milliseconds since midnight January 1, 1970 GMT.</li>
<li><i><b>public void invalidate(): </b></i>Invalidates this session then unbinds any objects bound to it.</li>
</ol>
<h3><b>Sessions and Clusters</b></h3>
<p>If you have an architecture with 2 web servers in a cluster, keep in mind that values stored in the session object of one server, may not be available in the session object on the other server. So, if a user&#8217;s requests are divided evenly between the two servers, sometimes session values may be missing.</p>
<p>The solution to this problem would be one of:</p>
<ol style="text-align: left;">
<li>Do not use session attributes.</li>
<li>Use a session database, into which session attributes are written, and from which it is read.</li>
<li>Use sticky session, where a user is always sent to the same server, throughout the whole session.</li>
</ol>
<p> ;</p>
</div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/requestdispatcher-interface/">Previous</a> <;<; || <a href="https://dineshonjava.com/java-servlets-overview/">Index </a>|| >;>;<a href="https://dineshonjava.com/send-redirect-in-servlet/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><b>Servlet Related Posts</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/java-servlets-overview/">Java Servlets Overview</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-life-cycle/">Servlet Life Cycle</a></b></li>
<li><b><a href="https://dineshonjava.com/hello-world-example-in-servlet-interface/">Servlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/2017/01/difference-between-servletconfig-and-cervletcontext-in-java-servlet/ "> Difference between ServletConfig and ServletContext<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/"> Difference between GenericServlet and HttpServlet<br />
</a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-web-application-servlet/">What is web application?</a></b></li>
<li><b><a href="https://dineshonjava.com/advantages-of-servlets-over-cgi/">Advantages of Servlets over CGI</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-genericservlet-and/">GenericServlet Example</a></b></li>
<li><b><a href="https://dineshonjava.com/requestdispatcher-interface/">RequestDispatcher Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servletconfig-interface/">ServletConfig</a></b></li>
<li><b><a href="https://dineshonjava.com/servletcontext-interface/">ServletContext </a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-filters/">Servlet Filter Example</a></b></li>
<li><b><a href="https://dineshonjava.com/servlets-database-access/">Database Access Example using Sevlet</a></b></li>
<li><b><a href="https://dineshonjava.com/servlet-upload-file-example/">File Uploading Example using Servlet</a></b></li>
</ol>
</div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/requestdispatcher-interface/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/send-redirect-in-servlet/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '263'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…