Servlet设置Cookie

Posted acgame

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet设置Cookie相关的知识,希望对你有一定的参考价值。

技术分享图片

 

Cookie_Servlet.java:

package servlet_pack;

import java.io.IOException;
import java.io.PrintWriter;

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;

/**
 * Servlet implementation class Cookie_Servlet
 */
@WebServlet("/Cookie_Servlet")
public class Cookie_Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Cookie_Servlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie c = new Cookie("username", "admin"); // 创建一个Cookie,name=username,value=admin
        c.setMaxAge(60*60*24); // 设置Cookie的存活时间,单位是秒
        response.addCookie(c); // 将Cookie添加到响应中
        
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("<!DOCTYPE html>");
        out.print("<html>");
        out.print("<head>");
        out.print("<title>A Servlet</title>");
        out.print("</head>");
        out.print("<body>");

        out.print("Cookie set success");
        
        out.print("</body>");
        out.print("</html>");
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

TestCookie.java:

 1 package servlet_pack;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.annotation.WebServlet;
 8 import javax.servlet.http.Cookie;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 /**
14  * Servlet implementation class TestCookie
15  */
16 @WebServlet("/TestCookie")
17 public class TestCookie extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19        
20     /**
21      * @see HttpServlet#HttpServlet()
22      */
23     public TestCookie() {
24         super();
25         // TODO Auto-generated constructor stub
26     }
27 
28     /**
29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
30      */
31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
32         Cookie[] cs = request.getCookies(); // 拿到Cookie数组
33         
34         response.setContentType("text/html");
35         PrintWriter out = response.getWriter();
36         out.print("<!DOCTYPE html>");
37         out.print("<html>");
38         out.print("<head>");
39         out.print("<title>A Servlet</title>");
40         out.print("</head>");
41         out.print("<body>");
42 
43         if(cs != null && cs.length > 0){
44             for(int i = 0; i < cs.length; i++){
45                 Cookie c = cs[i]; // 拿到Cookie
46                 out.print(c.getName() + ":" + c.getValue()); // 输出cookie的名字和值
47                 
48             }
49         }
50         
51         out.print("</body>");
52         out.print("</html>");
53     }
54 
55     /**
56      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
57      */
58     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
59         // TODO Auto-generated method stub
60         doGet(request, response);
61     }
62 
63 }

运行结果:

技术分享图片

 

技术分享图片

 

以上是关于Servlet设置Cookie的主要内容,如果未能解决你的问题,请参考以下文章

Servlet的Cookie值保存与获取

Cookie工具类(获取cookie,设置cookie)

cookie 设置 httpOnly属性

cookie 设置 httpOnly属性

Cookie实现记住密码代码

5.Servlet的Cookie和Session