I have an html file that can be accessed by browsing to
https://localhost:8080/contextRoot/home.html
This html uses 2 images:
<img src="https://localhost:8080/contextRoot/image1.jpg">
<img src="https://localhost:8080/static/images/image2.jpg">
The first image is packaged in my war file and loads fine. When I reload the page, it is fetched from cache instead of re-downloading it. I see this in the developer tools of my browser.
The second image also loads fine, but it is downloaded every time the page is requested. It is never cached. It uses a special java servlet to handle what we call static content:
<servlet>
<servlet-name>staticFileServlet</servlet-name>
<servlet-class>com.company.web.file.StaticFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>staticFileServlet</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
This servlet searches the computer's disk for the folder C://images/
for a file called image1
and serves it by writing those bytes to the response, while also heading a content-type header to the response (so the browser knows what kind of file it is receiving).
I think I might have to add additional headers to explain the browser that this content should be cached. Can the Cache-control
header help me here? However, I thought browsers were smart enough to cache requests regardless of what headers I (don't) use.
Here are the response headers for an image that is succesfully cached (served by being the war file)
Accept-Ranges:bytes
Content-Length:354
Content-Type:image/gif
Date:Mon, 04 Jan 2016 09:43:42 GMT
ETag:W/"354-1449227028000"
Last-Modified:Fri, 04 Dec 2015 11:03:48 GMT
Server:Apache-Coyote/1.1
Here is an example of an image that is served by the servlet and isn't cached:
Cache-Control:max-age:864000
Content-Type:image/jpeg
Date:Mon, 04 Jan 2016 13:59:04 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
EDIT: my files are behind an SSL connection, which could cause the deny of caching. However, I'm certain it is not the server denying this caching because
- It is caching some images.
- There no headers (pregma, etag, cache-control...) set on the response.
Does google chrome automatically refuse caching from (some) ssl connections?