Tutorials : Six Steps to Faster J2EE Apps: Performance Tuning with JSP and Servlets :

Step 6: Use gzip cCompression

In general, compression is the process of compacting data into a smaller size. There are many defined compression standards defined, but gzip is the focus here.

Most browsers these days support gzip. Basically, sending data in gzip-compressed format gets your data to the client faster. The following code sample shows how:


public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse
httpServletResponse)
          throws IOException, ServletException{
          
   OutputStream out = null

   // Perform the check to see if the requesting client can support the format.
   // We can check the from the Accepting-Encoding header of the HTTP request.
   // If the header includes gzip, choose GZIP, else dont compress

   String encoding = httpServletRequest.getHeader("Accept-Encoding");    
      
   if (encoding != null && encoding.indexOf("gzip") != -1){
       httpServletResponse.setHeader("Content-Encoding" , "gzip");
       out = new GZIPOutputStream(httpServletResponse.getOutputStream());
   }
   else if (encoding != null && encoding.indexOf("compress") != -1){
       httpServletResponse.setHeader("Content-Encoding" , "compress");
       out = new ZIPOutputStream(httpServletResponse.getOutputStream());
   } 
   else{
       out = httpServletResponse.getOutputStream();

   }
   //Other methods will go here
}  

Spend More Time on Design

Starting with a sound application design always makes things easier down the line. Many of us don't generally spend a very little time actually designing applications—an approach that many times leads to the failure of an application. Failing that, if you can find a way to dedicate time to following best practices, the resulting clean code will definitely help you achieve better results.

Related Resources

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.