会话技术Cookie
会话技术
客户端发送一个请求,服务器做出一个相应,就是一次会话
当有两个客户端发送请求时,会发送相同的请求报文,所以服务器无法区分,然后引入了会话
主要有cookie,session
cookie常用代码
Cookie cookie = new Cookie("username", username);
response.addCookie(cookie);
response.setContentType("text/html;charset=utf-8");
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies) {
if("username".equals(cookie.getName())){
response.getWriter().println("欢迎您, " + cookie.getValue());
}
}
}
Cookie
优势:小,方便,服务器压力小
劣势:安全系数低
cookie的优势和劣势
优势:很轻便、使用的话也很方便,数据存储在客户端,服务器压力小一些
劣势:数据存储在客户端,数据安全性就比较低,cookie一般只存储一些非敏感数据;只能存储字符串类型,受限;大小一般4kb
cookie是服务器产生的数据,然后响应给客户端,客户端再次访问会携带这部分数据,然后就知道是哪个客户端访问的了
案例:获得客户上次登录的时间
package com.cskaoyan.cookie;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/last")
public class LastLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies) {
if("lastLogin".equals(cookie.getName())){
// "1321312312312313"
String value = cookie.getValue();
response.getWriter().println(new Date(Long.parseLong(value)));
}
}
}
//cookie的value值不能有空格 2020/12/01-10:27:00
//Cookie cookie = new Cookie("lastLogin", new Date().toString());
Cookie cookie = new Cookie("lastLogin", System.currentTimeMillis() + "");
//需要将cookie信息加入到HTTP响应头中
//response.setHeader();
response.addCookie(cookie);
}
}
登录成功后,获取其用户名
package com.cskaoyan.cookie;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
Cookie cookie = new Cookie("username", username);
response.addCookie(cookie);
response.getWriter().println("登录成功,即将跳转至个人主页....");
response.setHeader("refresh", "2;url=" + request.getContextPath() + "/info");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
package com.cskaoyan.cookie;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/info")
public class InfoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies) {
if("username".equals(cookie.getName())){
response.getWriter().println("欢迎您, " + cookie.getValue());
}
}
}
}
}
设置cookie存活时间
cookie仅仅存在于浏览器的内存中,浏览器关闭则cookie失效,如果想持久化保存,需要设置时间
cookie.setMaxAge(180);
如果将时间设置为0,可以删除cookie
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/last2")
public class LastLoginServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies) {
if("lastLogin".equals(cookie.getName())){
// "1321312312312313"
cookie.setMaxAge(0);
response.addCookie(cookie);
String value = cookie.getValue();
response.getWriter().println(new Date(Long.parseLong(value)));
}
}
}
}
}
设置路径
如果没有设置的情况下,默认访问当前主机下面的所有资源均会携带cookie,如果希望访问某个路径才携带cookie,则可以设置一个path
如果某个cookie设置了path,那么删除cookie时删除不成功,原因在于
删除cookie时,也需要指定和生成cookie时设置的相同的path,如果没有设置相同的path,则不会删除cookie
如果新建cookie的时候,没有设置path,那么删除的时候也不需要设置path
package com.cskaoyan.cookie;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
@WebServlet("/last")
public class LastLoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies) {
if("lastLogin".equals(cookie.getName())){
// "1321312312312313"
String value = cookie.getValue();
response.getWriter().println(new Date(Long.parseLong(value)));
}
}
}
//cookie的value值不能有空格 2020/12/01-10:27:00
//Cookie cookie = new Cookie("lastLogin", new Date().toString());
Cookie cookie = new Cookie("lastLogin", System.currentTimeMillis() + "");
//cookie.setMaxAge(180);
// /xxx/xxx
//设置path之后,仅当访问对应路径时才会携带cookie,其他不会携带
cookie.setPath(request.getContextPath() + "/last2");
//需要将cookie信息加入到HTTP响应头中
//response.setHeader();
response.addCookie(cookie);
}
}
@WebServlet("/last2")
public class LastLoginServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies) {
if("lastLogin".equals(cookie.getName())){
// "1321312312312313"
cookie.setMaxAge(0);
cookie.setPath(request.getContextPath() + "/last2");
response.addCookie(cookie);
String value = cookie.getValue();
response.getWriter().println(new Date(Long.parseLong(value)));
}
}
}
}
}