Jetty Commom things
Enable Gzip Compression on Jetty 8
Gzip Compression is easy to configure for Jetty and the results will be impressive if it is done correctly.On my experiments I see that, html files are compressed up to %80, JS and CSS files are compressed up to %70. Gzip Filter makes your job very easy for this operation. Adding this filter to your web.xml;
| 1 2 3 4 5 6 7 8 9 10 11 12 | <filter>    <filter-name>GzipFilter</filter-name>    <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>    <init-param>        <param-name>mimeTypes</param-name>        <param-value> text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,application/x-javascript,image/svg+xml </param-value>    </init-param></filter><filter-mapping>    <filter-name>GzipFilter</filter-name>    <url-pattern>/*</url-pattern></filter-mapping> | 
In Mime Type’s parameter, you can specify the types that need to be compressed. Note that, compressing images is not recommended.
In Filter Mapping , you can specify which url pattern in case you like to apply compression to specific URL’s.
Set Http headers to static files on Jetty and Cache Control
There are two ways to do this on Jetty. One of them is to use the DefaultServlet class. In case the initial parameters does not cover your needs, you can override the DefaultServlet , but i will do this in the next post. Now, i like to show you how to use the the DefaultServlet class to set http headers to our static files such as images, scripts and style pages.
We start by adding the servlet to the web.xml file;
| 1 2 3 4 5 6 | <servlet>    <servlet-name>default</servlet-name>    <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>        <load-on-startup>1</load-on-startup></servlet> | 
After this, what you need to know is adding the necessary initial parameters to get the headers you need, Let’s start by acceptRanges parameter, to get the Accept-Ranges header you need to add,
| 1 2 3 4 | <init-param>    <param-name>acceptRanges</param-name>    <param-value>true</param-value></init-param> | 
Other useful header you might want is Cache-Control header, this header enables caching in client side, reduces both load time and server load. To enable this,
| 1 2 3 4 | <init-param>    <param-name>cacheControl</param-name>    <param-value>max-age=604800,public</param-value></init-param> | 
max-age specifies expiration date of the static file you cached in seconds.(604800 seconds equals to 1 week which is the time recommended by Google). Important thing to keep in mind is that, once you enable this option and your file is cached in client, there will be no request for this file to your server until the file exprires. This means if you make any changes in your files, they will not take effect. You need to change the names of the files that you edit before publishing. The common practice is using a random string or a version number. This is the main reason why sites like Facebook or Google have scripts named sem_20274d81add4f21f9f6be6beb53336cd.js. If you like you can check the source code and the javadoc of DefaultServlet to get detailed options;
JavaDoc : http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/servlet/DefaultServlet.html
Source Code : http://download.eclipse.org/jetty/stable-7/xref/org/eclipse/jetty/servlet/DefaultServlet.html
Set Http headers to static files on Jetty with a custom class
In previous article, I tried to explain how to use DefaultServlet to set headers by using it as a filter in web.xml. However it may not cover your need in some cases. In this situation you can always override DefaultServlet. Let’s make an example;
In this example case , I will add “Vary:Accept Encoding” header to my .js and .css files. This is a practice recommended by Google , however jetty does not add this parameter by default even though , you add the gzip true paramater. So in this case we will override doGet function in DefaultServlet. Let’s call our class MyDefaultServlet, to include this class we add below code;
| 1 2 3 4 5 | <servlet>    <servlet-name>default</servlet-name>    <servlet-class>com.bahadir.action.MyDefaultServlet</servlet-class>    <load-on-startup>1</load-on-startup></servlet> | 
You can add the same parameters in DefaultServlet as long as you extend from it.After that you override doGet function to make neccessery changes in the response;
| 1 2 3 4 5 6 7 8 9 10 11 | publicclassMyDefaultServlet extendsDefaultServlet {    @Override    protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException    {        if(request.getServletPath().endsWith(".css") || request.getServletPath().endsWith(".js")){            response.setHeader(HttpHeaders.VARY,HttpHeaders.ACCEPT_ENCODING);        }        super.doGet(request, response);    }} | 
You may need to add the following jars to your project;
- jetty-servlet.jar
- jetty-server.jar
- jetty-util.jar
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号