Java session scope

I am studying programming as a hobby. I started studying because I wanted to create a secondhand book sales site, but in order to dynamically generate web pages and perform data processing on the server, knowledge of Java Servlet (Java Servlet) is required. I started studying at ... (^_^;) Considering the secondhand book site as an example, the concept of session, which holds the information that the customer put in the cart, was not easy to understand, so I wrote this article !! ヾ ^ _ ^ ♪

--Session --Mechanism to maintain the status of each user

--In the Servlet, handle using the javax.servlet.http.HttpSession interface

 CounterServlet.java


package session;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CounterServlet extends HttpServlet {
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{

		//1.Get session
		HttpSession session = request.getSession();

		//2.Getting an object from a session
		Integer count = (Integer)session.getAttribute("count");

		//3.Counter+1
		if(count == null){
			count = new Integer(0);
		}
		count = new Integer(count.intValue() + 1);

		//4.Store objects in session
		session.setAttribute("count", count);

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html><body>");

		//Display session ID
		out.println("<p>sessionId=" + session.getId());

		//Display session ID
		out.print("<p>count=" + count);

		//Show link to this Servlet
		String linkurl = request.getRequestURI();
		out.println("<p><a href=\"" + linkurl + "\">RELOAD</a>");
		out.println("</body></html>");
	}

}
  1. When the HttpSession object is created, the application server assigns a unique identifier "session ID". Session identity seems to be secured by this session ID.
  2. Get the object named "count" from the session attribute and cast it to Integer type. The first request does not have an object named count registered in the session attribute, so the value of count will be null.
  3. Count up Since it is null in the first stage, substitute 0.
  4. In the setAttribute method, register the value after adding 1 in the session attribute.

Passing data in session scope The data registered in the request scope could not be passed across requests, but the data registered in the session scope can be passed.

--request scope --Objects valid between the Servlet and the forwarded JSP that run in a single HTTP protocol --Destroy when the request is processed --Set as HttpServletRequest attribute

--session scope --Objects valid in Servlets and JSPs that run in the same session --Abandoned at the end of session after a certain period of time --Set as an attribute of HttpSession

Methods for manipulating session-scoped objects

Object getAttribute(java.lang.String name)
Gets the object specified by the argument. Returns null if it does not exist.
Since the returned type is Object type, it seems that it is often used by casting appropriately.

void setAttribute(java.lang.String name, java.lang.Object value)
Register the object specified by the argument value with the name name.

void removeAttribute(java.lang.String name)
Deletes the object specified by the argument name.

java.util.Enumeration getAttributeNames()
Returns a list of registered names.

Session timeout If you do not delete the session data, unused data will remain on the server. As a workaround, the application server has a mechanism to check the elapsed time since the client last accessed and delete it.

web.xml


<session-config>
        <session-timeout>3</session-timeout>
</session-config>

Recommended Posts

Java session scope
Java variable scope (scope)
Java variable scope
Scope
Java
Java
Java Servlet / JSP Request Scope Part 1
[In-house study session] Java exception handling (2017/04/26)
[Java] Object-oriented syntax-class / field / method / scope
[Study session memo] Java Day Tokyo 2017
[Basic knowledge of Java] Scope of variables
Java learning (0)
[Java] array
Summary of in-house newcomer study session [Java]
Java protected
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
Java method
Java array
[Java] ArrayDeque
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
java notes
java beginner
[java] interface