Support for ETag

In this section, you will learn about how to use ETag in Spring MVC.

Support for ETag

Support for ETag

In this section, you will learn about how to use ETag in Spring MVC.

An entity tag or Etag is part of HTTP which provides web cache validation mechanism. This mechanism saves bandwidth because a full response need not send if content is not modified. HTTP/1.1 supported web server utilize Etag , which is an HTTP response header, to find out changes at provided URL.

It can be a good replacement of Last-Modified header. The client can utilize the Etag header, returned by the server with a representation, in subsequent GETs, in an If-None-Match header. If the content at given URL is not changed, the server returns 304:Not Modified.

Servlet filter ShallowEtagHeaderFilter provides ETag support and since it is a plain Servlet Filter , it can be implement with any framework. The ShallowEtagHeaderFilter cache the JSP/ content and creates an MD5 hash over that and returns MD5 hash as an ETag header in the response.

Next time the same resource is requested by a client, it utilizes that MD5 hash as the If-None-Match value. The filter notices this and view is again render by it. After rendering , it compares the two hashes for equality. If they are equal - 304 is returned. This filter ShallowEtagHeaderFilter doesn't save processing because it renders each time to compare hash. It only saves bandwidth since rendered response is not sent back on the bandwidth.

ShallowEtagHeaderFilter can be configure in the web.xml as below :

<filter>
	<filter-name>etagFilter</filter-name>
	<filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class>
</filter>

<filter-mapping>
	<filter-name>etagFilter</filter-name>
	<servlet-name>XYZ</servlet-name>
</filter-mapping>