+ -
当前位置:首页 → 问答吧 → 关于cookie的paht和domain

关于cookie的paht和domain

时间:2011-10-13

来源:互联网

为什么我在Servlet里获取的cookie对象的getPath()和getDomain()的值永远都是null呢?

TestServlet类部分如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
session.setAttribute("diudiu", "aaa");
Cookie cookie = new Cookie("aaa","bbb");
cookie.setPath("/");
response.addCookie(cookie);
response.sendRedirect("/Test/servlet/TestServlet2");

out.flush();
out.close();


}

TestServlet2类部分如下:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie[] cookie = request.getCookies();
for (int i = 0; i < cookie.length; i++) {
String name = cookie[i].getName();
String id = cookie[i].getValue();
String domain = cookie[i].getDomain();
String path = cookie[i].getPath();
System.out.print(name + "\n\r" + id + "\n\r" + domain + "\n\r" + path);
}

out.flush();
out.close();
}

作者: werty2891   发布时间: 2011-10-13

我要得分

作者: sxlfy   发布时间: 2011-10-14

热门下载

更多