Cookie和session的区别

本文阅读 1 分钟
首页 代码,Java 正文

1.定义

img

2.Cookie的存活时间

img

3.Cookie存储中文

img

4.Cookie基本用法

img

package com.jkj.web.cookie;

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;
import java.net.URLEncoder;

@WebServlet("/first")
public class FirstServlet extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        //创建cookie
        Cookie cookie = new Cookie("userName","jack");
        String value="小马哥";
        //url编码
        URLEncoder.encode(value,"utf-8");
        Cookie cookie1 = new Cookie("userName",value);
        //设置存活时间 60秒
        //cookie.setMaxAge(60);
        //发送cookie,response
        resp.addCookie(cookie);
        resp.addCookie(cookie1);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        this.doGet(req, resp);
    }
}


package com.jkj.web.cookie;

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;
import java.net.URLDecoder;

@WebServlet("/second")
public class SecondServlet extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        //获取cookie
        //获取cookie数组
        Cookie[] cookies = req.getCookies();
        //遍历数组
        for (Cookie cookie : cookies) { 
            String name = cookie.getName();
            if ("userName".equals(name)){ 
                String value = cookie.getValue();
                //解码
                String values = URLDecoder.decode(value, "utf-8");
                //获取数据
                System.out.println(name+":"+values);
                //结束循环
                break;
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        this.doGet(req, resp);
    }
}

1.Session活化

img

2.Session的使用

img

3.Session的销毁

img

4.Session实例

package com.jkj.web.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("/test1")
public class SessionTest1 extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        //获取session对象
        HttpSession session = req.getSession();
        //存储数据
        session.setAttribute("key1","jack");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        this.doGet(req, resp);
    }
}


package com.jkj.web.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("/test2")
public class SessionTest2 extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        //获取session对象
        HttpSession session = req.getSession();
        //接收数据
        Object value = session.getAttribute("key1");
        System.out.println(value);  //jack
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        this.doGet(req, resp);
    }
}

img

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125335295
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复