aspose file tools*
The moose likes Servlets and the fly likes Question: How do you send a JSP page as compressed GZIP encoding? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Question: How do you send a JSP page as compressed GZIP encoding?" Watch "Question: How do you send a JSP page as compressed GZIP encoding?" New topic
Author

Question: How do you send a JSP page as compressed GZIP encoding?

Ray Chang
Greenhorn

Joined: Nov 12, 2000
Posts: 7
Does anyone know how to send a JSP page as compressed GZIP encoding? From reading "Core Servlets and JavaServer Pages" I know that servlets can send their HTML content as GZIP format, but I can't seem to get the technique to work for JSP pages (where it would help the most). Any help would be greatly appreciated. Thanks! -Ray
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Are you saying that you want the JSP code to output a GZIP file or that you want to send the contents of myfile.jsp in a compressed format?
JSP code automatically assumes that output will be a PrintWriter, which means it will not send binary data but will try to do a character translation.
Bill

------------------
author of:
Ray Chang
Greenhorn

Joined: Nov 12, 2000
Posts: 7
Hi Bill,
For example, the below servlet code 'sniffs' the client browser. If the browser accepts gzip encoding, the servlet sends the html output via gzip encoding. If the browser is old (ex: Netscape 3.0) and doesn't accept gzip encoding, the html output is sent normally.
I imagine that delivering html content in gzip-compressed format can make a big difference to clients using 28/56k modem connections.. a massive html page can be delivered to a browser as 2k rather than 500k..
Do you (or anyone) know how I can do this with jsp pages? Thanks..


[This message has been edited by Ray Chang (edited November 25, 2000).]
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hi Ray,
Sending the huge text page with compression , and setting the response header appropriately reduces the overall download time 1/10, from the bench mark given by Marty Hall, in his book 'Core Servlets , Java Server Pages' Page 105.
We can do this in jsp also. But since the 'out' var in jsp page is a 'JspWriter' which is NOT zip enabled, we have to get the GZIPOutputStream(outVar) where OutputStream outVar = response.getOutputStream(); Also we have to set the response header as shown in your code.
So I think we have to write the whole jsp page inside a scriplet. <% %> Don't you think so? Or, is there any other way easy way you got ?
regds
maha anna
[This message has been edited by maha anna (edited November 26, 2000).]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Looking at the code that the Tomcat JSP engine writes:
pageContext = _jspxFactory.getPageContext(this, request, response,
"", true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
--- where out, session, etc. are the "implicit objects" expected on all JSP pages.
It looks to me like you would have to customize the JSP engine in order to get a GZIP compression output stream instead of the JspWriter. You could certainly do this with the Tomcat source code.
Bill
Ray Chang
Greenhorn

Joined: Nov 12, 2000
Posts: 7
Originally posted by maha anna:
We can do this in jsp also. But since the 'out' var in jsp page is a 'JspWriter' which is NOT zip enabled, we have to get the GZIPOutputStream(outVar) where OutputStream outVar = response.getOutputStream(); Also we have to set the response header as shown in your code.

Hi Maha,
I tried many variations. The closest I got was the code below.. but I got an error: "Error: The type of the left-hand side in this assignment, "javax/servlet/jsp/JspWriter", is not compatible with the type of the right-hand side expression, "java/util/zip/GZIPOutputStream".


Originally posted by William Brogden:
It looks to me like you would have to customize the JSP engine in order to get a GZIP compression output stream instead of the JspWriter. You could certainly do this with the Tomcat source code.
Bill

Hi Bill,
Since I am just learning servlets & jsp, this might be something I don't want to mess with. If you do happen to know how to make the configuration change to the JRun server (what I'm using), that'd be helpful. But in truth, I'm not sure I would want the gzip sniffer to occur in ALL the jsp pages on a website.. just the certain selected JSP pages.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hi Ray,
We can't redefine the 'implicit' object 'out' to be different type. Your try is correct. You just have to change the var name. I slightly modified your code. It is working fine (Zipped output with Internet Explorer browser). Please check.
regds
maha anna
Ray Chang
Greenhorn

Joined: Nov 12, 2000
Posts: 7
Originally posted by maha anna:
Hi Ray, We can't redefine the 'implicit' object 'out' to be different type. Your try is correct. You just have to change the var name. I slightly modified your code. It is working fine (Zipped output with Internet Explorer browser). Please check.

Hi Maha..
Maybe because I'm on JRun, but your code gave me an error (see below). But even assuming the JSP code works for you, your solution still involves wrapping everything inside a scriptlet? Which means we might as well be using a servlet.. One of the advantages of JSP over servlets is that JSP doesn't require coding HTML tags/text in the format:
out.println("<HTML_stuff_here>");
If I have to build a whole website with the out.println(""); syntax just so I can use the gzip sniffer, it's sort of like taking one step forward but two steps back.. If this is the case, I'd probably drop the whole gzip idea, and just build the website on standard JSP.. Quicker to build, and easier to keep my sanity.. Big thanks either way for your help and feedback!


brian l smith
Greenhorn

