【JavaWeb】Servlet的应用全面讲解(三)cookie和session
HTTP 协议是一种无状态协议,即每次服务端接收到客户端的请求时,都是一个全新的请求,服务器并不知道客户端的历史请求记录;Session 和 Cookie 的主要目的就是为了弥补 HTTP 的无状态特性。
一、Cookie
Cookie,有时也用其复数形式Cookies,指某些网站为了辨别用户身份、进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密)
(一)cookie的创建和发送
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("name","admin");
resp.addCookie(cookie);
}
(二)cookie的获取
将会返回一个cookie数组,供获取cookie数据
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie[] cookies = req.getCookies();
if(cookies != null && cookies.length>0){
for (Cookie cookie : cookies){
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("名称:"+name+" 值:"+value);
}
}
}
(三)cookie的到期时间
1. 到期时间的表示
2. 代码演示
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie = new Cookie("uname1","zhangsan");
cookie.setMaxAge(-1);
resp.addCookie(cookie);
Cookie cookie2 = new Cookie("uname2","lisi");
cookie.setMaxAge(30);
resp.addCookie(cookie2);
Cookie cookie3 = new Cookie("uname3","wangwu");
cookie.setMaxAge(0);
resp.addCookie(cookie3);
}
(四)cookie中文乱码问题、以及如何重新设置
1. 说明
2. 代码演示
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = "姓名";
String value = "张三";
name = URLEncoder.encode(name);
value = URLEncoder.encode(value);
Cookie cookie = new Cookie(name,value);
resp.addCookie(cookie);
Cookie[] cookies = req.getCookies();
if(cookies != null&& cookies.length>0){
for(Cookie cook : cookies){
System.out.println(URLDecoder.decode(cook.getName()));
System.out.println(URLDecoder.decode(cook.getValue()));
}
}
Cookie cookie2 = new Cookie("name","zhangsan");
resp.addCookie(cookie2);
}
(五)指定路径获取cookie
1. 说明
2. 代码演示
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie01 = new Cookie("cookie01","cookie01");
cookie01.setPath("/");
resp.addCookie(cookie01);
Cookie cookie02 = new Cookie("cookie02","cookie02");
cookie01.setPath("/servlet");
resp.addCookie(cookie02);
Cookie cookie03 = new Cookie("cookie03","cookie03");
cookie01.setPath("/指定站点名");
resp.addCookie(cookie03);
Cookie cookie04 = new Cookie("cookie04","cookie04");
cookie01.setPath("/servlet/cookie04");
resp.addCookie(cookie04);
}
二、Session
在计算机中,尤其是在网络应用中,称为“会话控制”。Session 对象存储特定用户会话所需的属性及配置信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Session 对象中的变量将不会丢失,而是在整个用户会话中一直存在下去。当用户请求来自应用程序的 Web 页时,如果该用户还没有会话,则 Web 服务器将自动创建一个 Session 对象。
(一)session对象的获取和访问
1. 说明
2.代码演示
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
String id = session.getId();
System.out.println(id);
System.out.println(session.getCreationTime());
System.out.println(session.getLastAccessedTime());
System.out.println(session.isNew());
}
(二)session域对象的应用
1. 说明
2. 代码演示
重定向时,session域依旧能够共享
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setAttribute("uname", "admin");
session.setAttribute("upwd", "123456");
session.removeAttribute("upwd");
req.setAttribute("name","zhangsan");
resp.sendRedirect("sessionTest.jsp");
}
jsp页面获取session域对象
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>获取域对象</title>
</head>
<body>
<%
//获取session域对象
String uname = (String) request.getSession().getAttribute("uname");
String upwd = (String) request.getSession().getAttribute("upwd");
//获取request域对象
String name = (String) request.getAttribute("name");
out.print("uname :"+ uname + ",upwd :"+ upwd +",name :" +name);
%>
</body>
</html>
session对象的过期和销毁
1. 说明
2. 代码演示
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.setAttribute("uname", "admin");
session.invalidate();
}
End
简单理解cookie和session和cookie就是一个字典,只是一个储存在服务器、一个存储在客户端。
|