当前位置:编程学习 > JAVA >>

深入体验JavaWeb开发内幕——由创建Cookie到Cookie的简单应用

[java] 
<pre name="code" class="java">MakeCookie .java</pre><br> 
package net.csdn.cookie;import java.io.IOException;<br> 
import java.io.PrintWriter;import java.util.Date;<br> 
import javax.servlet.ServletException;import javax.servlet.http.Cookie;<br> 
import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;<br> 
import javax.servlet.http.HttpServletResponse;<br> 
public class MakeCookie extends HttpServlet <br> 
{<br> 
public void doGet(HttpServletRequest request, HttpServletResponse response)<br> 
throws ServletException, IOException {<br> 
 makeCookie(response);}private void makeCookie(HttpServletResponse response) <br> 
throws IOException {<br> 
//为客户端指定显示编码<br> 
  response.setContentType("text/html;charset=utf-8");PrintWriter out = response.getWriter();<br> 
//创建一个Cookie实例名字为"username",值为"lijizh1013"Cookie cookie = new Cookie("username","lijizh1013");<br> 
//为Cookie设置存活时间,默认的为到该浏览器关闭<br> 
 cookie.setMaxAge(24*3600);<br> 
//为Cookie设置路径<br> 
 cookie.setPath("/Cookie/MakeCookie");<br> 
//向浏览器写入Cookie<br> 
 response.addCookie(cookie);out.println("Cookie已经写入到客户端!");<br> 
}<br> 
public void doPost(HttpServletRequest request, HttpServletResponse response<br> 
)throws ServletException, IOException {doGet(request, response);}} 
<pre></pre> 
<p>效果:</p> 
<p>在浏览器发送请求前没有任何cookie文件</p> 
<p><img src=/2012/1030/20121030100654484.png" alt="" height="112" width="734"></p> 
<p>发送请求后:</p> 
<p><img src=/2012/1030/20121030100654100.png" alt=""></p> 
<p><img src=/2012/1030/20121030100654917.png" alt=""></p> 
<p>获取Cookie的值:</p> 
<p></p> 
<pre name="code" class="java">getCookie(request, response);</pre><br> 
具体如下: 
<p></p> 
<p></p> 
<pre name="code" class="java">private void getCookie(HttpServletRequest request, 
            HttpServletResponse response) throws IOException { 
              为浏览器设置显示编码 
               response.setContentType("text/html;charset=utf-8"); 
        PrintWriter out = response.getWriter(); 
           //通过  request.getCookies()返回一个Cookie数组对数组进行遍历得到具体的Cookie后,通过Cookie对象的getName()和getValue()方法得到Cookie的 
          //名和值 
                Cookie [] cookies = request.getCookies(); 
        for(int i=0;cookies!=null && i<cookies.length;i++){ 
            String cname = cookies[i].getName(); 
            String cvalue = cookies[i].getValue(); 
            out.println("获取Cookie"+cname+"——"+cvalue); 
        }</pre>效果如图: 
<p></p> 
<p><img src=/2012/1030/20121030100654152.png" alt=""></p> 
<p>一个关于获取用户访问时间的实例:</p> 
<p></p> 
<pre name="code" class="java">response.setContentType("text/html;charset=utf-8"); 
        PrintWriter out = response.getWriter(); 
        out.println("你上次访问的时间是:"); 
        Cookie [] cookies = request.getCookies(); 
        for(int i=0;cookies!=null && i<cookies.length;i++){ 
            if(cookies[i].getName().equals("LastAccessName")){ 
                long time  = Long.valueOf(cookies[i].getValue()); 
                Date date = new Date (time); 
                out.println(date.toLocaleString()); 
            } 
         
             
        } 
        Cookie cok = new Cookie("LastAccessName",System.currentTimeMillis()+""); 
        cok.setMaxAge(1*3600); 
        response.addCookie(cok);</pre><br> 
效果如图: 
<p></p> 
<p>当用户第一访问时:</p> 
<p><img src=/2012/1030/20121030100654315.png" alt=""></p> 
<p>下一次再访问时:</p> 
<p><img src=/2012/1030/20121030100654606.png" alt=""></p> 
<p>再次访问时:</p> 
<p><img src=/2012/1030/20121030100654284.png" alt=""></p> 
<p>这样就可以通过Cookie得知浏览器上次访问的时间了,赶快自己试一试吧!<br> 
</p> 
<br> 
<pre></pre> 
<pre></pre> 
<pre></pre> 
补充:Web开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,