测试:
return request.getCookies() == null;
不是一种合适的方式测试.还有另外一种方法吗?
设置一个cookie并尝试将其读回.
import javax.servlet.*; import javax.servlet.http.*; public class Test4Cookies extends HttpServlet { private static final Cookie cookie = new Cookie( "hello" , "world" ); private static final String paramName = "foo"; private static final String successURI = "/success.htm"; private static final String failureURI = "/failure.htm"; public void doPost(HttpServletRequest req, HttpServletResponse res) { if ( req.getParameter( paramName ) == null ) { res.addCookie( cookie ); res.sendRedirect(req.getRequestURI() +"?"+ paramName +"=bar" ); } else { res.sendRedirect (( req.getCookies().length == 0 ) ? failureURI : successURI ) } public void doGet(HttpServletRequest req, HttpServletResponse res) { doPost(req, res); } }