JSP web.xml统一页面编码
2013-04-09 17:16
211 查看
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;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO 自动生成方法存根
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void destroy() {
// TODO 自动生成方法存根
this.encoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
web.xml中:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.yourcompany.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意url-pattern是/*。
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO 自动生成方法存根
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void destroy() {
// TODO 自动生成方法存根
this.encoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
web.xml中:
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.yourcompany.util.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
注意url-pattern是/*。
相关文章推荐
- JSP---web.xml中统一配置全站错误处理页面,提高网站的友好度
- JSP——web.xml中配置错误页面
- struts2项目web.xml里加filter后,jsp页面不显示问题
- web.xml中DispatcherServlet的url-pattern配置错误导致JSP页面在浏览器中显示为源码
- java,jsp错误页面在web.xml中的设置
- 如何在JSP页面中访问web.xml中的初始化参数?
- javaJSP实现页面的统一编码
- 关于jsp web项目,jsp页面与servlet数据不同步的解决办法(报错404、405等)即访问.jsp和访问web.xml中注册的/servlet/的区别
- web.xml中设置统一的自定义错误页面
- 使用web.xml配置Web应用之初始化和预加载servlet和JSP页面
- 在JSP页面中访问web.xml中的初始化参数
- web.xml或JSP中指定错误页面
- JSP页面统一编码过滤器
- 读取web.xml参数,jsp动作指令,jsp页面发布xml或者pdf数据
- web.xml配置struts2之后访问jsp页面报Http 404错误
- 在web.xml配置错误信息跳转页面
- web.xml配置jsp-file例子
- Web开发_问题解决(3)之linux服务器上获取路径问题||jsp页面报错问题
- jsp去除空行的web.xml配置
- myeclipse中的jsp页面pageEncoding怎样设置默认编码为“UTF-8”