How to boost your website in Java?

UPDATED: 21 January 2012
Innovators making internet faster then our thoughts. Its also essential we have to serve our user best of it. User wants everything on finger tip like a blink of light. Some of you may searching for the content like

  • How to speed up site?
  • How to load site faster?
  • How to make Java, JSP, Servlet site fast?

To make your website secure and safe read this article How to create java website secure and safe?

I'll give you some tips to do it and you can see the effect its loading way much faster. Some content you ignored but it really matters when user have slow connection speed. Let me show how you made your site and its old idea to do it. Your site currently having this scenario as below.

This is how your site works if you didn't optimized it. Now we'll see how you can optimize if you didn't. Check your site grade using Yslow http://developer.yahoo.com/yslow/ We'll do optimization and after that check your grade you'll see it increased.

Compress StyleSheet, javaScripts
You must compress content to make response faster. So it'll take less time to load. Removing whitespace, unwanted semi colons from the StyleeSheet, javaScript. Use the below online tool to compress it

  • StyleeSheet (CSS) - Tool compress css and give you compressed CSS. Compress CSS in .css file as well as data between <style></style>
  • Tool : http://www.csscompressor.com/
  • Note: This tool compress CSS but some time it made changes according standard CSS rules. If you made any tricks in CSS it'll be omitted. After compression test your CSS and make changes again  for tricky CSS
Note : Place StyleSheet at the TOP and javaScript before the </body>.

Compress images, Don't scale it.
Compress images using some software, or online so it load faster on client side. Below some sites that helps you to convert and compress images. Secondly don't scale images say image 128x128 and you try to show as a 30x30. Better to make that image 30x30 rather then showing 128x128 image using width = 30.


Combine static images
Combine all static png images. Say if you have menubar icons so combining all images in one will reduce http requests. http://csssprites.com/

Remove Buffer
This is also required to flush the buffer. Put the below code between </head> <body> <% response.flushBuffer();%>

Few HTTP request
Combine all scripts in one file so it makes less parallel HTTP request. Say browser loads data after DNS look up(Domain Validation). So if you use multiple domain (image, script, etc... from other site) then it make DNS look up for every site. Try to minimize the HTTP request.Make HTTP request between 2-4 if possible. 

Set expire header for the content important task.
Browser it self store data in cache. But if you store data with out expiry. It'll load images, javascripts, stylesheets, etc... any time. Store data with proper expiry so it'll refresh data at that time only. All site have some static content like logo, menu image, script, stylesheet. Store this data at the client side so browse each time use data from cache. Browser doesn't make request for that data on server so server has to response less data. I'll show how you can do it.

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletResponse;

public class cacheControl implements javax.servlet.Filter {
    FilterConfig filterConfig = null;

    public void init(FilterConfig filterConfig) {
        this.filterConfig = filterConfig;
    }
    public void doFilter(ServletRequest req,
            ServletResponse res,
            FilterChain chain)
            throws IOException, ServletException {
        String sCache = filterConfig.getInitParameter("cache");

        if (sCache != null) {
            ((HttpServletResponse) res).setHeader("Cache-Control", sCache);
        }
        chain.doFilter(req, res);
    }
    public void destroy() {
        this.filterConfig = null;
    }
}

Create servlet using above code. Make an entry in web.xml if you are not using some IDE. Now set filer for what you want to set expiry header. Like *.css, *.js, *.png, etc....Copy paste the below code in web.xml . *.css, *.js, means every css from your domain, Below code have max-age=250000 mean it'll store data round about two days. You can also append *.png, *.jpg, etc... create proper filter.


<filter>
        <filter-name>Cache</filter-name>
        <filter-class>alien.config.cacheControl</filter-class>
        <init-param>
            <param-name>cache</param-name>
            <param-value>public, max-age=250000</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Cache</filter-name>
        <url-pattern>*.css</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>Cache</filter-name>
        <url-pattern>*.js</url-pattern>
    </filter-mapping>

Now check your grade using Yslow it would improve. The above filer response after some 2-3 request if you already opened site in your browser because browser already stored data. If your request comes from first time from that browser it'll store everything.

Compress content and serve to user.
Now we will pass data as a Gzip to client/user. All you need is find server.xml now find the Connector tag  and Append below code to it.

compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css"
It'll look like
<Connector URIEncoding="utf-8" connectionTimeout="20000" port="8084" protocol="HTTP/1.1" redirectPort="8443" compression="on" compressionMinSize="2048" noCompressionUserAgents="gozilla, traviata" compressableMimeType="text/html,text/xml,text/plain,text/javascript,text/css"/>

0 comments :