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,如下所示: