javaweb的会话跟踪技术
Cookie
图中有三个会话
1、cookie的基本使用
package com.web.web2;
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("/aServlet")
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//发送cookie
//1.创建cookie对象
Cookie cookie=new Cookie("username","zs");
//2.发送cookie,response
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
打开的页面是空白页面,要想看到数据,则在浏览器中找到设置->安全与隐私–>cookie
package com.web.web2;
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("/bServlet")
public class BServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取cookie
//1.获取cookie数组
Cookie[] cookies=req.getCookies();
//2.遍历数组
//这个增强循环可以靠iter打出来
for (Cookie cookie : cookies) {
String name = cookie.getName();
if ("username".equals(name)){
//3.获取数据
String value = cookie.getValue();
System.out.println(name+":"+value);
break;
}
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
打开的页面网址是http://localhost:8080/javaweb/aServlet
后再打开新的页面,网址是http://localhost:8080/javaweb/bServlet
返回控制台看结果
2、Cookie原理
3、Cookie使用细节
PS:Tomcat8版本以上可以支持存储中文
package com.web.web2;
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("/aServlet")
public class AServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//发送cookie
//1.创建cookie对象
Cookie cookie=new Cookie("username","zs");
//设置存活时间 1周7天
cookie.setMaxAge(60*60*24*7);
//2.发送cookie,response
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
打开页面后,再关闭,再打开页面http://localhost:8080/javaweb/bServlet
控制台却能显示结果,说明结果已经被保存在浏览器中(可以联想到浏览器中的记住密码)
Seesion
1、基本使用
package com.web.web2.session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/ses1")
public class SessionDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//存储到Session中
//1.获取Session对象
HttpSession session = req.getSession();
//2.存储数据
session.setAttribute("username","zs");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
package com.web.web2.session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/ses2")
public class SessionDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据,从session中
//1.获取session对象
HttpSession session = req.getSession();
//2.获取数据
Object username = session.getAttribute("username");
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
ses1和ses2各执行一遍,看控制台
2、Session原理
如何验证使用的同一个session,很简单,在两个文件中都加入System.out.println(session);
3、Session使用细节
这个销毁配置`webapp.xml文件里配置,单位为分钟,默认是30分钟
package com.web.web2.session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/ses2")
public class SessionDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取数据,从session中
//1.获取session对象
HttpSession session = req.getSession();
//销毁
session.invalidate();
//2.获取数据
Object username = session.getAttribute("username");
System.out.println(username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}