Is there a way to compress the result of the JSP and return it to the browser? Most browsers support GZIP and I have compressed straight HTML and also used this technique with XML/XSL. Btw, I am using a servlet-centric approach.

Serge Knystautas

Working strictly within the servlet 2.2 spec, it is not possible. The main problem is that there is no support for filtering the content of a generated servlet of JSP. The additional problem for JSPs is that they call getWriter(), afterwhich you cannot call getServletOutputStream(). This is significant because the Writer will encode data, and while that's fine for XML, that's not ok with a GZIP stream.

When servlets first came out (on the JavaWeb Server and with the JSDK), there was a concept of servlet chaining. This allowed you to specify that an internal mime-type would execute another servlet. That new servlet would read the output of the first servlet as it's input stream. i.e., servlet1 or JSP1 generates a page, sets the content-type to internal/compress, the servlet engine associates the mime-type internal/compress with CompressServlet, CompressServlet runs reading the output of the first servlet and compresses and streams the gzip content out. Some commercial servlet engines such as Allaire's JRun still supports this concept. We used servlet chaining to do useful tasks such as stripping unnecessary white space, and dynamically creating zip files of whatever you wanted on the server.

The other approach is to wait until Servlet 2.3 spec is implemented by vendors, which has support for filtering. In the spec, you write a ServletFilter that implements doFilter(), and this allows you to manipulate the input stream, headers, or whatever you want. The spec is out for current review, and supposedly the Jakarta site will release Tomcat 4.0 with servlet 2.3 support around the end of 2000. Other servlet engines will probably have support this fall or winter as well.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact