1. 程式人生 > >jsp或java中設定頁面預設IE瀏覽版本

jsp或java中設定頁面預設IE瀏覽版本

在我們開發中使用不同IE瀏覽器版本,顯示的效果不一樣。

我們可以中程式碼中設定預設版本

jsp中按如下設定

<meta http-equiv="X-UA-Compatible" content="IE=5" >

<meta http-equiv="X-UA-Compatible" content="IE=7" >

<meta http-equiv="X-UA-Compatible" content="IE=8" >

<meta http-equiv="X-UA-Compatible" content="IE=edge" >

java中   

package common;
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 javax.servlet.http.HttpServletResponse;


public class IECompatibilityFilter
  implements Filter
{
  private FilterConfig filterConfig = null;
  String ieCompatible = null;


  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    throws IOException, ServletException
  {
    HttpServletRequest req = (HttpServletRequest)request;
    HttpServletResponse res = (HttpServletResponse)response;


    if ((this.ieCompatible == null) || 
      (this.ieCompatible.trim().equalsIgnoreCase("AUTO"))) {
      String userAgent = req.getHeader("User-Agent");
      if (userAgent != null)
        if (userAgent.contains("MSIE 7.0"))
        {
          res.setHeader("X-UA-Compatible", "IE=EmulateIE7");
        } else if (userAgent.contains("MSIE 8.0"))
        {
          res.setHeader("X-UA-Compatible", "IE=EmulateIE8");
        } else if (userAgent.contains("Trident/7.0"))
        {
          res.setHeader("X-UA-Compatible", "IE=EmulateIE10");
        } else if (this.ieCompatible == null)
        {
          res.setHeader("X-UA-Compatible", "IE=edge");
        }
    } else if (this.ieCompatible != null) {
      res.setHeader("X-UA-Compatible", this.ieCompatible);
    }


    chain.doFilter(req, res);
  }


  public void init(FilterConfig filterConfig)
  {
    this.filterConfig = filterConfig;
    if (this.filterConfig != null)
      this.ieCompatible = this.filterConfig
        .getInitParameter("X-UA-Compatible");
  }


  public void destroy()
  {
  }
}

web.xml 中

<filter>
<filter-name>IECompatibilityFilter</filter-name>
<filter-class>common.IECompatibilityFilter</filter-class>
<init-param>
<param-name>X-UA-Compatible</param-name>
<!--<param-value>IE=EmulateIE8</param-value>-->
<param-value>AUTO</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>IECompatibilityFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>