8 Replies Latest reply on Oct 6, 2007 12:47 AM by raist_majere

    Cookies lost when included JSP file used

    weisinger

      The following file creates a cookie when called alone, but when this file is included in another JSP, the cookie doesn't survive.
      Is this a bug in the JBoss App server, or is there configuration or code that I can use to get it to work?

      cookietest.jsp

      <%@ page contentType="text/html" %>
      <html><body>
      <p>
      <jsp:directive.page import="javax.servlet.http.Cookie"/>
      <jsp:scriptlet><![CDATA[
       Cookie c = new Cookie("NewCookie","Data for Cooki");
       out.println(c.getName()+": "+c.getValue()+"<br/>");
       response.addCookie(c);
      ]]></jsp:scriptlet>
      </p>
      </body></html>


      It fails though when included in this JSP page:

      jsptest.jsp
      <%@ page contentType="text/html" %>
      <jsp:include page="./cookietest.jsp" flush="true"/>
      <html><body>
      <center><b>JSP Test</b></center>
      </body></html>


        • 1. Re: Cookies lost when included JSP file used
          jfclere

          That is because of JSP spec's requirement.

          • 2. Re: Cookies lost when included JSP file used
            weisinger

             

            "jfrederic.clere@jboss.com" wrote:
            That is because of JSP spec's requirement.


            Could you be more specific?
            Is it not possible at all to create cookies on a JSP page that include another JSP page?

            Thanks

            • 3. Re: Cookies lost when included JSP file used
              jfclere

              The cookies in the included JSP pages are ignored.

              • 4. Re: Cookies lost when included JSP file used
                weisinger

                 

                "jfrederic.clere@jboss.com" wrote:
                The cookies in the included JSP pages are ignored.


                You're right. I see that under the JSP spec description for the include directive:

                http://sdlc-esd.sun.com/ESD24/JSCDL/jsp/2.1-fr/jsp-2_1-fr-spec.pdf?AuthParam=1184259549_291d84a0dc93c8ab5a265e4374e74aa3&TUrl=an1npDpbKod7kSYrROhENTonIeY4W0D1Lc4nXz+pGFFranixdCdgxDTPbW4=&TicketId=dVB+OQJINeg7/Q==&GroupName=SDLC&BHost=sdlc6g.sun.com&FilePath=/ESD24/JSCDL/jsp/2.1-fr/jsp-2_1-fr-spec.pdf&File=jsp-2_1-fr-spec.pdf
                An included page cannot change the response status code or set headers. This precludes invoking methods like setCookie.
                Attempts to invoke these methods will be ignored.
                The constraint is equivalent to the one imposed on the include method of the RequestDispatcher class.


                The workaround seems to be to have the base jsp file create the cookies.
                But doing that also seems to fail. Flipping around the example files I used above, the following also fails to create the cookie. Just including the file seemed to cause it to fail to create the cookie.

                jsptest.jsp
                <%@ page contentType="text/html" %>
                
                <jsp:include page="./cookietest.jsp" flush="true"/>
                
                <jsp:directive.page import="javax.servlet.http.Cookie"/>
                <jsp:scriptlet><![CDATA[
                 Cookie c = new Cookie("NewCookie","Data for Cooki");
                 out.println(c.getName()+": "+c.getValue()+"<br/>");
                 response.addCookie(c);
                ]]></jsp:scriptlet>
                
                <html><body>
                <center><b>JSP Test</b></center>
                </body></html>


                cookietest.jsp
                <%@ page contentType="text/html" %>
                <html><body>
                <p>
                Hello from included page.
                </p>
                </body></html>


                After commenting out the include, the cookies show up.
                Is this what I should expect? Is there something else that I need to do?

                <!-- jsp:include page="./cookietest.jsp" flush="true"/ -->


                • 5. Re: Cookies lost when included JSP file used
                  jfclere

                  flush="true"
                  That flushes the header and the cookies too early.
                  Remove it or use flush="false".

                  • 6. Re: Cookies lost when included JSP file used
                    weisinger

                    After doing that it works.

                    But it's too bad that it isn't possible to bring forward the cookies created by the included page.

                    Thanks for your help.

                    • 7. Re: Cookies lost when included JSP file used
                      weisinger

                      Unfortunately, there's still a problem.
                      I now understand the results of the test case above using included jsp files.

                      What I'm working on though uses Coldfusion cfm files.
                      I thought that I had replicated the same problem with just JSP pages, so that was what I originally reported.

                      In the JSP page, I'd like to include a cfm file, like the following:

                      <jsp:include page="../../../app/Application.cfm" flush="false" />


                      Below that include, I have the scriptlet code to create a cookie in the JSP.

                      If I comment out the include, the cookie is created.
                      When the include is used, the cookie doesn't show up.
                      flush is turned off.

                      Is there still something else I must do to get cookies to show up?
                      If this is a bug, is it one within JBoss or ColdFusion?

                      Thanks

                      • 8. Re: Cookies lost when included JSP file used
                        raist_majere

                        You should use a page directive specifying a greater buffer size. Take a look at the spec:


                        buffer
                        Specifies the buffering model for the initial out JspWriter to
                        handle content output from the page.
                        If none, then there is no buffering and all output is written
                        directly through to the ServletResponse PrintWriter.
                        The size can only be specified in kilobytes. The suffix kb is
                        mandatory or a translation error must occur.
                        If a buffer size is specified then output is buffered with a
                        buffer size not less than that specified.
                        Depending upon the value of the autoFlush attribute, the
                        contents of this buffer is either automatically flushed, or an
                        exception is raised, when overflow would occur.
                        The default is buffered with an implementation buffer size of
                        not less than 8kb.

                        autoFlush
                        Specifies whether the buffered output should be flushed
                        automatically (true value) when the buffer is filled, or
                        whether an exception should be raised (false value) to
                        indicate buffer overflow. It is illegal, resulting in a translation
                        error, to set autoFlush to false when buffer=none. The default
                        value is true


                        addCookie sets a header, which cannot be done once the page is flushed.