Servlet中請給出一個Cookie的增刪改查的例子

Servlet與Cookie:
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
馬克-to-win:Cookie有點像Session。Session是把鍵值對存在服務器端,一個Servlet存值,另外一個Servlet可以取值。Cookie也是以鍵值對的形式用于讀取,不過是保存在客戶端瀏覽器的某個文本里面。取時,也要從這臺機器的這個瀏覽器上去取。像Session一樣,你也可以設置過期時間,比如“一年”。和Session不同的是:用戶可以把自己瀏覽器的Cookie工作系統(tǒng)關掉。這就是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
*

執(zhí)行了下面的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");
    }
}