cookie 是保存在用户浏览器的 把用户的信息写给用户浏览器保存,保存的比较多
session 保存在服务器中,是服务器写给用户的,保存的比较少
服务器写cookie
Cookie cookie = new Cookie(“lastLoginTime”, System.currentTimeMillis() + “”);
resp.addCookie(cookie);
@WebServlet("/cookieTime_Servlet")
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 out = resp.getWriter();
Cookie[] cookies = req.getCookies();
if (cookies != null) {
out.write("你上一次访问的时间是: ");
for (int i = 0; i <cookies.length ; i++) {
Cookie cookie=cookies[i];
if(cookie.getName().equals("lastLoginTime")){
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
} else {
out.write("这是您第一次访问本站");
}
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
resp.addCookie(cookie);
}
服务器清除用户浏览器的cookie
cookie.setMaxAge(0);//cooki生命周期为0
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Cookie cookie=new Cookie("lastLoginTime",System.currentTimeMillis()+"");
cookie.setMaxAge(0);//cooki生命周期为0
resp.addCookie(cookie);
}
服务器写session保存信息
session.setAttribute(“name”, “name”);
@WebServlet("/s1")
public class sessionDemo1 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.setAttribute(“name”, “name”);
//获得sessionId
String sessionId = session.getId();
//判断是不是新创建的session
if (session.isNew()) {
resp.getWriter().write("session创建成功,ID: "+sessionId);
}else {
resp.getWriter().write("session已经在服务器中存在了,ID: "+sessionId);
}
}
服务器读取session信息
session.getAttribute(“name”);
@WebServlet("/s2")
public class sessionDemo2 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();
String name = (String) session.getAttribute(“name”);
System.out.println(name);
}
服务器移除session信息
session.removeAttribute(“name”);
session.invalidate();//并且销毁
@WebServlet("/s3")
public class sessionDemo3 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.removeAttribute(“name”);
session.invalidate();
}
在web.xml也可以设置session生命周期
<?xml version="1.0" encoding="UTF-8"?><session-config>
<session-timeout>1</session-timeout>
</session-config>