Reputation: 3211
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
out.println("<html>");
out.println("<head>");
Cookie offeredCommunityCookie = new Cookie("offered_community", "true");
offeredCommunityCookie.setMaxAge(51840000); // 600 days
response.addCookie(offeredCommunityCookie);
Will this cookie will be actually added to the response? Since cookies goes into header of the message as:
Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2
Why Servlet container, if this doesn't work, do not display a warning or throws an exception?
Upvotes: 0
Views: 1988
Reputation: 3211
As suspected, adding cookies seems not to be working as expected if you did writing to the response before it.
This is the servlet I run which, after execution, I don't see "test_cookies" as a cookie in my localhost using Chrome. Web servlet is Glassfish 4.1, but I guess it is a container-independent issue.
package common;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author mladen
*/
public class TestCookiesServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
out.println("<html>");
out.println("<head>");
out.flush();
response.flushBuffer();
Cookie offeredCommunityCookie = new Cookie("test_cookies", "true");
offeredCommunityCookie.setMaxAge(51840000); // 600 days
response.addCookie(offeredCommunityCookie);
} finally {
out.println("</head>");
out.println("<body>Test<p/>Test<p/>Test");
out.println("</body>");
out.println("</html>");
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Upvotes: 1