Servlet中请给出一个Cookie的增删改查的例子

Servlet与Cookie:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。
马克-to-win:Cookie有点像Session。Session是把键值对存在服务器端,一个Servlet存值,另外一个Servlet可以取值。Cookie也是以键值对的形式用于读取,不过是保存在客户端浏览器的某个文本里面。取时,也要从这台机器的这个浏览器上去取。像Session一样,你也可以设置过期时间,比如“一年”。和Session不同的是:用户可以把自己浏览器的Cookie工作系统关掉。这就是Cookie不如Session 的重要的原因。不可靠,不保险。程序员编的程序都白费了。另外,对于Cookie来讲,servlet只能拿回属于自己整个Web应用的Cookie(别人的Web应用不行)。当然了,Session范围更小,只能拿回自己用户浏览器写过的东西。



马克-to-win:底下,我就给出一个Cookie的增删改查的例子。只需运行cookie.html。这个html带动四个增删改查Servlet。读者可以先增加Cookie,之后查询一下,再删除,再查询一下。反正自己研究研究这个例子。实验使用ie8做的,cookie的查找在例子当中。



注意此文件不能直接打开,只能拷贝到别的目录下,之后用记事本打开。我就这样打开后,给大家看一下。

mark-to-win0
yes0
localhost/ServletHello/
1024
2493888000
30731375
472266800
30657950
*
mark-to-win
yes
localhost/ServletHello/
1024
2172629504
30657951
472376800
30657950
*

执行了下面的delete之后,变成了:
mark-to-win0
yes0
localhost/ServletHello/
1024
414149632
30731383
2685375728
30657957
*





例:4.2.1:

cookie.html:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cookie Test</title>
</head>
<body>
<FORM ACTION="AddCookie" METHOD="POST">
add<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM><br>
<FORM ACTION="DeleteCookie" METHOD="POST">
delete<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM><br>
<FORM ACTION="QueryCookie" METHOD="POST">
Query<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM><br>
<FORM ACTION="ModifyCookie" METHOD="POST">
Modify<INPUT TYPE="SUBMIT" VALUE="Submit">
</FORM><br>
</body>
</html>



package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddCookie extends HttpServlet {
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Cookie returnVisitorCookie0 = new Cookie("mark-to-win0", "yes0");
        Cookie returnVisitorCookie = new Cookie("mark-to-win", "yes");
        Cookie returnVisitorCookie1 = new Cookie("mark-to-win1", "yes1");
        /* public void setMaxAge(int expiry) Sets the maximum age of the cookie
         * in seconds. A positive value indicates that the cookie will expire
         * after that many seconds have passed. A negative value means that the
         * cookie is not stored persistently and it won't be recorded in the
         * file. will be deleted when the Web browser exits. A zero value causes
         * the cookie to be deleted. By default, the value is -1, -1 indicating
         * the cookie will persist until browser shutdown. in the cookie file,
         * if the value is negative, it won't be recorded in the file. As long
         * as the value is positive, itself and its expiry time will be recorded
         * in the file. After that moment, that cookie expires, but that cookie
         * is not deleted from the file, any way, there is schedule to expire in
         * the file.另外,当你设置cookie时长为0,删除它后,如文件中所有cookie都被你用这种方法删除后,文件也会被ie自动删掉。你如果想删 cookie文件,正常的得通过ie8设置中的删除钮。如果想看cookie文件,得拷贝文件到其他地方,之后用记事本看。*/
        returnVisitorCookie0.setMaxAge(60 * 60 * 24 * 365); // 1 year
        returnVisitorCookie.setMaxAge(10 * 60 * 1);
        response.addCookie(returnVisitorCookie0);
        response.addCookie(returnVisitorCookie);
        response.addCookie(returnVisitorCookie1);    
        response.sendRedirect("cookie.html");
    }
}






package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DeleteCookie extends HttpServlet {
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Cookie returnVisitorCookie0 = new Cookie("mark-to-win0", null);
        Cookie returnVisitorCookie = new Cookie("mark-to-win", null);
        Cookie returnVisitorCookie1 = new Cookie("mark-to-win1", null);

        returnVisitorCookie0.setMaxAge(0);
        returnVisitorCookie.setMaxAge(0);
        returnVisitorCookie1.setMaxAge(0);
//        response.addCookie(returnVisitorCookie0);
        response.addCookie(returnVisitorCookie);
        response.addCookie(returnVisitorCookie1);
        response.sendRedirect("cookie.html");
    }
}




package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ModifyCookie extends HttpServlet {
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        Cookie returnVisitorCookie0 = new Cookie("mark-to-win0", "no0");
        Cookie returnVisitorCookie = new Cookie("mark-to-win", "no");
        Cookie returnVisitorCookie1 = new Cookie("mark-to-win1", "no1");
        returnVisitorCookie0.setMaxAge(60 * 60 * 24 * 365); // 1 year
        returnVisitorCookie.setMaxAge(10 * 60 * 1);
        response.addCookie(returnVisitorCookie0);
        response.addCookie(returnVisitorCookie);
        response.addCookie(returnVisitorCookie1);
        response.sendRedirect("cookie.html");
    }
}








package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class QueryCookie extends HttpServlet {
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        /* public Cookie[] getCookies() Returns an array containing all of the
         * Cookie objects the client sent with this request. This method returns
         * null if no cookies were sent. if there is no the file of cookie,
         * certainly, the cookie is null.ie8 当中,Cookie file is where?/Tool/internet
         * option/Setting/View Files/Order according to last visit.记住一定要先刷新一下,这样刚生成的cookie才能被找到。 find a file:
         * Cookie:administrator@localhost/WebModule1/ the file content is
        
         */
        Cookie[] cookies = request.getCookies();
        /*the following paragraph sometimes print 3 cookies(length is 3),
         * sometimes print 2 cookies, (length is 2) if after 30 seconds,
         * returnVisitorCookie won't come out, if you close the ie,
         * returnVisitorCookie1 won't come out because it is just a session.
         */
        /* if (cookies != null) { System.out.println("length is " +
         * cookies.length); for (i = 0; i < cookies.length; i++) { Cookie c =
         * cookies[i]; System.out.println(c.getName()+":"+c.getValue()); }
         *
         * }
         */
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie c = cookies[i];
                System.out.println(c.getName() + c.getValue());
            }
        } else {
            System.out.println("there is no cookie");
        }
        response.sendRedirect("cookie.html");
    }
}