读了该文章http://book.51cto.com/art/200811/96440.htm,我尝试运行了里头的代码:package servletcookie;import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;@SuppressWarnings("serial")
public class WelcomeVisitor extends HttpServlet {
String newUserName = "";
Cookie c = null;
Cookie[] cookies = null;
public void service(HttpServletRequest request, 
HttpServletResponse response)
throws ServletException, IOException {
cookies = request.getCookies();
String title;
title = "Welcome , " + getCookieValue();
newUserName = request.getParameter("Name");
if (newUserName == null || newUserName.trim().equals("")) {
newUserName = "Anonymous";
}
if (newUserName != null) {
response.addCookie(makeCookie());
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>" + title + "</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<h1>" + title + "</h1>");
out.println("<FORM ACTION=\"/testServlet/servlet/WelcomeVisitor\"  method=\"POST\">");
out.println("<p>Input your name:</p>");
out.println("<INPUT TYPE=\"TEXT\" NAME=\"Name\">");
out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"Submit Form\">");
out.println("</FORM>");
out.println("</body>");
out.println("</html>");
out.close();
} private Cookie makeCookie() {
Cookie c = new Cookie("Name", newUserName);
c.setMaxAge(60 * 60 * 24 * 365); // 1 year  System.out.println("name=" + c.getName());
System.out.println("value=" + c.getValue());
return c;
} private String getCookieValue() {
String cookieValue = "Anonymous";
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) {
c = cookies[i];
if (c.getName().equals("Name")) {
cookieValue = c.getValue();
break;
}
}
}
return cookieValue;
}
}程序可以运行,控制台上也一直有显示内容。只不过没有办法达到文章内所说的效果。无论提交多少次刷新了都依然是WelcomeVisitor:Anonymous.而控制台上都是
name=Name
value=Anonymous
浏览器并没有禁用cookie,而且每次输入双击输入框时曾经输入过的内容都会有得选择。请问哪位高手、大侠可以告诉小人哪里出了问题?要怎样才能得到文章中提到的效果?