我正在尝试运行 Tomcat 中的 cookie 示例,但它给我带来了问题。Tomcat为程序显示的源代码是
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class CookieExample extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// print out cookies
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++) {
Cookie c = cookies[i];
String name = c.getName();
String value = c.getValue();
out.println(name + " = " + value);
}
// set a cookie
String name = request.getParameter("cookieName");
if (name != null && name.length() > 0) {
String value = request.getParameter("cookieValue");
Cookie c = new Cookie(name, value);
response.addCookie(c);
}
}
}
我正在java.lang.NullPointerException
排队for (int i = 0; i < cookies.length; i++) {
我正在传递 cookieName 和 cookieValue。我尝试先设置 cookie,然后打印它,但它在同一行给出了相同的错误。问题是,当我几个月前尝试这个程序时,它在 Internet Explorer 上运行良好,但在 Chrome 上却不行。我猜它给出了一个 http 作为名称和代理作为值。