Cookies and sessions
This is the back-end small class of the monastery. Each article is shared from
[background introduction] [knowledge analysis] [common problems] [solutions] [coding practice] [extended thinking] [more discussion] [References]
Eight aspects of in-depth analysis of back-end knowledge / skills. This article shares:
[cookies and sessions]
Hello, I am the eighth student of Shanghai Branch of it Academy. I am an honest, pure and kind java programmer. Today, I'd like to share with you the fifth Java task on the official website of the Academy, expanding the knowledge points in thinking - cookies and sessions
1 background introduction
What is conversation? Users open a browser, click multiple hyperlinks, access multiple web resources on the server, and then close the browser. The whole process is called a session. HTTP protocol is a "stateless" protocol. The client browser establishes a connection with the server, sends a request, obtains the corresponding information, and then closes the connection, which means that every time the client retrieves a web page, When the client opens a separate connection to the web server, the server will automatically not keep any records requested by the client. Therefore, the container cannot recognize whether the next request and the previous request are the same request. For the container, each request is new.
In the process of using the browser to talk with the server, some data will inevitably be generated. The web server has no short-term memory. How to save these user data?
The client needs a unique session ID. for the first request of the client, the container will generate a unique session ID and send it back to the client through the response. The client will send back this session ID in each subsequent request. When the container sees it, it will find the matching session and associate it with the request.
2 knowledge analysis
Cookie is a client technology. The program writes each user's data to their respective browser in the form of cookie. When users use the browser to access the web resources in the server, they will go with their own data. In this way, web resources deal with the data of users. Session is a server-side technology. Using this technology, the server can create an exclusive session object for each user's browser when running. Because the session is exclusive to the user's browser, users can put their own data in their own session when accessing the server's web resources. When users access other web resources in the server again, Other web resources then take data from the user's own session to serve the user.
3 frequently asked questions
What if the browser disables cookies?
4 Solutions
URL rewriting, hiding form fields
5 coding practice
Cookie: Cookie tool class
/*
添加cookie
@param response
@param key cookie主键
@param value cookie值
/
public static void addCookie(HttpServletResponse response,String key,String value){
Cookie cookie = new Cookie(key,value);
//设置路径
cookie.setPath("/");
//设置保存时间为1天,单位为s
cookie.setMaxAge(246060);
//通过response.addCookie将此条cookie添加到客户端
response.addCookie(cookie);
}/*
删除cookie
@param response
@param request
@param key
/
public static void deleteCookie(HttpServletResponse response, HttpServletRequest request,String key) {
//获取浏览器访问服务器时传递来的cookie数组
Cookie cookies[] = request.getCookies();
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
if (cookies[i].getName().equals(key)) {
Cookie cookie = new Cookie(key, null);
//此路径需与之前创建时相同
cookie.setPath("/");
//设置为0即为删除
cookie.setMaxAge(0);
response.addCookie(cookie);
}
}
}
}/*
获取指定cookie
@param request
@param key
@return
@throws UnsupportedEncodingException
*/
public static String getCookieValue(HttpServletRequest request,String key) throws UnsupportedEncodingException {
//通过request.getCookies获取客户端提交的所有cookie
for (Cookie cookie : request.getCookies()) {
if (cookie.getName().equals(key)) {
return URLDecoder.decode(cookie.getValue(), "UTF-8");
}
}
return null;
}
}
controller
@RequestMapping(value = "/cookie", method = RequestMethod.POST)
public String cookie(@RequestParam String name, String password, HttpServletResponse response, Model model){
CookieUtil.addCookie(response,"name",name);
CookieUtil.addCookie(response,"password",password);
model.addAttribute("name",name);
model.addAttribute("password",password);
return "cookie";
}@RequestMapping(value = "getcookie",method = RequestMethod.GET)
public String getcookie(HttpServletRequest request,Model model) throws UnsupportedEncodingException {
String name =CookieUtil.getCookieValue(request,"name");
String password = CookieUtil.getCookieValue(request,"password");
model.addAttribute("name",password);
return "cookie";
}@RequestMapping(value = "deletecookie",method = RequestMethod.GET)
public String deletecookie(HttpServletRequest request,HttpServletResponse response,Model model) throws UnsupportedEncodingException {
// CookieUtil.deleteCookie(response,request,"name");
CookieUtil.deleteCookie(response,"password");
String name = CookieUtil.getCookieValue(request,password);
return "redirect:test.jsp";
}
}
Form JSP
密码
<a href="/getcookie">获取cookie
<a href="/deletecookie">删除cookie