I've created two servlets:
UserReceiverServlet receives a username from a form. It then sets the username to an attribute and forwards the request to UserDisplayServlet.
UserDisplayServlet will add the username to a cookie and then display the current continents of both the attribute which was set and the cookie which was stored.
However, while I've managed to determine that the cookie is definitely being created and the value is being stored, when this Servlet goes to look for it, it doesn't find it
Here is UserDisplayServlet. It is correctly receiving a value through request.getAttribute("username"), so attributeUsername isn't the problem...
/**
*
*/
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>"); }
}
}
If it is helpful, here is the servlet that is receiving the input and forwarding it to the above 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();
}
}
}
What am I missing to retrieve the value to stored in the cookie?