session Instructions

        Learn Cookie later, solves the problem of sharing data without sending the request. Cookie is a browser-side data storage technology, this lesson focuses on an important additional data storage technology, session technology.

Session Learning :

Problem: Request object is solved once is not requested within the same Servlet data sharing, then deal with the different requests a user needs to use the same data how to do it?

solve:

Use session technology.

principle:

The first user uses the browser sends a request to the server, the server after receiving the request, invoked Servlet corresponding processing. Users will create in the process

A session object is used to store user data request processing relevant public, and this session object JSESSIONID in Cookie is stored in the browser ( temporary storage, i.e., failure of the browser is closed) . When a user initiates a second request and the subsequent request, the request message will be included JSESSIONID , server upon receiving the request, the corresponding Servlet invocation request processed simultaneously according JSESSIONID return the corresponding session object.

Features:

Session technique is dependent on Cookie art server data storage technology. Created by the server for each user has a separate session default storage time is 30 Fenzhong

effect:

Solve the problem of different data sharing request of a user.

use:

1. Create Session objects

2. The data stored in session objects

3. Get the session objects

4. The data acquired from the session objects

5. If the acquisition session data is not present in the return null .

note:

We do not close the browser, and the session without fail, any user with a request of any Servlet items acquired are the same in a session object.

Scope:

Single session

Case:
Use session to store data:
import java.io.IOException;

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 ServletA extends HttpServlet {
		@Override
		protected void service(HttpServletRequest req, HttpServletResponse resp)
				throws ServletException, IOException {
			//设置请求编码格式
			req.setCharacterEncoding("utf-8");
			//设置响应 编码格式
			resp.setContentType("text/html;charset=utf-8");
			//获取请求信息
			String uname=req.getParameter("uname");
			//处理请求数据
			System.out.println("ServletA.service():"+uname);
			//创建Session对象
				HttpSession session = req.getSession();
			//存储数据到session对象中
				session.setAttribute("uname", uname);
				System.out.println("ServletA.service():"+session.getId());
			//响应处理结果
				//重定向
				resp.sendRedirect("b");
		}
}

Data obtained using the session:

import java.io.IOException;

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 ServletB extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//设置请求编码格式
		req.setCharacterEncoding("utf-8");
		//设置响应 编码格式
		resp.setContentType("text/html;charset=utf-8");
		//获取请求信息
			//获取Session对象
				HttpSession session = req.getSession();
			//获取A的处理结果数据
				String uname=(String) session.getAttribute("uname");
		//处理请求数据
			//打印A流转的数据
			System.out.println("ServletB.service():"+uname);
		//响应处理结果
			//重定向
	}
}

Test run:

After knocking on the transport redirected to b

 
Published 178 original articles · won praise 14 · views 20000 +

Guess you like

Origin blog.csdn.net/ZGL_cyy/article/details/104465766