posts - 397,comments - 0,views - 25332

Cookie能不能存中文?

  在tomcat 8 之前 Cookie中不能直接存储中文数据

     需要将中文数据转码--- 一般采用url编码(%E3)

  在Tomcat 8 之后, cookie支持中文数据。

 

复制代码
@WebServlet("/CookieDemo5")
public class CookieDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie1 = new Cookie("msg","你好");
        response.addCookie(cookie1);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

会话技术_Cookie_细节4_Cookie共享

假设在一个tomcat服务器中,部署了多个web项目,那么在这些web项目中cookie能不能共享?  

  默认情况下cookie不能共皁

setPath(string path):设置cookie的获取范围。默认情况下,设置当前的虚拟目录

  如果要共享,则可以将path设置为”/"

不同的tomcat服务器间cookie共享问题?

   setDomain(String path):如果设置一级域名相同,那么多个服务器之间cookie可以共享

     setDomain(".baidu.com"),那么tieba.baidu.com和news.baidu.com中cookie可以共享

 

复制代码
@WebServlet("/CookieDemo5")
public class CookieDemo5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie cookie1 = new Cookie("msg","你好");
        cookie1.setPath("/");
        response.addCookie(cookie1);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
复制代码

 

复制代码
@WebServlet("/CookieDemo3")
public class CookieDemo3 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Cookie cookie1 = new Cookie("msg","hello");
        Cookie cookie2 = new Cookie("name","zhangsan");
        response.addCookie(cookie1);
        response.addCookie(cookie2);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
复制代码

 

 

posted on   淤泥不染  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示