response.addCookie写完了之后的东西

时间:2015-03-10 08:09:50

标签: java servlets cookies

    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);

这个cookie实际上会被添加到响应中吗? 由于cookie进入消息的标题:

Cookie: NAME1=OPAQUE_STRING1; NAME2=OPAQUE_STRING2

为什么Servlet容器,如果不起作用,不显示警告或抛出异常?

1 个答案:

答案 0 :(得分:0)

如所怀疑的那样,如果你在回复之前写过回复,那么添加cookie似乎没有按预期工作。

这是我运行的servlet,在执行之后,我没有使用Chrome在我的localhost中看到“test_cookies”作为cookie。 Web servlet是Glassfish 4.1,但我想这是与容器无关的问题。

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>

}