Joined: Mar 15, 2001
Posts: 3
I am trying to working on compressing the jsp(s) for a website I am building. I would like to use gzip encoding in doing so. I have a page called main.jsp. This page has several page includes. Example header.jsp, footer.jsp, navigation.jsp, and content.jsp.
My question is: Has anyone tried to use gzip encoding on something like this. I has created test pages that use gzip but nothing this complicated. Is it possible? If not are there any other ways to use compression?
ps. I am running JRun Server 3.0
Thanks for any help.
Alok Pota
Ranch Hand

Joined: Mar 07, 2001
Posts: 185
APACHE has something called mod_gzip which will do it for you.
brian l smith
Greenhorn

Joined: Mar 15, 2001
Posts: 3
Originally posted by Alok Pota:
APACHE has something called mod_gzip which will do it for you.


I currently run NT with IIS running JRun Server 3.0
Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
Hi, I'm NEW
Regarding this problem with compresing JSP's with gzip or zip. I thought it would be possible to create a servlet that accepts the file to compress as extra path info and that way enabeling compression of JSP's..
this is my code so far:

I'm using the com.oreilly.servlet.ServletUtils package, you can download it from http://www.servlets.com/cos/index.html (cos-19Jun2001.zip)
The servlet detemines the browsers capabilities and then get's the right OutputStream. It requests the JSP, and sends it to the client compressed...
But somehow, it doesn't work for JSP's..
Could someone check this out ???

Thanks!!!
Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
it relates to this thread Servlet reading JSP file
but I'm thinking it can't be done...

[This message has been edited by Dave Van Even (edited July 20, 2001).]
David Roussel
Greenhorn

Joined: Aug 01, 2001
Posts: 1
The solution to this problem, and which is good practice anyway, is to use a servlet as the entry to parts of your application. Then use the RequestDispatcher to forward on to your JSPs to give the view.
For instance accessing the url /myapp/admin?cmd=add.user will invoke the admin servlet. The servlet checks the users login, creates the beans needed on the addUser.jsp page, adds then beans to the request context and does the gzip wrapping (explained in a moment). If any error occurs forward the request to error.jsp, otherwise forward the request to the correct jsp, as determined by the cmd page parameter.
The trick to getting the output stream compression to work is that RequestDispatcher.forward() allows you to pass in your own ServletResponse. All you have to do is create your own class that implements ServletResponse to warp the original ServletResquest. Each method should just delegate to the wrapped object, except the getOutputStream() method which should do all the compression set described further up this thread.

If you don't want to have servlets as the entry point to your web app, then you can make use of the JSP extends directive. This directive should be placed at the top of each JSP page and instructs the JSP compiler to use a give class as the base class of the generated page servlet. From this base class you then have all the hooks to add in output stream compression for a JSP transparently to the JSP. See any JSP tutorial for how to use the extends directive.
I hope this helps.
David Roussel
Dave Van Even
Ranch Hand

Joined: Jul 19, 2001
Posts: 101
thanks a ton!!!
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
One solution would be to use a filter.
See an example of compressing any server output here:
http://www.orionserver.com/tutorials/filters/lesson5/
Howard Thorp
Greenhorn

Joined: Aug 20, 2001
Posts: 1
I would really appreciate if you can post some coding example on
how to use gzip for JSPs
Thanks
Originally posted by David Roussel:
The solution to this problem, and which is good practice anyway, is to use a servlet as the entry to parts of your application. Then use the RequestDispatcher to forward on to your JSPs to give the view.
For instance accessing the url /myapp/admin?cmd=add.user will invoke the admin servlet. The servlet checks the users login, creates the beans needed on the addUser.jsp page, adds then beans to the request context and does the gzip wrapping (explained in a moment). If any error occurs forward the request to error.jsp, otherwise forward the request to the correct jsp, as determined by the cmd page parameter.
The trick to getting the output stream compression to work is that RequestDispatcher.forward() allows you to pass in your own ServletResponse. All you have to do is create your own class that implements ServletResponse to warp the original ServletResquest. Each method should just delegate to the wrapped object, except the getOutputStream() method which should do all the compression set described further up this thread.

If you don't want to have servlets as the entry point to your web app, then you can make use of the JSP extends directive. This directive should be placed at the top of each JSP page and instructs the JSP compiler to use a give class as the base class of the generated page servlet. From this base class you then have all the hooks to add in output stream compression for a JSP transparently to the JSP. See any JSP tutorial for how to use the extends directive.
I hope this helps.
David Roussel

Robert Priest
Greenhorn

Joined: Sep 05, 2001
Posts: 1
This one works with JRun 2.3.2:
Include the following to start of jsp.
===========================================
Stringencodings= request.getHeader("Accept-Encoding");
StringencodeFlag= request.getParameter("encoding");
if ((encodings != null) && (encodings.indexOf("gzip") !=-1) )
{
System.out.println("Gzip encoded");
response.setHeader("Content-Encoding", "gzip");
out = new uk.co.lg.epoc.servlet.tools.JspPrintWriter(new PrintWriter(new java.util.zip.GZIPOutputStream(response.getOutputStream()), false));
}
else
{
System.out.println("Not GZip encoded");
}
=======================
Include at the end of the jsp a
finally{out.close();}
to ensure that the GZipOutputStream flushes to the browser.
=============================
Implement appropriate JspPrintWriter for JSP environment:
For example:
================
package jsp.tools;
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);
}
}

Enjoy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question: How do you send a JSP page as compressed GZIP encoding?