一、cookie的操作

对Cookie的操作无外乎三部分:读、分析、写。

写Cookie

Cookie theCookie = new Cookie(“username” , “Tom”);

response.addCookie(theCookie);

当Servlet向客户端写Cookie时,还可以通过Cookie类的setMaxAge(intexpiry)方法来设置Cookie的有效期。参数expiry以秒为单位,它具有以下含义:

·如果expiry大于零,就指示浏览器在客户端硬盘上保存Cookie的时间为expriy秒。

·如果expiry等于零,就指示浏览器删除当前Cookie。

·如果expiry小于零,就指示浏览器不要把Cookie保存到客户端硬盘。Cookie仅仅存在于当前浏览器进程中,当浏览器进程关闭,Cookie也就消失。

Cookie默认的有效期为-1。对于来自客户端的Cookie,Servlet可以通过Cookie类的getMaxAge()方法来读取Cookie的有效期。

读取分析客户端Cookie

Cookie[] cookies = request.getCookies();

HttpServletRequest类的getCookies()方法返回一个Cookie数组,它包含了HTTP请求中的所有Cookie。如果在HTTP请求中没有任何Cookie,那么getCookies()方法返回null。

对于每个Cookie对象,可调用getName()方法来获得Cookie的名字,调用getValue()方法来获得Cookie的值。

 

二、常用的Cookie类方法

String getName()   用于返回Cookie的名称

void setValue(String name)    用于设置Cookie的值

String getName()    用于返回Cookie的值

void setMaxAge(int expiry)    用于设置Cookie在浏览器客户端上保存的秒数

int getMaxAge()     用于获取Cookie在浏览器客户端上保存的秒数

void setPath(String uri)    用于设置该Cookie项的有效目录路径

String getPath()    用于获取该Cookie项的访问目录路径

void setDomain(String pattern)    用于设置该Cookie项的有效域

String getDomain()    用于返回该Cookie的项有效域

void setVersion(int v)    用于设置该Cookie采用的项协议版本

int getVersion()    用于返回该Cookie项采用的协议版本

void setComment(String purpose)    用于设置该Cookie项的注解部分

String getComment()    用于返回该Cookie项的注解部分

void setSecure(boolean flag)    用于设置该Cookie项是否只能采用安全协议传送

boolean getSecure()    用于返回该Cookie项是否只能使用安全的协议传送

 

三、Cookie案例(1)——显示用户上次访问时间

(1)新建 LastAcessServlet 类

老规矩,还是在 Servlet 项目下,新建 com.liuyanzhao 包,然后新建LastAcessServlet.java




    1. package
    2. import
    3. import
    4. import
    5. 
    6. import
    7. import
    8. import
    9. import
    10. import
    11. 
    12. import
    13. 
    14. /*
    15.  * @author LiuYanzhao 
    16.  */
    17. 
    18. public class LastAcessServlet extends
    19. private static final long
    20. public void doGet(HttpServletRequest request,HttpServletResponse response) throws
    21. //指定服务器输出内容的编码方式为UTF-8,防止乱码
    22. "text/html;charset=utf-8");
    23. /*
    24.          * 设定与个人cookie的name为:lastAcessTime
    25.          * 读取客户端发送的cookie获得用户上次访问的时间显示
    26.          */
    27. null;
    28. //获取所有cookie,并将这些cookie存放在数组中
    29.         Cookie[] cookies = request.getCookies();
    30. for(int i=0;cookies!=null
    31. if("lastAcess".equals(cookies[i].getName())) {
    
    32. //如果cookie名称为lastAcess,则获取该cookie的值
    33.                 lastAcessTime = cookies[i].getValue();
    34. break;
    35.             }
    36.         }
    37. //判断是否存在名称为lastAcess的cookie
    38. if(lastAcessTime==null) {
    
    39. "您是首次访问本站");
    40. else
    41. "您上次访问本站的时间是:"+lastAcessTime);
    42.         }
    43. //创建cookie,将当前时间作为cookie的值发送给客户端
    44. new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new
    45. new Cookie("lastAcess", currentTime);
    46. 60*60);
    47. //发送cookie
    48.         response.addCookie(cookie);
    49. 
    50.     }
    51. 
    52. protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws
    53. this.doPost(req, resp);
    54.     }
    55. }


    (2在 web.xml 里添加 LastAcessServlet 映射





    1. <servlet>
    2. <servlet-name>LastAcessServlet</servlet-name>
    3. <servlet-class>com.liuyanzhao.LastAcessServlet</servlet-class>
    4. </servlet>
    5. <servlet-mapping>
    6. <!-- 映射为 LastAcessServlet -->
    7. <servlet-name>LastAcessServlet</servlet-name>
    8. <url-pattern>/LastAcessServlet</url-pattern>
    9. </servlet-mapping>


    (3)重启 Tomcat , 打开浏览器

    ①在浏览器输入:http://localhost:8080/ServletTest/LastAcessServlet

    java设置response header java设置cookie过期时间_HTTP

    ②刷新浏览器或者关闭浏览器,重新输入刚才的 url

    java设置response header java设置cookie过期时间_客户端_02