Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=21157
authormarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 8 Jan 2011 22:27:26 +0000 (22:27 +0000)
committermarkt <markt@13f79535-47bb-0310-9956-ffa450edef68>
Sat, 8 Jan 2011 22:27:26 +0000 (22:27 +0000)
Write cookie before response might be committed

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1056820 13f79535-47bb-0310-9956-ffa450edef68

webapps/docs/changelog.xml
webapps/examples/WEB-INF/classes/CookieExample.java

index 4b05521..90c9213 100644 (file)
   </subsection>
   <subsection name="Web applications">
     <changelog>
+      <fix>
+        <bug>21157</bug>: Ensure cookies are written before the response is
+        commited in the Cookie example. Patch provided by Stefan Radzom. (markt)
+      </fix>
       <add>
         <bug>50294</bug>: Add more information to documentation regarding format
         of configuration files. Patch provided by Luke Meyer. (markt) 
index c49abea..00b5b89 100644 (file)
@@ -47,6 +47,15 @@ public class CookieExample extends HttpServlet {
                       HttpServletResponse response)
         throws IOException, ServletException
     {
+        
+        String cookieName = request.getParameter("cookiename");
+        String cookieValue = request.getParameter("cookievalue");
+        Cookie aCookie = null;
+        if (cookieName != null && cookieValue != null) {
+            aCookie = new Cookie(cookieName, cookieValue);
+            response.addCookie(aCookie);
+        }
+
         response.setContentType("text/html");
 
         PrintWriter out = response.getWriter();
@@ -89,11 +98,7 @@ public class CookieExample extends HttpServlet {
             out.println(RB.getString("cookies.no-cookies"));
         }
 
-        String cookieName = request.getParameter("cookiename");
-        String cookieValue = request.getParameter("cookievalue");
-        if (cookieName != null && cookieValue != null) {
-            Cookie cookie = new Cookie(cookieName, cookieValue);
-            response.addCookie(cookie);
+        if (aCookie != null) {
             out.println("<P>");
             out.println(RB.getString("cookies.set") + "<br>");
             out.print(RB.getString("cookies.name") + "  "