日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

java中cookie操作詳細(xì)_JSP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:java圖片處理類(圖片水印,圖片縮放)
可實(shí)現(xiàn)以下常用功能:縮放圖像、切割圖像、圖像類型轉(zhuǎn)換、彩色轉(zhuǎn)黑白、文字水

1.設(shè)置Cookie

 代碼如下 
Cookie cookie = new Cookie("key", "value");

  cookie.setMaxAge(60);
 

  設(shè)置60秒生存期,如果設(shè)置為負(fù)值的話,則為瀏覽器進(jìn)程Cookie(內(nèi)存中保存),關(guān)閉瀏覽器就失效。

 代碼如下

  cookie.setPath("/test/test2");
 

  設(shè)置Cookie路徑,不設(shè)置的話為當(dāng)前路徑(對(duì)于Servlet來說為request.getContextPath() + web.xml里配置的該Servlet的url-pattern路徑部分) 。

 代碼如下 
response.addCookie(cookie);
 

  2.讀取Cookie

  該方法可以讀取當(dāng)前路徑以及“直接父路徑”的所有Cookie對(duì)象,如果沒有任何Cookie的話,則返回null。

 

 代碼如下

 Cookie[] cookies = request.getCookies(); 

  3.刪除Cookie

 代碼如下 
Cookie cookie = new Cookie("key", null);

  cookie.setMaxAge(0);
 

  設(shè)置為0為立即刪除該Cookie;

 代碼如下 
cookie.setPath("/test/test2");
 

  刪除指定路徑上的Cookie,不設(shè)置該路徑,默認(rèn)為刪除當(dāng)前路徑Cookie;

  

 代碼如下 response.addCookie(cookie); 

下面用一個(gè)完整的實(shí)例來說明

 

 代碼如下 
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.3lian.com/">
<html xmlns="http://www.3lian.com/">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>

<body>
<%
    String username = null;
    Cookie[] cookies = request.getCookies();
    if(cookies!=null)
    {
        for(int i=0;i<cookies.length;i++)
        {
            if("cookies_user".equals(cookies[i].getName()))
            {
                username = cookies[i].getValue();//cookies_user}
        }
       
        if("onepc".equals(username))
        {
            out.println("Hello");
        }else
        {
        %>
       
<table width="302" border="1">
  <form id="form1" name="form1" method="post" action="clogin.jsp">
  <tr>

    <td width="79"><div align="center"></div></td>
    <td width="207"><input type="text" name="user" id="user" /></td>
  </tr>
  <tr>
    <td><div align="center"></div></td>
    <td><input type="text" name="textfield2" id="textfield2" /></td>
  </tr>
  <tr>
    <td><div align="center">     
    </div></td>
    <td><select name="select" id="select">
      <option value="31536000">one year</option>
      <option value="120">two min</option>
      </select></td>
  </tr>
  <tr>
    <td colspan="2"><label>
      <input type="submit" name="button" id="button" value="" />
    </label></td>

  </tr>
      </form>
</table>

        <%
        }
   
    }

%>


</body>
</html>
 


login.jsp

 代碼如下

<%
    String user = request.getParameter("user");
    Cookie cookie = new Cookie("cookies_user",user);
    cookie.setMaxAge(120);
    response.addCookie(cookie);
    response.sendRedirect("cindex.jsp");

%>
 


4.注意:假設(shè)路徑結(jié)構(gòu)如下

  

 代碼如下 
test/test2/test345/test555/test666 

  a.相同鍵名的Cookie(值可以相同或不同)可以存在于不同的路徑下。

分享:jsp switch語句的用法
如果希望選擇執(zhí)行若干代碼塊中的一個(gè),你可以使用switch語句: 語法: switch(n) { case 1: 執(zhí)行代碼塊 1 break case 2: 執(zhí)行代碼塊 2 break default: 如果n即不是1也不是2,則執(zhí)行此代碼 } 工作原理:switch后面的(n)可以是表達(dá)式,也可以(并通常)是變量。然后表達(dá)

共2頁上一頁12下一頁
來源:模板無憂//所屬分類:JSP教程/更新時(shí)間:2013-04-17
相關(guān)JSP教程