com.googlecode.webutilities.servlets
Class JSCSSMergeServlet

java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by com.googlecode.webutilities.servlets.JSCSSMergeServlet
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class JSCSSMergeServlet
extends javax.servlet.http.HttpServlet

The JSCSSMergeServet is the Http Servlet to combine multiple JS or CSS static resources in one HTTP request. using YUICompressor.

Using JSCSSMergeServet the multiple JS or CSS resources can grouped together (by adding comma) in one HTTP call.

Usage

Put the webutilities-x.y.z.jar in your classpath (WEB-INF/lib folder of your webapp).

Declare this servlet in your web.xml ( web descriptor file)

 ...
 <servlet>
        <servlet-name>JSCSSMergeServet</servlet-name>
        <servlet-class>com.googlecode.webutilities.JSCSSMergeServet</servlet-class>
        <!-- This init param is optional and default value is minutes for 7 days in future. To expire in the past use negative value. -->
        <init-param>
                <param-name>expiresMinutes</param-name>
                <param-value>7200</param-value> <!-- 5 days -->
        </init-param>
        <!-- This init param is also optional and default value is true. Set it false to override. -->
        <init-param>
                <param-name>useCache</param-name>
                <param-value>false</param-value>
        </init-param>
  </servlet>
 ...
 
Map this servlet to serve your JS and CSS resources
 ...
 <servlet-mapping>
   <servlet-name>JSCSSMergeServet</servlet-name>
   <url-pattern>*.js</url-pattern>
   <url-pattern>*.json</url-pattern>
   <url-pattern>*.css</url-pattern>
 </servlet-mapping>
 ...
 

In your web pages (HTML or JSP files) combine your multiple JS or CSS in one request as shown below.

To serve multiple JS files through one HTTP request

 <script language="JavaScript" src="/myapp/js/prototype,controls,dragdrop,myapp.js"></script>
 

To serve multiple CSS files through one HTTP request

 <link rel="StyleSheet" href="/myapp/css/infra,calendar,aquaskin.css"/>
 

Also if you wanted to serve them minified all together then you can add YUIMinFilter on them. See YUIMinFilter from webutilities.jar for details.

Init Parameters

Both init parameters are optional.

expiresMinutes has default value of 7 days. This value is relative from current time. Use negative value to expire early in the past. Ideally you should never be using negative value otherwise you won't be able to take advantage of browser caching for static resources.

  expiresMinutes - Relative number of minutes (added to current time) to be set as Expires header
  useCache - to cache the earlier merged contents and serve from cache. Default true.
 

Dependency

Servlet and JSP api (mostly provided by servlet container eg. Tomcat).

servlet-api.jar - Must be already present in your webapp classpath

Notes on Cache

If you have not set useCache parameter to false then cache will be used and contents will be always served from cache if found. Sometimes you may not want to use cache or you may want to evict the cache then using URL parameters you can do that.

URL Parameters to skip or evict the cache

 _skipcache_ - The JS or CSS request URL if contains this parameters the cache will not be used for it.
 _dbg_ - same as above _skipcache_ parameters.
 _expirecache_ - The cache will be cleaned completely. All existing cached contents will be cleaned.
 
 Eg.
 <link rel="StyleSheet" href="/myapp/css/infra,calendar,aquaskin.css?_dbg=1"/>
 or
 <script language="JavaScript" src="/myapp/js/prototype,controls,dragdrop,myapp.js?_expirecache_=1"></script>
 

Limitations

The multiple JS or CSS files can be combined together in one request if they are in same parent path. eg. /myapp/js/a.js, /myapp/js/b.js and /myapp/js/c.js can be combined together as /myapp/js/a,b,c.js. If they are not in infra path then they can not be combined in one request. Same applies for CSS too.

Visit http://code.google.com/p/webutilities/wiki/JSCSSMergeServlet for more details. Also visit http://code.google.com/p/webutilities/wiki/AddExpiresHeader for details about how to use for setting expires/Cache control header.

Version:
2.0
Author:
rpatil
See Also:
Serialized Form

Field Summary
static String INIT_PARAM_AUTO_CORRECT_URLS_IN_CSS
           
static String INIT_PARAM_CACHE_CONTROL
           
static String INIT_PARAM_CUSTOM_CONTEXT_PATH_FOR_CSS_URLS
           
static String INIT_PARAM_EXPIRES_MINUTES
           
static String INIT_PARAM_TURN_OFF_E_TAG
           
static String INIT_PARAM_TURN_OFF_URL_FINGERPRINTING
           
 
Constructor Summary
JSCSSMergeServlet()
           
 
Method Summary
protected  void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp)
           
 void init(javax.servlet.ServletConfig config)
           
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INIT_PARAM_EXPIRES_MINUTES

public static final String INIT_PARAM_EXPIRES_MINUTES
See Also:
Constant Field Values

INIT_PARAM_CACHE_CONTROL

public static final String INIT_PARAM_CACHE_CONTROL
See Also:
Constant Field Values

INIT_PARAM_AUTO_CORRECT_URLS_IN_CSS

public static final String INIT_PARAM_AUTO_CORRECT_URLS_IN_CSS
See Also:
Constant Field Values

INIT_PARAM_TURN_OFF_E_TAG

public static final String INIT_PARAM_TURN_OFF_E_TAG
See Also:
Constant Field Values

INIT_PARAM_TURN_OFF_URL_FINGERPRINTING

public static final String INIT_PARAM_TURN_OFF_URL_FINGERPRINTING
See Also:
Constant Field Values

INIT_PARAM_CUSTOM_CONTEXT_PATH_FOR_CSS_URLS

public static final String INIT_PARAM_CUSTOM_CONTEXT_PATH_FOR_CSS_URLS
See Also:
Constant Field Values
Constructor Detail

JSCSSMergeServlet

public JSCSSMergeServlet()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException

doGet

protected void doGet(javax.servlet.http.HttpServletRequest req,
                     javax.servlet.http.HttpServletResponse resp)
              throws javax.servlet.ServletException,
                     IOException
Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
javax.servlet.ServletException
IOException


Copyright © 2011. All Rights Reserved.