我正在尝试使用请求对象的getCookies()方法以累积的方式跟踪在每个servlet上添加的cookie,但令我惊讶的是,在第一个servlet中,请求对象知道其余所有存在的cookie servlet。它不是应该以累积方式显示吗?当请求对象仅位于第一个servlet上时,它如何才能获得其他servlet中的其余cookie?
我试图在Weblogic和Tomcat等各种服务器上进行检查,但都显示出相同的结果。即使尝试请求分派器的include()方法或forward()方法中的任何一个,我也得到相同的输出。
servlet1的代码
@WebServlet("/serv1")
public class Servlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie c1 = new Cookie("ck1", "Servlet 1 cookie 1");
Cookie c2 = new Cookie("ck2", "Servlet 1 cookie 2");
response.addCookie(c1);
response.addCookie(c2);
System.out.println("--------Cookies at Servlet 1 are------------");
Cookie[] cookies = request.getCookies();
for(Cookie i: cookies) {
System.out.println(i.getName() + " " + i.getValue());
out.println(i.getName() + " " + i.getValue());
}
RequestDispatcher requestDispatcher = request.getRequestDispatcher("./serv2");
requestDispatcher.include(request, response);
}
}
servlet2的代码
@WebServlet("/serv2")
public class Servlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie c3 = new Cookie("ck3", "Servlet 2 cookie 3");
Cookie c4 = new Cookie("ck4", "Servlet 2 cookie 4");
response.addCookie(c3);
response.addCookie(c4);
System.out.println("--------Cookies at Servlet 2 are------------");
Cookie[] cookies = request.getCookies();
for(Cookie i: cookies) {
System.out.println(i.getName() + " " + i.getValue());
out.println(i.getName() + " " + i.getValue());
}
RequestDispatcher requestDispatcher = request.getRequestDispatcher("./serv3");
requestDispatcher.include(request, response);
}
}
servlet3的代码
@WebServlet("/serv3")
public class Servlet3 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie c5 = new Cookie("ck5", "Servlet 3 cookie 5");
Cookie c6 = new Cookie("ck6", "Servlet 3 cookie 6");
response.addCookie(c5);
response.addCookie(c6);
System.out.println("--------Cookies at Servlet 3 are------------");
Cookie[] cookies = request.getCookies();
for(Cookie i: cookies) {
System.out.println(i.getName() + " " + i.getValue());
}
}
}
删除cookie
System.out.println("Deleting the cookies...");
for(Cookie i: cookies) {
i.setValue("");
i.setMaxAge(0);
}
控制台中的预期结果必须如下
--------Cookies at Servlet 1 are------------
ck1 Servlet 1 cookie 1
ck2 Servlet 1 cookie 2
--------Cookies at Servlet 2 are------------
ck1 Servlet 1 cookie 1
ck2 Servlet 1 cookie 2
ck3 Servlet 2 cookie 3
ck4 Servlet 2 cookie 4
--------Cookies at Servlet 3 are------------
ck1 Servlet 1 cookie 1
ck2 Servlet 1 cookie 2
ck3 Servlet 2 cookie 3
ck4 Servlet 2 cookie 4
ck5 Servlet 3 cookie 5
ck6 Servlet 3 cookie 6
Deleting the cookies...
控制台中的实际结果如下
--------Cookies at Servlet 1 are------------
ck1 Servlet 1 cookie 1
ck2 Servlet 1 cookie 2
ck3 Servlet 2 cookie 3
ck4 Servlet 2 cookie 4
ck5 Servlet 3 cookie 5
ck6 Servlet 3 cookie 6
--------Cookies at Servlet 2 are------------
ck1 Servlet 1 cookie 1
ck2 Servlet 1 cookie 2
ck3 Servlet 2 cookie 3
ck4 Servlet 2 cookie 4
ck5 Servlet 3 cookie 5
ck6 Servlet 3 cookie 6
--------Cookies at Servlet 3 are------------
ck1 Servlet 1 cookie 1
ck2 Servlet 1 cookie 2
ck3 Servlet 2 cookie 3
ck4 Servlet 2 cookie 4
ck5 Servlet 3 cookie 5
ck6 Servlet 3 cookie 6
Deleting the cookies...
据我所知,我已经提供了这些信息。告知是否需要编辑或格式化。请提供基于控制流和更多实际可行输出的原因,而对理论的亲和力较小。