Darth Blue Ray
Darth Blue Ray

Reputation: 9745

Retrieving cookie

I'm new to servlets and I have a question about cookies.

I have a servlet where I create a cookie and write a page where I put a link to another servlet where I want to retrieve the cookie.

For some reason I can't retrieve it. Any suggestions why it's empty?

Thx

First servlet:

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
        // create new cookies
        Cookie FirstCookie = new Cookie("user", "This is the value of my first Cookie");
        //all pages on the server should receive the cookie
        FirstCookie.setPath("/");
        FirstCookie.setVersion(0);
        FirstCookie.setSecure(false);
        //60 sec * 60 min * 24h * 5d = 
        FirstCookie.setMaxAge(432000);
        FirstCookie.setComment("some comment");
        // save the cookies
        response.addCookie(FirstCookie);


    out.println("<html><body>");
    out.println("<h1>CookieServlet</h1>");
    out.println("<a href='ReadCookiesServlet'>To the Cookie page</a>");
    out.println("</body></html>");
    out.close();
}

Second servlet:

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("Text/html");

    PrintWriter pw = response.getWriter();
    pw.println("<html><body>");
    pw.println("<h1>Bestaande cookies:</h1>");
    Cookie[] myCookies = request.getCookies();
    if (myCookies != null) {
        for (int i = 0; i < myCookies.length; i++) {
            pw.println("<table border=1 ALIGN=\'CENTER\'>");
            Cookie currentCookie = myCookies[i];
            if (currentCookie.getName().equalsIgnoreCase("user")) {

                pw.println("<tr><td>" + "Name : " + currentCookie.getName() + "<td></tr>");
                pw.println("<tr><td>" + "Path : " + currentCookie.getPath() + "<td></tr>");
                pw.println("<tr><td>" + "Version : " + currentCookie.getVersion() + "<td></tr>");
                pw.println("<tr><td>" + "Secure : " + currentCookie.getSecure() + "<td></tr>");
                pw.println("<tr><td>" + "Age : " + currentCookie.getMaxAge() + "<td></tr>");
                pw.println("<tr><td>" + "Value : " + currentCookie.getValue() + "<td></tr>");
                pw.println("<tr><td>" + "Comment : " + currentCookie.getComment() + "<td></tr>");
                pw.println("</table>");
            }
        }

    }

    pw.println("</body></html>");

    pw.close();
}

Upvotes: 0

Views: 176

Answers (1)

flup
flup

Reputation: 27103

I suspect it is empty because it did not get set by the first servlet. The way it is currently written, you have to supply a user parameter like this: FirstServletThatSetsCookie?user=flup when you surf to the first servlet or it will not set the cookie at all!

Note this bit:

String[] user = request.getParameterValues("user");
if (user != null) {
    // create new cookies

Upvotes: 1

Related Questions