cookie,session
会话:用户打开一个浏览器,点击了很多按钮,访问多个web资源,关闭浏览器,这个过程可以称为会话。
客户端 服务端
- 1.服务端给客户端一个信件,客户端下次访问服务器的时候带上信件就可以了;cookie
- 2.服务器登记你来过,下次来的时候我来匹配你;session
保存客户端信息的两种方式:
- cookie
- 客户端技术:请求和响应
- session
- 服务器端技术,保存客户端信息,把信息或者数据放在session中
package com.javaweb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
/**
* @author panglili
* @create 2022-07-19-19:39
*/
public class Cookie extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter writer = resp.getWriter();
javax.servlet.http.Cookie[] cookies = req.getCookies();
if(cookies!=null){
writer.write("time:");
for(int i=0;i<cookies.length;i++){
javax.servlet.http.Cookie cookie=cookies[i];
if(cookie.getName().equals("lastLoginTime")){
long lastLoginTime=Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
System.out.println("*******");
writer.write(date.getDate());
writer.write(date.toLocaleString());
}
System.out.println("$$$$$$$$");
}
}else{
writer.write("first inter");
}
javax.servlet.http.Cookie cookie1;
cookie1 = new javax.servlet.http.Cookie("lastLoginTime", System.currentTimeMillis()+"");
//cookie1.setMaxAge(24*60*60);
resp.addCookie(cookie1);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
session实现:
- 服务器给每个用户创建一个session对象。
- 一个session独占一个浏览器,只要浏览器没关,session就可以存在
package com.javaweb.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* @author panglili
* @create 2022-07-19-20:32
*/
public class Session extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
//给session存值,可以通过其他session获取
session.setAttribute("name","heihe");
//获取session的id
String id = session.getId();
//判断是不是第一次创建
if(session.isNew()){
resp.getWriter().write("session创建成功"+id);
}else{
resp.getWriter().write("session已经存在"+id);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<servlet>
<servlet-name>session</servlet-name>
<servlet-class>com.javaweb.servlet.Session</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>session</servlet-name>
<url-pattern>/session</url-pattern>
</servlet-mapping>
<!--session失效时间-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>