同样的必须将所有的应用到的字符编码都定义成一样的(UTF-8)等等
特别要留意的地方是在server.xml中也要加上<URIEncoding="utf-8">
<Connector port="8090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIencoding="UTF-8"/>
同样创建一个类FilterCoder
public class FilterCode implements Filter {
FilterConfig config=null;
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain Chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
System.out.println("do the filter");
req.setCharacterEncoding(config.getInitParameter("code"));
Chain.doFilter(request, response);
HttpServletResponse rep = (HttpServletResponse) response;
rep.setCharacterEncoding(config.getInitParameter("code"));
}
public void init(FilterConfig arg0) throws ServletException {
this.config=arg0;
}
}
FilterCoder不用讲应该也会设置他的url作用域吧
<filter>
<filter-name>FilterCode</filter-name>
<filter-class>FilterCode</filter-class>
<init-param>
<param-name>code</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FilterCode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
ok! FilterCoder的设置就完成了
还有一点值得注意的地方:
就是你想在servlet中也要out.println("中文");
就必须response.setContentType("text/html;charset=UTF-8");也要加上去!
大功告成了!!!!!!!