1.首先要保证jsp页面的编码格式、数据库编码格式、java文件保存格式、服务器server.xml中的URIEncoding编码格式保持一致
2 手动写一个过滤器,并且在web.xml文件中进行配置。
具体代码参考如下(调试通过):
package cn.com.model2.util;
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 CharacterEncodingFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
}
}
在web.xml文件中进行配置:
<filter>
<filter-name>CharacterEncoding Set</filter-name>
<filter-class>cn.com.model2.util.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncoding Set</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>