Servlet Cookies处理(二)

Servlet Cookies处理(二)

通过Servlet读取Cookies
要读取Cookies,你需要通过调用HttpServletRequest的getCookies()方法创建一个javax.servlet.http.Cookie对象的数组。然后循环遍历数组,并使用getName()和getValue()方法来访问每个cookie和关联的值。
读取上一章Servlet Cookies处理的实例中设置的Cookies

// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// 扩展 HttpServlet 类
public class ReadCookies extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      Cookie cookie = null;
	  Cookie[] cookies = null;
      // 获取与该域相关的 Cookies 的数组
      cookies = request.getCookies();    
	  // 设置响应内容类型
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String title = "Reading Cookies Example";
      String docType =
      "<!doctype html public \"-//w3c//dtd html 4.0 " +
      "transitional//en\">\n";
      out.println(docType + "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" );
      if( cookies != null ){
         out.println("<h2>查找 Cookies 名称和值</h2>");
         for (int i = 0; i < cookies.length; i++){
            cookie = cookies[i];
            out.print("名称:" + cookie.getName( ) + ",");
            out.print("值:" + cookie.getValue( )+" <br/>");
         }
      }else{
          out.println("<h2 class="tutheader">未找到 Cookies</h2>");
      }
      out.println("</body>");
      out.println("</html>");
   }
}

编译上面的Servlet ReadCookies,并在web.xml文件中创建Servlet对应的位置,如果你已经设置了first_name cookie为John,last_name cookie为Player,尝试运行http://localhost:8080/ReadCookies,将显示如下结果:
在这里插入图片描述

通过Servlet删除Cookies
删除Cookies是非常简单的,你如果想删除一个cookie,那么就需要按以下三个步骤运行:
1、 读取一个现有的cookie,并把它存储在Cookie对象中。
2、 使用setMaxAge()方法设置cookie的年龄为零,来删除现有的cookie。
3、 把这个cookie添加到相应头。

下面的例子将删除现有的名为first_name的cookie,当你下次运行ReadCookies的Servlet时,它会返回first_name为空值。

// 导入必需的 java 库
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// 扩展 HttpServlet 类
public class DeleteCookies extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      Cookie cookie = null;
	  Cookie[] cookies = null;
      // 获取与该域相关的 Cookies 的数组
      cookies = request.getCookies();     
	  // 设置响应内容类型
      response.setContentType("text/html");
      PrintWriter out = response.getWriter();
      String title = "Delete Cookies Example";
      String docType =
      "<!doctype html public \"-//w3c//dtd html 4.0 " +
      "transitional//en\">\n";
      out.println(docType + "<html>\n" +
                "<head><title>" + title + "</title></head>\n" +
                "<body bgcolor=\"#f0f0f0\">\n" );
       if( cookies != null ){
         out.println("<h2>Cookies 名称和值</h2>");
         for (int i = 0; i < cookies.length; i++){
            cookie = cookies[i];
            if((cookie.getName( )).compareTo("first_name") == 0 ){
                 cookie.setMaxAge(0);
                 response.addCookie(cookie);
                 out.print("已删除的 cookie:" + 
                              cookie.getName( ) + "<br/>");
            }
            out.print("名称:" + cookie.getName( ) + ",");
            out.print("值:" + cookie.getValue( )+" <br/>");
         }
      }else{
          out.println("<h2 class="tutheader">No cookies founds</h2>");
      }
      out.println("</body>");
      out.println("</html>");
   }
}

编译上面的Servlet DeleteCookies,并在web.xml文件中创建Servlet对应的位置,现在运行http://localhost:8080/DeleteCookies,将显示如下结果:
在这里插入图片描述

现在尝试运行http://localhost:8080/ReadCookies,它将只显示一个cookie,如下所示:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值