我创建了两个servlet:
UserReceiverServlet从表单接收用户名。然后,它将用户名设置为属性,并将请求转发给UserDisplayServlet。
UserDisplayServlet会将用户名添加到cookie中,然后显示已设置的属性和存储的cookie的当前大陆。
然而,虽然我已经设法确定cookie肯定是在创建并且值正在存储,但是当这个Servlet去寻找它时,它找不到它
这是UserDisplayServlet。它通过request.getAttribute(“username”)正确接收值,所以attributeUsername不是问题......
/**
*
*/
package hu.flux.user;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.*;
/**
* @author Brian Kessler
*
*/
public class UserDisplayServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void service (HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("text/html");
String attributeUsername = (String) request.getAttribute("username");
String cookieUsername = null;
for (Cookie cookie: request.getCookies())
{ if (cookie.getName() == "username")
{
String value = cookie.getValue();
if (value.isEmpty()) { value = "--- EMPTY ---"; }
cookieUsername = cookie.getValue();
break;
}
}
if (cookieUsername == null) {cookieUsername = "--- NOT FOUND ---"; }
String newCookieValue;
if (!(cookieUsername.isEmpty())) { newCookieValue = cookieUsername;}
else if (!(attributeUsername.isEmpty())) { newCookieValue = cookieUsername;}
else { newCookieValue = "UNKOWN";}
Cookie usernameCookie = new Cookie ("username", attributeUsername);
usernameCookie.setMaxAge(24*60*60);
response.addCookie(usernameCookie);
PrintWriter out = null;
try { out = response.getWriter(); }
catch (IOException e)
{
System.err.print ("Cannot getWriter():" + e);
e.printStackTrace();
}
out.println("<html>");
out.println("<head>");
out.println("<title>User Display Servlet</title>");
out.println("</head>");
out.println("<body");
if ((attributeUsername != null) && (!(attributeUsername.isEmpty())))
{
out.println ("<p>I have an attribute which says your username is "
+ request.getAttribute("username") + "</p>");
}
else { out.println ("<p>The attribute hasn't been set.</p>"); }
if ((cookieUsername != null) && (!(cookieUsername.isEmpty())))
{
out.println ("<p>You have a cookie which says your username is " + cookieUsername + "</p>");
out.println ("</body></html>");
}
else { out.println ("<p>The cookie hasn't been set.</p>"); }
}
}
如果它有用,这里是接收输入并将其转发到上述servlet的servlet:
/**
*
*/
package hu.flux.user;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.*;
/**
* This simple servlet receives a username from a form,
* stores the name in a context attribute,
* and forwards the context attribute to another servlet
* which displays the name.
*
* @author Brian Kessler
*
*/
public class UserReceiverServlet extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
public void service (HttpServletRequest request, HttpServletResponse response)
{
String username = request.getParameter("username");
request.setAttribute("username", username);
try {
getServletConfig()
.getServletContext()
.getRequestDispatcher("/userdisplay")
.forward(request, response);
} catch (ServletException e) {
System.err.println ("Can't forward: " + e);
e.printStackTrace();
} catch (IOException e) {
System.err.println ("Can't forward: " + e);
e.printStackTrace();
}
}
}
我缺少什么来检索存储在cookie中的值?
答案 0 :(得分:1)
发现问题:
if (cookie.getName() == "username")
应该是:
if (name.equals("username"))
总是绊倒我!
答案 1 :(得分:0)
你怎么知道还没有设置cookie? HTML正文肯定不会告诉你,因为内容是在服务器端而不是客户端评估的。如果您真的希望页面告诉您是否已成功设置cookie,您需要在HTML中嵌入一些javascript来访问浏览器的cookie存储区。
如果我试图解决这个问题,我首先要查看服务器和客户端的HTTP响应。在服务器端启用webapp容器支持的响应转储/日志记录;例如对于Tomcat,打开RequestDumperValve。在客户端,使用浏览器的首选项对话框(或其他)查看您正在与之交互的服务器的cookie存储的内容。然后使用浏览器的Javascript调试器(或其他)查看响应头是否包含用于设置cookie的标头。 (如果您使用的是firefox,请安装firebug插件。)