全文总结:
在读取Cookie,然后操作时,除了getName(),getValue()外,不要妄图得到其他信息,如下方法不会得到值的:
cookie.getMaxAge();
cookie.getDomain();
。。。
因为,客户端传来的时候,就只剩下key和value了。
Java中的Cookie操作:
在Java中利用Serlvet或者JSP scriptlet可以向浏览器端写入Cookie。
同样,利用Servet或者JSP scriptlet 也可以读取到 Cookie信息。
Servlet规范中的Cookie API 如下:
setMaxAge setPath setDomain等方法,可以对Cookie状态进行控制;
同样存在如下方法,可以获得相应的状态:
getMaxAge getPath getDomain等方法,可以对Cookie的状态。
然后,问题来了:
读取Cookie时,发现除了Cookie的key和value外,其他的信息都丢失了!!
原因很简单:
Cookie从服务器端发送到客户端时,信息是完整的;
Cookie从客户器端发送到服务端时,信息只剩下key、value了。
(想想也明白,Domain不对的Cookie、Path不对的Cookie、过期的Cookie,客户端是不会发送过来的。)
那为什么Java中提供了相应的get方法呢? 那个方法是在生成Cookie后,尚未发送到客户端时,使用的。是“封装”的体现吧 O(∩_∩)O~
案例说话:
写入Cookie的Servlet
- package edu.xjtu.servlet;
-
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class AddCookie extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- Cookie cookie01 = new Cookie( "site" , "JavaEye" );
- cookie01.setMaxAge(60 * 60 * 1 );
- cookie01.setPath("/" );
- Cookie cookie02 = new Cookie( "name" , "xiaolongfeixiang" );
- cookie02.setMaxAge(60 * 60 * 2 );
- cookie02.setPath("/servlet" );
-
-
- response.addCookie(cookie01);
- response.addCookie(cookie02);
-
- response.setContentType("text/html;charset=UTF8" );
- PrintWriter out = response.getWriter();
- out.println("<HTML>" );
- out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>" );
- out.println(" <BODY>" );
- out.println(" cookie写入了" );
- out.println(" <a href='show'> 查看cookie </a>" );
- out.println(" </BODY>" );
- out.println("</HTML>" );
- out.flush();
- out.close();
- }
-
- }
package edu.xjtu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddCookie extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie cookie01 = new Cookie("site","JavaEye");
cookie01.setMaxAge(60*60*1);
cookie01.setPath("/");
Cookie cookie02 = new Cookie("name","xiaolongfeixiang");
cookie02.setMaxAge(60*60*2);
cookie02.setPath("/servlet");
response.addCookie(cookie01);
response.addCookie(cookie02);
response.setContentType("text/html;charset=UTF8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" cookie写入了");
out.println(" <a href='show'> 查看cookie </a>");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
读取Cookie的Servlet
- package edu.xjtu.servlet;
-
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class ShowCookie extends HttpServlet {
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- Cookie[] cookies = request.getCookies();
-
- StringBuffer buffer = new StringBuffer();
-
- for (Cookie cookie : cookies){
- buffer.append(cookie.getName()).append(" : " ).append(cookie.getValue());
- buffer.append(" -- " ).append( " MaxAge : " +cookie.getMaxAge());
- buffer.append(" -- " ).append( " Path : " +cookie.getPath()).append( "<br/>" );
- }
-
- response.setContentType("text/html;charset=UTF8" );
- PrintWriter out = response.getWriter();
- out.println("<HTML>" );
- out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>" );
- out.println(" <BODY>" );
- out.println(" 读取的Cookie:<br>" );
- out.println(buffer.toString());
- out.println(" </BODY>" );
- out.println("</HTML>" );
- out.flush();
- out.close();
-
- }
-
- }
package edu.xjtu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowCookie extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Cookie[] cookies = request.getCookies();
StringBuffer buffer = new StringBuffer();
for(Cookie cookie : cookies){
buffer.append(cookie.getName()).append(" : ").append(cookie.getValue());
buffer.append(" -- ").append(" MaxAge : "+cookie.getMaxAge());
buffer.append(" -- ").append(" Path : "+cookie.getPath()).append("<br/>");
}
response.setContentType("text/html;charset=UTF8");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.println(" 读取的Cookie:<br>");
out.println(buffer.toString());
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
写入Cookie的页面 :
Response的信息如下:(信息齐全!!)
查看Cookie的页面:(发现结果丢了一部分:)
看浏览器的Cookie :
Cookie: name 属性都对着的
Cookie: site 属性也都对着的
再看看网络:(发现传过来的Cookie都只有key-value了。所以那些状态信息都丢失了!!)
验证了前面的总结:
在读取Cookie然后操作时,除了getName(),getValue()外,不要妄图得到其他信息,如下方法不会得到值的:
cookie.getMaxAge();
cookie.getDomain();
。。。
因为,客户端传来的时候,就只剩下key和value了。
感谢rentianchou,在解决他(她)提出的问题时,我做了这个实验!!谢谢rentianchou