首页 > 其他分享 >会话技术_Cookie_细节2_Cookie存活时间与会话技术_Cookie_细节3_Cookie存储中文

会话技术_Cookie_细节2_Cookie存活时间与会话技术_Cookie_细节3_Cookie存储中文

时间:2023-02-09 11:25:02浏览次数:47  
标签:response 会话 细节 Cookie import cookie servlet javax

会话技术_Cookie_细节2_Cookie存活时间  

      

         1.一次可不可以发送多个cookie? 

         2.cookie在浏览器中保存多次时间?

            1.默认情况下。当浏览器关闭后,Cookie数据被销毁

            2.持久化存储:

                setMaxAge(int seconds)

                1.整数:将Cookie数据写到硬盘的文件中。持久化存储。cookie存活时间

                2.负数:默认值

                  3.零:删除cookie信息

package bd.xueqiang.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;

@WebServlet("/cookieDemo04")
public class CookieDemo04 extends HttpServlet {
    /**
     * Cookis快速入门
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.创建Cookie对象
        Cookie msg1 = new Cookie("msg", "setMaxAge");
        //2.设置cookie的存活时间
        //msg1.setMaxAge(30);//将cookie持久化到硬盘,30秒后会自动删除cookie文件
        // msg1.setMaxAge(-1);
        //msg1.setMaxAge(300);
        msg1.setMaxAge(0);
        //3.发送Cookis
        response.addCookie(msg1);



    }

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

 

         3.cookie能不能中文?

         4.cookie获取范围多大?   

          

会话技术_Cookie_细节3_Cookie存储中文

    

         1.一次可不可以发送多个cookie? 

         2.cookie在浏览器中保存多次时间?

         3.cookie能不能中文?

            1、在tomcat8之前 ,cookie中不能直接存储中文数据。

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

            2、在tomcat8之后 ,cookie支持中文数据。

package bd.xueqiang.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;

@WebServlet("/cookieDemo05")
public class CookieDemo05 extends HttpServlet {
    /**
     * Cookis快速入门
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.创建Cookie对象
        Cookie msg1 = new Cookie("msg", "萤火之光岂能与皓月争辉");

        //3.发送Cookis
        response.addCookie(msg1);



    }

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

 

                                                                                                                                                                                                                    

         4.cookie获取范围多大?   

 

标签:response,会话,细节,Cookie,import,cookie,servlet,javax
From: https://www.cnblogs.com/x3449/p/17104581.html

相关文章