首页 > 其他分享 >cookie

cookie

时间:2022-08-24 16:57:52浏览次数:57  
标签:cookies ServletException resp req cookie doGet

cookie:将数据储存在客户端。 session:将数据储存在服务端,较安全

 

 携带sevlet a 的数据,来访问sevlet b。





@WebServlet("/a")
public class se extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ServletException {
//super.doGet(req, resp);
// List<brands> brand=new ArrayList<>();
// brand.add(new brands(1,"三只松鼠","三只松鼠"));
// brand.add(new brands(2,"优衣库","优衣库"));
//
// req.setAttribute("brands",brand);
//
// req.getRequestDispatcher("src/main/webapp/index.jsp").forward(req,resp);
Cookie cookies=new Cookie("us","dd");
resp.addCookie(cookies);
}




@WebServlet("/b")
public class s extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doGet(req, resp);
Cookie[] cookies=req.getCookies();
for(Cookie cookie:cookies){
String name=cookie.getName();
if("us".equals(name)){
String value=cookie.getValue();
System.out.println(name+value);
break;
}

}
}

细节:储存中文即cookie的存活时间
@WebServlet("/a")
public class se extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, ServletException {
Cookie cookies=new Cookie("us","dd");
cookies.setMaxAge(60*60*24*7);//设置cookie的存活时间,即记住数据多久
String value="张三";
value= URLEncoder.encode(value,"UTF-8");//储存中文
System.out.println(value);
resp.addCookie(cookies);
}



session

 

 

@WebServlet("/session1")
public class session1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//super.doGet(req, resp);
HttpSession session=req.getSession();
session.setAttribute("username","doudou");

}



@WebServlet("/session2")
public class session2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// super.doGet(req, resp);
HttpSession session=req.getSession();
Object username=session.getAttribute("username");
System.out.println(username);

}

标签:cookies,ServletException,resp,req,cookie,doGet
From: https://www.cnblogs.com/doudou666/p/16620713.html

相关文章

  • HTTP cookie
    Asessioncookie,alsoknownasanin-memorycookie,transientcookieornon-persistentcookie,existsonlyintemporarymemorywhiletheusernavigatesthewe......
  • cookie
    Acookieisabakedorcookedfoodthatistypicallysmall,flatandsweet.Itusuallycontainsflour,sugarandsometypeofoilorfat.Itmayincludeother......
  • Javaweb-Cookie初识
    快速了解Cookie什么是Cookie?首先看一下Cookie在目前整个Web技术体系中的位置可以看到它是在浏览器端这边关联服务器端Session会话域的,可以推断它是和Session类似,用来保......
  • Express 项目,res.cookie() 设置 Cookie 无法被保存在浏览器的 Application 中
    res.cookie()给客户端响应头封装的Cookie无法被保存在客户端浏览器的Application中,只能在Set-Cookie中看到有这个值:在前后端分离项目中,存在跨域问题,导致Cookie......
  • django中的cookie与session
    发展史1.早期的时候,网站都没有保存用户功能的需求,所有用户访问网站返回的结果都是一样的,比如新闻、文章等网站!2.但是,随着网站的发展,出现了一些需要保存用户信息的网站,比......
  • 大家都能看得懂的源码 - 如何封装 cookie/localStorage/sessionStorage hook?
    本文是深入浅出ahooks源码系列文章的第九篇,该系列已整理成文档-地址。觉得还不错,给个star支持一下哈,Thanks。今天来看看ahooks是怎么封装cookie/localStorage/sess......
  • 12-Cookie
    12-Cookie概述终于学到了最想了解的Cookie和Session了,其实这些概念很简单,,强烈推荐狂神说的视频把Cookie讲的十分简单Cookie,是曲奇饼思考一下,学校如何知道你是这个学......
  • Js的cookie和session
    Cookie的概念:保存在客户端一个字符串属性,读取cookie的值时,得到一个字符串;cookie除了name名和value值外,还有expires过期时间、path路径、domain域、以及secure安全。JS代码......
  • java 携带session 前台传递cookie 跨域解决方案 vue + java
    前台axios设置withCredentials:true后台设置header("Access-Control-Allow-Origin","源地址";header("Access-Control-Allow-Credentials","true");这里源地址......
  • cookie、localStorage和sessionStorage的区别
    都是一种存储机制 cookielocalStoragesessionStorage大小4kb10Mb(同步执行,太大会影响渲染进度)5Mb兼容H4/H5H5H5访问任何窗口任何窗口同一窗口......