table of Contents
2.4.1 The server sends multiple cookies?
2.4.2 How long does the cookie save in the browser?
2.4.3 Can cookies store Chinese?
Four summary of three domain objects
A session overview
1.1 What is a session?
In daily life: the process of asking and answering questions from when you make a call to when you hang up is a conversation.
In the B/S architecture: when the browser sends a request to the server for the first time, a session is established; until one of the parties disconnects, the session ends.
One session: contains multiple requests and responses.
1.2 Conversation technology
Question: Http is a stateless protocol. Two consecutive requests in the same session are independent of each other and do not understand each other.
Function: Used for == storage == data generated by the browser and server in the request and response process ==
Client session technology: cookie
server-side session technology: session
Two Cookie [Key Points]
2.1 Overview
Cookie function: share data between multiple requests in a session, save the data to the client (browser)
jd shopping cart
2.2 Quick start
public class SetServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.创建cookie对象,设置数据
Cookie cookie = new Cookie("name","jack");
// 2.通过response,响应(返回)cookie
response.addCookie(cookie);
}
}
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
// 1. 通过request对象,获取到携带的所有cookie
Cookie[] cookies = req.getCookies();
// 2. 遍历数组
if(cookies !=null ){
for (Cookie cookie : cookies) {
// 获取的就是cookie的name值
String name = cookie.getName();
// 获取cookie的value值
String value = cookie.getValue();
System.out.println(name + "----" + value );
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>GetServlet</servlet-name>
<servlet-class>com.lagou.servlet.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetServlet</servlet-name>
<url-pattern>/getServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SetServlet</servlet-name>
<servlet-class>com.lagou.servlet.SetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SetServlet</servlet-name>
<url-pattern>/setServlet</url-pattern>
</servlet-mapping>
2.3 Working principle
Based on HTTP protocol: request header cookie and response header set-cookie
2.4 Cookie details
2.4.1 The server sends multiple cookies?
public class SetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 创建cookie对象,设置数据
Cookie cookie1 = new Cookie("name", "jack");
Cookie cookie2 = new Cookie("age", "18");
// 2. 通过response对象响应cookie
resp.addCookie(cookie1);
resp.addCookie(cookie2);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.4.2 How long does the cookie save in the browser?
public class MaxAgeCookie extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 创建cookie对象,设置数据
Cookie cookie1 = new Cookie("name", "jack");
// 设置cookie的存活时间
// -1 浏览器关闭,cookie就销毁
// cookie1.setMaxAge(-1);
// 正数 存活60秒,过期销毁
// cookie1.setMaxAge(60*10);
// 0 立即销毁
cookie1.setMaxAge(0);
// 2. 通过response对象响应cookie
resp.addCookie(cookie1);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>MaxAgeCookie</servlet-name>
<servlet-class>com.lagou.servlet.MaxAgeCookie</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MaxAgeCookie</servlet-name>
<url-pattern>/maxAgeCookie</url-pattern>
</servlet-mapping>
2.4.3 Can cookies store Chinese?
public class SetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 创建cookie对象,设置数据
String encodevalue = URLEncoder.encode("杰克 ", "UTF-8");
cookie1 = new Cookie("name", encodevalue);
Cookie cookie2 = new Cookie("age", "18");
// 2. 通过response对象响应cookie
resp.addCookie(cookie1);
resp.addCookie(cookie2);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
// 1. 通过request对象,获取到携带的所有cookie
Cookie[] cookies = req.getCookies();
// 2. 遍历数组
if(cookies !=null ){
for (Cookie cookie : cookies) {
// 获取的就是cookie的name值
String name = cookie.getName();
// 获取cookie的value值
String decodevalue = URLDecoder.decode(value, "UTF-8");
System.out.println(name + "----" + decodevalue);
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
2.5 Cookie characteristics
Three Session [Key Points]
3.1 Overview
Use Cookie Issues
1. Store up to 4K strings
2. Store data is not safe.
Session function: Share data between multiple requests in a session and save the data to the server
3.2 Quick start
HttpSession is also a domain object
Step analysis
1. Store the data in the session
// 1. Get the session object through the request object
HttpSession session = request.getSession();
// 2. Operate the session API and store the data
session.setAttribute("username"," Haha, haha");2. Get data from session
// 1. Get session object through request object
HttpSession session = request.getSession();
// 2. Operate session API to get data
session.getAttribute("username");
public class SetSession extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 将需要共享的数据存到session中
// 1.获取到session对象
HttpSession session = req.getSession();
// 2.通过session对象进行数据存入
session.setAttribute("name","哈哈,呵呵");
System.out.println("当前已将共享数据存到session中.....");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class GetSession extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 将session中的共享数据取出来
// 1.获取session对象
HttpSession session = req.getSession();
// 2.获取共享数据
String name = (String) session.getAttribute("name");
System.out.println(name);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.3 Working principle
Session is based on Cookie technology
3.4 Life cycle
Four summary of three domain objects
request、session、ServletContext
4.1 API
4.2 Life cycle
When to create | When to destroy | Scope of action | |
---|---|---|---|
ServletContext | The server starts normally, Created when the project loads |
Server is down or Destroy when the project is unloaded |
Whole web project (Shared data) |
HttpSession | The user calls request.getSession() for the first time Method created |
Server shuts down abnormally, inactive for 30 minutes, suicide |
In one session, between multiple requests (Shared data) |
HttpServletRequest | Created when the user sends a request | Destroy after the server responds |
A request, repeatedly forwarded between (Shared data) |
4.3 Summary
Can use small but not large ones:
request<session<servletContext
Common scenarios:
request: the result of a query (servlet forwarding jsp)
session: store the private data of the current session,
user login status
verification code, user login status
shopping cart
servletContext: If you need all servlets to be accessible, use this domain object.