|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjavax.servlet.GenericServlet
javax.servlet.http.HttpServlet
com.googlecode.webutilities.servlets.JSCSSMergeServlet
public class JSCSSMergeServlet
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.
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.
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.
Servlet and JSP api (mostly provided by servlet container eg. Tomcat).
servlet-api.jar - Must be already present in your webapp classpath
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.
_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>
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.
| 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 |
|---|
public static final String INIT_PARAM_EXPIRES_MINUTES
public static final String INIT_PARAM_CACHE_CONTROL
public static final String INIT_PARAM_AUTO_CORRECT_URLS_IN_CSS
public static final String INIT_PARAM_TURN_OFF_E_TAG
public static final String INIT_PARAM_TURN_OFF_URL_FINGERPRINTING
public static final String INIT_PARAM_CUSTOM_CONTEXT_PATH_FOR_CSS_URLS
| Constructor Detail |
|---|
public JSCSSMergeServlet()
| Method Detail |
|---|
public void init(javax.servlet.ServletConfig config)
throws javax.servlet.ServletException
init in interface javax.servlet.Servletinit in class javax.servlet.GenericServletjavax.servlet.ServletException
protected void doGet(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
throws javax.servlet.ServletException,
IOException
doGet in class javax.servlet.http.HttpServletjavax.servlet.ServletException
IOException
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||