<%@ page language="java" pageEncoding="GBK" %>
或者<%@ page contenttype="text/html;char=gbk";>这里可以用gb2312或者gbk只是gbk比gb2312支持跟多
这个思路方法用于jsp页面中中文显示
思路方法 2:使用过滤器
过滤器使用主要针对表单提交插入数据库数据都是?号这也是应为tomcat不按request所指定编码进行编码还是自作主张采用默认编码方式iso-8859-1编码
编写个SetCharacterEncodingFilter类
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 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");
(valuenull)
this.ignore=true;
(value.equalsIgnoreCase("true"))
this.ignore=true;
this.ignore=false;
}
public void doFilter(
ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO 自动生成思路方法存根
(ignore (request.getCharacterEncoding null)) {
String encoding = selectEncoding(request);
(encoding != null)
request.CharacterEncoding(encoding);
}
chain.doFilter(request, response);
}
public void destroy {
// TODO 自动生成思路方法存根
this.encoding = null;
this.filterConfig = null;
}
protected String selectEncoding(ServletRequest request) {
(this.encoding);
}
}
最新评论