JavaWeb - cookie/session的区别以及使用

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值