X-UA-Compatible Java Servlets / JSF

9 Mar
2010

A User on the glassfish mailing list posted a question:

I would like to aks it. How must set up ie8 compatibility mode in glassfish?
This is the iis setting:

<system.webServer>
       <httpProtocol>
               <customHeaders>
                       <clear />
                       <add name="X-UA-Compatible" value="IE=EmulateIE7" />
               </customHeaders>
       </httpProtocol>
</system.webServer>

Well… how to do that?

Simply create a Servlet Filter!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.dominikdorn.dc.filters;
 
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;
 
 
@WebFilter(urlPatterns = {"/*"}, initParams = {@WebInitParam(name = "compatibilityMode", value = "IE=EmulateIE7")})
public class UserAgentCompatibleFilter implements javax.servlet.Filter {
    private Logger log = Logger.getLogger("UserAgentCompatibleFilter");
    private String compatibilityMode;
 
    public void destroy() {
    }
 
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        if (compatibilityMode != null) {
            HttpServletResponse res = (HttpServletResponse) resp;
            res.addHeader("X-UA-Compatible", compatibilityMode);
        }
        chain.doFilter(req, resp);
    }
 
    public void init(FilterConfig config) throws ServletException {
        compatibilityMode = config.getInitParameter("compatibilityMode");
        if (compatibilityMode == null) {
            log.warning("No CompatibilityMode set for UserAgentCompatibleFilter, thus disabling it");
        }
    }
}

If you’re not using Servlet 3.0, simply comment out the @WebServlet annotation. If you want to customize
the header, add this to your web.xml and modify it, so that it suit your needs.

1
2
3
4
5
6
7
8
9
10
11
12
13
    <filter>
        <filter-name>UserAgentCompatibleFilter</filter-name>
        <filter-class>com.dominikdorn.dc.filters.UserAgentCompatibleFilter</filter-class>
        <init-param>
            <param-name>compatibilityMode</param-name>
            <param-value>IE=EmulateIE7</param-value>
        </init-param>
    </filter>
 
    <filter-mapping>
        <filter-name>UserAgentCompatibleFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

To see if it works, compile and redeploy your app.. you can then test it, e.g. with curl:

curl -D - http://localhost:8080/info.xhtml
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0
Server: GlassFish v3
Set-Cookie: JSESSIONID=3afbe6498aa3f495e8340d8e67ef; Path=/
X-UA-Compatible: IE=EmulateIE7
X-Powered-By: JSF/2.0
Content-Type: text/html;charset=UTF-8
Content-Length: 2997
Date: Tue, 09 Mar 2010 16:09:03 GMT

Suggestions? Comment here!

1 Response to X-UA-Compatible Java Servlets / JSF

Avatar

Juhász Tibor

March 10th, 2010 at 10:58

Hi Dominik!

Thanks your solution! This work nice!

Comment Form

top