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

request.getCookies() 返回值始终为空问题

写了两个java文件,一个用于写入Cookies一个用于读取显示Cookies,但是在读取时request始终为空,到底问什么呢,代码如下:

SetCookies.java

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 SetCookies extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
for(int i = 0; i < 3; i++) {
Cookie cookie = new Cookie("Cookie-ID:" + i, "Cookie-value:" + i);
response.addCookie(cookie);

cookie = new Cookie("Cookie-ID(M)" + i, "Cookie-value(M):" + i);
cookie.setMaxAge(3600);
response.addCookie(cookie);
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Set the Cookies</title></head>");
out.println("<body>");
out.println("<a href=\"ShowCookies\">Click to show cookies</a>");
out.println("</body></html>");
}

}


ShowCookies.java

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 ShowCookies extends HttpServlet{

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Show all the parameters</title></head>");
out.println("<body>");
out.println("<div align='center'>Show all of the cookies</div>");
out.println("<form id='form1' name='form1' method='get'>");
out.println("<table width='215' height='42' border='2' align='center' cellpadding='0' cellspacing='0'>");
Cookie[] cookies = request.getCookies();

if(cookies == null) {
return;
} else {
for(Cookie cookie : cookies) {
out.println("<tr>");
String name = cookie.getName();
String value = cookie.getValue();
out.println("<td width='91'><div align='center'>"+ name + "</div></td>");
out.print("<td width='262'>" + value + "</td>");
out.println("</tr>");
}
}

out.println("</table></form></body></html>");
}

}


配置没有问题,SetCookies可以显示,但是ShowCookies始终显示不出结果,Debug了一下,在ShowCookies的doGet()方法中,形参request始终为空,但是用chrome查看网页信息的时候可以查看到所有的Cookies,表示Cookies已经加载到网页,但是为什么不能读取呢??? --------------------编程问答-------------------- 因为你把cookie写入到响应中,这个响应是相对于 out.println("<html><head><title>Set the Cookies</title></head>"); 这个页面的请求。
你再次点击链接等于重新发送了一个请求,那么这个请求自然为空的。因为你
"<a href=\"ShowCookies\">Click to show cookies</a>这个请求中没有添加任何参数。 --------------------编程问答--------------------
引用 1 楼 AA5279AA 的回复:
因为你把cookie写入到响应中,这个响应是相对于 out.println("<html><head><title>Set the Cookies</title></head>"); 这个页面的请求。
你再次点击链接等于重新发送了一个请求,那么这个请求自然为空的。因为你
"<a href=\"ShowCookies\">Click to show cookies</a>这个请求中没有添加任何参数。


但是Cookie不是只要是在一个web-application中就是可以访问的吗?访问SetCookies之后在IE存放cookie的文件夹里可以看到新建的cookie文件,但是就是读取不能读取,既然是在同一web-app下的应该是可以读取的啊 --------------------编程问答--------------------

引用 2 楼 zeko075 的回复:
Quote: 引用 1 楼 AA5279AA 的回复:

因为你把cookie写入到响应中,这个响应是相对于 out.println("<html><head><title>Set the Cookies</title></head>"); 这个页面的请求。
你再次点击链接等于重新发送了一个请求,那么这个请求自然为空的。因为你
"<a href=\"ShowCookies\">Click to show cookies</a>这个请求中没有添加任何参数。


但是Cookie不是只要是在一个web-application中就是可以访问的吗?访问SetCookies之后在IE存放cookie的文件夹里可以看到新建的cookie文件,但是就是读取不能读取,既然是在同一web-app下的应该是可以读取的啊

cookie是本地的,与cookie相对应的是服务器端的seesion。
这两个想对应起来才起作用。
可以这么想
一个application下面有很多的request
一个request下面有很多session
--------------------编程问答-------------------- //创建cookie
public static void createCookie(HttpServletResponse response,  
Hashtable<String, String> nameValues, int days) {  
Set<String> set = nameValues.keySet();  
Iterator<String> it = set.iterator();  
for (; it.hasNext();) {  
String name = (String) it.next();  
String value = (String) nameValues.get(name);  
// 生成新的cookie  
Cookie cookie = new Cookie(name, value);  
// 设置有效日期  
cookie.setMaxAge(days * 24 * 60 * 60);  
// 设置路径(默认)  
cookie.setPath("/");  
// 把cookie放入响应中  
response.addCookie(cookie);  
}  
}  
//读取cookie
public static Hashtable<String, String> getCookies(  
HttpServletRequest request) {  
Cookie[] cookies = request.getCookies();  
Hashtable<String, String> cookieHt = new Hashtable<String, String>();  
if (cookies.length > 0) {  
for (int i = 0; i < cookies.length; i++) {  
Cookie cookie = cookies[i];  
cookieHt.put(cookie.getName(), cookie.getValue());  
}  
}  
return cookieHt;  
}  
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,