JavaWeb-Cookie和Session
cookie:存放在客户端
session:存放在服务端
cookie
package com.th0r.servlet;
import javax.servlet.ServletException;
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;
public class Demo1 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");
if (req.getCookies() != null){
resp.getWriter().print("你上次访问的时间是:");
for (Cookie cookie : req.getCookies()) {
if (cookie.getName().equals("lastLogin")){
long l = Long.parseLong(cookie.getValue());
Date date = new Date(l);
resp.getWriter().print(date.toString());
}
}
} else {
resp.getWriter().println("这是你第一次访问");
}
//设置cookie
Cookie cookie = new Cookie("lastLogin", System.currentTimeMillis() + "");
//设置cookie有效期
cookie.setMaxAge(2*60*60);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
第一次访问
第二次访问
- 一个cookie只能保存一个信息
- 一个web站点可以发送多个cookie,最多存放20个
- cookie的大小限制为4kb
- 浏览器的cookie上限为300个
删除cookie的方法
- 不设置有效期,关闭浏览器,自动失效
- 设置有效期为0
Session
- 服务器会给每一个用户(浏览器)创建一个Seesion对象
- 一个Seesion独占一个浏览器,只要浏览器没有关闭,这个Session就存在
- 用户登录之后,整个网站它都可以访向
创建Session
package com.th0r.servlet;
import com.th0r.pro.Person;
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;
public class Demo3 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");
HttpSession session = req.getSession();
session.setAttribute("name", new Person("Bob", 18));
String sessionId = session.getId();
if (session.isNew()){
resp.getWriter().write("Session 创建成功" + sessionId);
} else {
resp.getWriter().write("Session 已存在" + sessionId);
Person person = (Person)session.getAttribute("name");
resp.getWriter().print("\n"+person);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
销毁session
package com.th0r.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;
public class Demo4 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");
HttpSession session = req.getSession();
if (session.getAttribute("name") != null){
session.removeAttribute("name");
session.invalidate();
resp.getWriter().println("注销成功!");
}else {
resp.getWriter().println("未登录!");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
设置时间
web.xml
<session-config>
<session-timeout>15</session-timeout>
</session-config>