一、 Cookie定义与操作
1. 1 定义
cookie1是服务器通知客户端保存键值对的一种技术,
客户端(浏览器端)用于保存数据的一种技术。
客户端有了cookie后,每次请求都发送给服务器。
- 每个cookie大小不超过4kb;
cookie应用场景:
唯品会电商购物:购物车中的订单信息纯存储到cookie中。
1.2 创建cookie
- 创建cookie对象Cookie cookie = new Cookie(key, value)
- 通知客户端保存数据:cookie :response.addCookie(cookie)
测试案例:
- 重写service方法,防止中文乱码;
- 创建cookie
配置xml文件资源路径。
public class Cookietest extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
creatcookie(req,resp);
}
public void creatcookie(HttpServletRequest req, HttpServletResponse resp){
Cookie cookie = new Cookie("key1","value1");
resp.addCookie(cookie);
try {
resp.getWriter().print("cookie保存成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试结果
- cookie每次创建都需要添加保存 resp.addCookie(cookie)
- cookie不能单个获取必须一次全部获取
获取cookie Cookie[] cookies = req.getCookies()
随后对其进行遍历操作获取指定cookie
一般也可采用封装一个cookie类。
- cookie工具类的封装
1.3 cookie值得修改
- 替换值:
- 直接再创建+保存一个新得cookie相同得key不同得values,用新得来替换旧的。
获取需要修改value得cookie,通过setValue方法来修改value得值
需要再添加一次cookie来覆盖原来得值。
1.4 cookie的生命周期
- 默认时session级别的,浏览器开启到关闭。
- cookie得生命控制:
- setMaxAge(数字)这里数字得单位为秒
- 正数表示指定秒数后过期;
- 负数表示过了浏览器这一步cookie就会被删除(默认值为-1)
- 零,表示马上删除cookie
二、Cookie值渲染到jsp页面/模板引擎上
首先创建渲染cookie对应得方法:
public void showCookie(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Cookie cookie = new Cookie("username","jack");
resp.addCookie(cookie);
resp.sendRedirect("http://localhost:8080/web/JSP/cookietest.jsp");
}
对应得jsp文件
<body>
<%--注意如果只获取cookie是获取得全部cookie;
如果获取cookie.username获取的是u而那么这个名字的cookie实例
如果获取的的是cookie.username.value
获取的是名为uesername这个cookie对应的value--%>
${cookie.username.value}
</body>
通过cookie来实现免密登陆
通过修改《学习信息管理系统》的login方法以及login.jsp来实现免密登录。
//创建cookie
Cookie cookie1 = new Cookie("username",username);
//添加cookie
response.addCookie(cookie1);
//创建cookie
Cookie cookie2 = new Cookie("password",password);
//添加cookie
response.addCookie(cookie2);
//重定向跳转页面;
response.sendRedirect("http://localhost:8080/web/student?flag=list");
<td> 登陆用户名:</td>
<td><input type="text" name="username" value="${cookie1.username.value}"></td>
</tr>
<tr>
<td> 登陆密码:</td>
<td><input type="password" name="password" value="${cookie2.password.value}"></td>
三、session的基本操作
3.1 sesion会话
- session是维护客户端和服务端关联的技术
- 根据会话id来维护。
- 特点:在服务器的任何位置创建session,该对象永远是单例的,指向同一内存地址;
3.2 创建sesion会话
- request.getSession() 第一次调用创建Session会话。
- isNew();判断是否是刚刚创建出来的。
- getId();得到session的会话id。
3.3 sesion生命周期
session默认生命周期30mins,也可以自己设置。
HttpSession session = req.getSession();
session.getMaxInactiveInterval();
session.setMaxInactiveInterval();
session.invalidate()方法:让session立即失效。
3.3 sesion维护关联
session是服务端保存数据的手段,cookie是客户端保存数据的手段
session实例的会话id(JSESSTONID)通过响应头的方式给客户端传<br /> 过去。
通过JSESSTONID可以知道服务的链接关系。
3.4 sesion在案例中的应用
在实际应用中我们需要在登陆时把登陆信息存在session中而不是只在后端做一个校验这样是一个伪登陆;
在做登陆模块时,在用户登陆时需要把用户信息保存在session中
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入jstl标签库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>学生列表</title>
<style>
body{
background-color: darkslategray;
}
</style>
</head>
<body>
<c:if test="${not empty user}">
<h1 align="center">学生列表页</h1>
<%--不用新封装方法,就直接用getStudentList--%>
<form action="http://localhost:8080/web/student?flag=list" method="post">
根据姓名模糊查询:<input type="text" name="likename" value="${likename}"> <input type="submit" value="搜索">
</form>
<div style="font-size: 10px;float: right">当前登陆用户为:<span style="color: #0040ff">${user.username}</div>
<table align="center" border="1px">
<tr>
<td colspan="3" align="center"><a href="http://localhost:8080/web/JSP/add.jsp">学生添加</a></td>
<td colspan="3" align="center"><a href="http://localhost:8080/web/student?flag=loginOut">注销</a></td>
</tr>
<tr>
<th>学生姓名</th>
<th>学生性别</th>
<th>学生年纪</th>
<th>学生生日</th>
<th>学生地址</th>
<th>操作</th>
</tr>
<c:forEach items="${students}" var="student">
<tr>
<td>${student.name}</td>
<td>${student.sex == 1? '男' : '女'}</td>
<td>${student.age}</td>
<td>${student.birthday}</td>
<td>${student.address}</td>
<td>
<a href="http://localhost:8080/web/student?flag=deleteStudentById&id=${student.id}">删除</a>
<a href="http://localhost:8080/web/student?flag=showStudentById&id=${student.id}">编辑</a>
</td>
</tr>
</c:forEach>
</table>
</c:if>
<c:if test="${empty user}">
<a href="http://localhost:8080/web/JSP/login.jsp">当前还未登陆请登陆后再访问</a>
</c:if>
</body>
</html>
/*
* 注销当前用户
* 使得session失效即可
* */
private void loginOut(HttpServletRequest request, HttpServletResponse response) {
//获取session对象
HttpSession session = request.getSession();
//使当前session对象失效
session.invalidate();
//重定向到列表页面;
try {
response.sendRedirect("http://localhost:8080/web/student?flag=list");
} catch (IOException e) {
e.printStackTrace();
}
}