session built-in objects and operation mechanism

A session:

Web applications, a session refers to a period of time, a user interaction through a series of requests and responses performed between the browser and the server. In one session, the user can access the Web application system, including a variety of resources, including web page.

Two .session built-in objects:

  When the user (browser) sends a request to the first Web application server, the server creates a unique identifier for the user session , the session lasted until the end of the visit (the browser is closed or the user for a long time without access to the Web applications). JSP using the session object represents a session, information is stored in the upcoming session object, the user get ready in this session.

Just close the browser and the Web server interrupts the connection, but the session object still exists on the server side, if the time expires, the Web server will be deleted, if the server is shut down, the object is serialized into SESSIONS.ser file, the server is restarted , then the file again deserialize session object.

Here is the code used Example:

Set session value set.jsp file

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		<%
			session.setAttribute("session",new Date());
		%>
	</body>
</html>

get.jsp document printing session values 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		<%=session.getAttribute("session") %>
	</body>
</html>

 

Three .session operating mechanism

1, when a user (browser) sends a request to the first Web server application, the request header is not included in the sessionID, the server creates a new session for the client, and this generates a session corresponding to the sessionID, then the sessionID with this response back to the client;


2, the user (browser) when the application sends a request to the Web server again, the request header contains sessionID, the server will first find a corresponding session by sessionID, not as the case sessionID this response back to the client again; if the session is due to grow beyond Web server to delete, then re-create a new session, and generate a corresponding sessionID this session, then the sessionID with this response back to the client;

发布了99 篇原创文章 · 获赞 93 · 访问量 5211

Guess you like

Origin blog.csdn.net/DangerousMc/article/details/103132041