乱玛问题
来源:互联网 发布:最简单的c语言游戏代码 编辑:程序博客网 时间:2024/06/08 19:19
为保证jsp不出现乱玛问题Fiter可以这样写,供参考
SetCharacterEncodingFilter.java
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.UnavailableException;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
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;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
然后在在web.xml中配置下
<web-app>
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<display-name>setCharacterEncodingFilter</display-name>
<description>setCharacterEncodingFilter</description>
<filter-class>SetCharacterEncodingFilter类的路径</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
SetCharacterEncodingFilter.java
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.UnavailableException;
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException,
ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
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;
}
}
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
然后在在web.xml中配置下
<web-app>
<filter>
<filter-name>setCharacterEncodingFilter</filter-name>
<display-name>setCharacterEncodingFilter</display-name>
<description>setCharacterEncodingFilter</description>
<filter-class>SetCharacterEncodingFilter类的路径</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
...
</web-app>
- 乱玛问题
- Qt 中文乱乱问题
- 中文乱编问题
- 文件乱问题
- session乱窜问题
- TextView乱换行问题
- 输入法挤乱布局问题
- RecyclerView+Checkbox乱序问题
- RecyclerView+Checkbox乱序问题
- 乱序字符串的问题
- 360 排版乱的问题
- 乱
- 乱
- 乱
- 乱
- 乱
- 乱
- 乱
- 教你理解复杂的C/C++声明
- 别吵了,.NET和JAVA两大帮派合并了!
- boolean的getter 和setter在JSF中的问题
- CListCtrl详解
- 几个.Net开源的CMS系统
- 乱玛问题
- EJB 调用原理分析
- WORK
- delphi2007全新体验
- 《大家的日语1》语法整理1~48课
- OSWorkflow 概念
- SQL 2000中的触发器使用
- 《大家的日语2》语法整理1~48课
- 发哥服务器站点