Cookie的簡易用法

Cookie

Cookie是客戶端技術,程序把每一個用戶的數據以cookie的形式寫給用戶各自的瀏覽器。當用戶使用瀏覽器再去訪問服務器中的web資源時,就會帶着各自的數據去。這樣,web資源處理的就是用戶各自的數據了。java

1.在java中增長一個cookieweb

Cookie cookie = new Cookie("mytest","123456");
response.addCookie(cookie);

2.在java中修改一個cookie瀏覽器

Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies) {
    if("mytest".equals(cookie.getName())) {
        cookie.setValue("mytestNEW");
        response.addCookie(cookie);
    }
}

3.怎麼在java中刪除一個cookie服務器

Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies) {
    if("mytest".equals(cookie.getName())) {
        cookie.setMaxAge(0);
        response.addCookie(cookie);
    }
}

4.怎麼在java中顯示cookie列表cookie

Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies) {
    try {
        response.getWriter().println(cookie.getName() + "-->" +   cookie.getValue());
    } catch (IOException e) {
        e.printStackTrace();
    }

}

5.怎麼在java中增長一箇中文cookie測試

Cookie cookie;
try {
    cookie = new Cookie("mytest",URLEncoder.encode("個人測試", "UTF-8"));
    response.addCookie(cookie);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

6.怎麼在java中顯示中文cookie值spa

Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies) {
    if("mytest".equals(cookie.getName())) {
        try {      response.getWriter().println(URLDecoder.decode(cookie.getValue(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

7.怎麼在java中根據cookie名稱得到cookie值code

Cookie[] cookies = request.getCookies();
for(Cookie cookie : cookies) {
    if("mytest".equals(cookie.getName())) {
        try {
            response.getWriter().println(cookie.getName() + "-->" + cookie.getValue());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

8.怎麼在java中設置cookie路徑blog

Cookie cookie1 = new Cookie("mytest","mytest1");
cookie1.setPath("/test1");
Cookie cookie2 = new Cookie("mytest","mytest2");
cookie2.setPath("/test2");
response.addCookie(cookie1);
response.addCookie(cookie2);

9.怎麼在java中設置cookie過時時間爲60秒資源

Cookie cookie = new Cookie("mytest","mytest1");
cookie.setMaxAge(60);
response.addCookie(cookie);

10.怎麼在java中設置cookie域名

Cookie cookie = new Cookie("mytest","mytest1");
cookie.setDomain("127.0.0.1");
response.addCookie(cookie);
相關文章
相關標籤/搜索