Handle HEAD requests

Most containers handle a HEAD request the same as a GET request and so include all logic to build the page. While only the header is requested this could be a waste of resources.

To solve this a filter can be made that filters HEAD requests and stops further processing of the request.

package app.filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class HeadFilter implements Filter {
    private static final Logger LOGGER = Logger.getLogger(HeadFilter.class);

    public void destroy() {
        // Do nothing

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        String method = ((HttpServletRequest) request).getMethod();
        LOGGER.debug("HeadFilter: dofilter with '" + method + "'");
        if (!method.equalsIgnoreCase("HEAD")) {
            chain.doFilter(request, response);

    public void init(FilterConfig filterConfig) throws ServletException {
        // Do nothing

Because this filter needs to be the first filter in the chain it can't be annotated while then the order is not deterministic. Instead the filter must be configured in web.xml:


 For HEAD requests authentication might not be needed. Put this in the same web.xml:

         <web-resource-name>Only HEAD</web-resource-name>