9 Replies Latest reply on Jan 2, 2002 1:20 PM by jimotte

    How can I compress JSP for output?????

    jimotte Newbie

      I have the following problem- of which I found no answer to in any forums so far- hopefully someone has had to deal with this:
      I have a control servlet that redirects to various JSP's:
      doPost() of my controlServlet:
      PageBean pageBean = (PageBean)request.getSession().getAttribute((String)request.getParameter("PageBean"));
      pageBean.execute(request);

      response.sendRedirect("JSP/"+pageBean.getNextPage());


      1 of the JSP's however is actually of type svg/image- and I have to populate it with a LOT of data (lots of XML)- which I do just fine- except that it needs to be compressed for the client to view- so the ? is how can this be done??
      I know that with servlets the OutputStream can be wrapped in a ZipOutputStream and the Header set to "compress" - but how can this be accomplished with JSP's? - without actually accessing the servlet generated ?
      Thanks in advance to whoever can solve this one!

        • 1. Re: How can I compress JSP for output?????
          Jochen Cordes Novice

          Hi !

          Actually two solutions come into my mind, but it depends on the technology you are using. One of the solutions is to write your own taglib for the jsp (BodyTagSupport). There you can access the generated xml-data (BodyContent) and compress it (have a look on www.jspin.com for tutorials on taglibs). The second is based on servlet-API 2.3 (implemented in Tomcat 4.0). It's possible to write a filter that will do the compression.

          Hope it helps,

          Jochen.

          • 2. Re: How can I compress JSP for output?????
            Dmitry Namiot Newbie

            See Coldtags suite on http://www.servletsuite.com/jsp.htm
            Optimaze taglib does this job.

            • 3. Re: How can I compress JSP for output?????
              jimotte Newbie

              Thanks for the tag Lib link- I tried the Optimize tag lib but now when I access the jsp I get a popup asking if I want to download it or save to disk- either way it does not display- and on the console I get the following error:
              [Default] Warning: validation was turned on but an org.xml.sax.ErrorHandler was
              not
              [Default] set, which is probably not what is desired. Parser will use a default

              [Default] ErrorHandler to print the first 10 errors. Please call
              [Default] the 'setErrorHandler' method to fix this.
              [Default] Error: URI=null Line=1: Element type "web-app" must be declared.
              [Default] Error: URI=null Line=2: Element type "servlet" must be declared.
              [Default] Error: URI=null Line=3: Element type "servlet-name" must be declared.
              [Default] Error: URI=null Line=4: Element type "servlet-class" must be declared.

              [Default] Error: URI=null Line=6: Element type "servlet" must be declared.
              [Default] Error: URI=null Line=7: Element type "servlet-name" must be declared.
              [Default] Error: URI=null Line=8: Element type "servlet-class" must be declared.

              [Default] Error: URI=null Line=10: Element type "servlet" must be declared.
              [Default] Error: URI=null Line=11: Element type "servlet-name" must be declared.

              [Default] Error: URI=null Line=12: Element type "servlet-class" must be declared
              .
              [Default] Warning: validation was turned on but an org.xml.sax.ErrorHandler was
              not
              [Default] set, which is probably not what is desired. Parser will use a default

              [Default] ErrorHandler to print the first 10 errors. Please call
              [Default] the 'setErrorHandler' method to fix this.
              [Default] Error: URI=null Line=3: Element type "taglib" must be declared.
              [Default] Error: URI=null Line=4: Element type "tlibversion" must be declared.
              [Default] Error: URI=null Line=5: Element type "jspversion" must be declared.
              [Default] Error: URI=null Line=6: Element type "shortname" must be declared.
              [Default] Error: URI=null Line=7: Element type "uri" must be declared.
              [Default] Error: URI=null Line=8: Element type "info" must be declared.
              [Default] Error: URI=null Line=10: Element type "tag" must be declared.
              [Default] Error: URI=null Line=11: Element type "name" must be declared.
              [Default] Error: URI=null Line=12: Element type "tagclass" must be declared.
              [Default] Error: URI=null Line=13: Element type "bodycontent" must be declared.


              any ideas why??

              • 4. Re: How can I compress JSP for output?????
                marc fleury Master

                Dimitry Namiot?

                The one and only dimitry namiot?

                It blows my mind to see you pimping your stuff on my lists :)

                good to see you, are you "From: the butt-icy-cold Russia" or back in "sunny CA"

                Hope you are doing well

                • 5. Re: How can I compress JSP for output?????
                  Dmitry Namiot Newbie

                  > Dimitry Namiot?
                  >
                  > The one and only dimitry namiot?
                  >
                  > It blows my mind to see you pimping your stuff on my
                  > lists :)
                  >
                  Hi Marc,

                  I am proud to be here :)

                  P.S. we are not bad. See http://www.servletsuite.com/

                  • 6. Re: How can I compress JSP for output?????
                    Dmitry Namiot Newbie

                    Please, tell us what you did exactly? any code?
                    Feel free to contact info@servletsuite.com
                    with questions

                    Best regards,
                    Dmitry Namiot

                    • 7. Re: How can I compress JSP for output?????
                      jimotte Newbie

                      Actually the opt:Compress tag works great for all of my regular jsp pages that are of text/html type- however for SVG: <%@ page contentType="image/svg-xml"%> It was not working due to a bug in the browsers that handle SVG of not being able to handle the Header of "Content-Encoding" "gzip"- it tries to open it up in a separate directory- see :http://www.siliconpublishing.org/svgfaq/Apache.asp
                      which basically states:
                      Peter Sorotokin clarifies the content encoding issues with IE:

                      Note that Content-Encoding can trigger a bug in IE, so it will never hand svgz over to SVG control (and give it to WinZip instead). Therefore, while from a standard's perspective, you *MUST* include Content-Encoding in your header, you might want not to do it for now (or at least make sure that what you are trying to do really works with IE). This violates HTTP standard, but Adobe SVG viewer is forgiving about missing Content-Encoding and will still detect and process a compressed file.


                      I have found this to be true in all browsers- not just Netscape- the content encoding header can not be set with svg- although it will still process the zipped content- that is why your tag lib dnamiot will not work with svg (perhaps in new version??)- To fix this problem and for the benefit of others with the same SVG compression problem in JSP's- I'll include the following code snippets:

                      At very Top of jsp (with no line spaces!!)Put this:

                      <%String encodings = request.getHeader("Accept-Encoding");
                      String encodeFlag = request.getParameter("encoding");
                      if ((encodings != null) && (encodings.indexOf("gzip") !=-1) || session.getAttribute("gzip")!=null){
                      out = new com.mycompany.client.jsputil.JspPrintWriter(new PrintWriter(new java.util.zip.GZIPOutputStream(response.getOutputStream()), false));
                      session.setAttribute("gzip", "true");
                      System.out.println("Gzip encoded");
                      }
                      else{
                      System.out.println("Not GZip encoded :"+encodings+" "+encodeFlag);
                      }%><?xml version="1.0" encoding="US-ASCII"?>
                      <%@ page contentType="image/svg-xml"%>

                      JSP BODY

                      End jsp- svg:



                      <%
                      out.flush();
                      out.close();
                      out=null;
                      pageContext = null;%>


                      Make sure there are no line spaces for the first XML declaration or the browsers will barf:
                      }%><?xml version="1.0" encoding="US-ASCII"?>

                      As for the subclassed PrintWriter I found this:

                      package com.mycompany.client.jsputil;

                      import java.io.*;

                      public class JspPrintWriter extends javax.servlet.jsp.JspWriter{
                      PrintWriter delegate;

                      public JspPrintWriter(PrintWriter aDelegate){
                      super(0, true);
                      delegate = aDelegate;
                      }
                      /**
                      * clear method comment.
                      */
                      public void clear() throws java.io.IOException
                      {
                      delegate.flush();
                      }
                      /**
                      * clearBuffer method comment.
                      */
                      public void clearBuffer() throws java.io.IOException
                      {
                      delegate.flush();
                      }
                      /**
                      * close method comment.
                      */
                      public void close() throws java.io.IOException {
                      delegate.close();
                      }
                      /**
                      * flush method comment.
                      */
                      public void flush() throws java.io.IOException {
                      delegate.flush();
                      }
                      /**
                      * getRemaining method comment.
                      */
                      public int getRemaining()
                      {
                      return 1;
                      }
                      /**
                      * newLine method comment.
                      */
                      public void newLine() throws java.io.IOException
                      {
                      delegate.println("");
                      }
                      /**
                      * print method comment.
                      */
                      public void print(char[] arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * print method comment.
                      */
                      public void print(char arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * print method comment.
                      */
                      public void print(double arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * print method comment.
                      */
                      public void print(float arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * print method comment.
                      */
                      public void print(int arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * print method comment.
                      */
                      public void print(long arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * print method comment.
                      */
                      public void print(Object arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * print method comment.
                      */
                      public void print(String arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * print method comment.
                      */
                      public void print(boolean arg1) throws java.io.IOException {
                      delegate.print(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println() throws java.io.IOException {
                      delegate.println();
                      }
                      /**
                      * println method comment.
                      */
                      public void println(char[] arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println(char arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println(double arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println(float arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println(int arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println(long arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println(Object arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println(String arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * println method comment.
                      */
                      public void println(boolean arg1) throws java.io.IOException {
                      delegate.println(arg1);
                      }
                      /**
                      * write method comment.
                      */
                      public void write(char[] cbuf, int off, int len) throws java.io.IOException {
                      delegate.write(cbuf,off,len);
                      }
                      }


                      I hope this helps out other developers as This was a real pain in the butt to solve (especially since I hate dealing with browsers)- and seems to work.

                      As for the tag lib from dnamiot- I still get the parsing error- although it is not fatal and does work- maybe has to do with my web.xml:

                      <web-app>

                      <servlet-name>control</servlet-name>
                      <servlet-class>com.mycompany.client.control.ControlServlet</servlet-class>


                      <servlet-mapping>
                      <servlet-name>control</servlet-name>
                      <url-pattern>/control/*</url-pattern>
                      </servlet-mapping>

                      <taglib-uri>taglib.tld</taglib-uri>
                      <taglib-location>/WEB-INF/tlds/taglib.tld</taglib-location>

                      </web-app>

                      any ideas??
                      Also 1 more side note- for those of you thinking why didnt I try using filters for the compressed response- I did- using JBoss2.4.3_Catalina 4 (with servlet 2.3 support)- It worked great for Netscape then had even new problems with IE- a socketWrite Exception- which was way too much for my head around Christmas (esp. since Catalina is real sensitive- like tomcat allowed fileInputStream as serializable but catalina does not- strange)- so I went back and did it the way as described above -so be forewarned as to filters- although it might possibly work with some "browser" code modifications-

                      Have fun


                      • 8. Re: How can I compress JSP for output?????
                        Dmitry Namiot Newbie

                        <web-app>

                        <servlet-name>control</servlet-name>
                        <servlet-class>com.mycompany.client.control.ControlServlet</servlet-class>


                        <servlet-mapping>
                        <servlet-name>control</servlet-name>
                        <url-pattern>/control/*</url-pattern>
                        </servlet-mapping>

                        <taglib-uri>taglib.tld</taglib-uri>
                        <taglib-location>/WEB-INF/tlds/taglib.tld</taglib-location>

                        </web-app>

                        what if you remove <web-app> and </web-app> lines?

                        • 9. Re: How can I compress JSP for output?????
                          jimotte Newbie

                          Found the reason for the warnings when using the opttags library tags- with Xerces parser (picky)- I had forgotten (not really needed before) the doc type tag at the beginning of my web.xml:

                          <?xml version="1.0" encoding="ISO-8859-1"?>
                          <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "<http://java.sun.com/j2ee/dtds/web-app_2_2.dtd>">

                          After that was put into the web.xml the compress tag works great for all other JSP types (minus the svg)- with no warning messages