Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending compressed (gzip) jsp 1

Status
Not open for further replies.

MultipartRequest

Programmer
Sep 12, 2001
19
CL
i know how to send a compressed page generated by a servlet, but is thre a way to send a compressed JSP?
 
Three solutions:

1) Create your own output object and write the entire page in <% %> using this custom output. This is required since we can't redefine the default
Code:
out
object in JSP.
Example:
Code:
<%    
String encodings = request.getHeader(&quot;Accept-Encoding&quot;);    PrintWriter outWriter = null;        
if ((encodings != null) && (encodings.indexOf(&quot;gzip&quot;) != -1)) {         
  /* Get default OutputStream, wrap in GZIPOutputStream */
  OutputStream defaultStream = response.getOutputStream();
  outWriter = new PrintWriter(new GZIPOutputStream(defaultStream), false);
  response.setHeader(&quot;Content-Encoding&quot;, &quot;gzip&quot;);    
} 
else {  	
  /* Do not send Zipped, get default OutputStream */        
  outWriter = new PrintWriter(response.getOutputStream(), false);          
}    
/* Rest of the page using outWriter.println() */
%>
I don't really like this solution.

2) If you are using a Servlet as the entry point for your JSP (and most of the time you should be aka MVC Pattern), then pass a wrapped version of the HttpServletResponse in the RequestDispatcher. Create a class that implements HttpServletResponse to wrap the original. Each method should just delegate to the wrapped object, except the getOutputStream() method which will need to be overridden to do all the compression stuff in the example above. I will not give an example for this because I am lazy. But trust me, it is not very hard. This is a very good solution if you can't do...

3) My favorite. Use a Filter (Servlet 2.3) to compress any responses sent from the Web Application. This is even better because you could reuse this Filter in all of your Web Applications and set minimum size limits for compression easily. Great Object Oriented Design, competely the decouples the Compression Layer from the Application Layer.

Hope this helps. Sorry it probably wasn't as straight forward as you might have hoped.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top