JSP/Servlet设置编码格式的问题(Android)
刚刚做了一个Android前台与后台Java的通信,查询回来到客户端的数据,中文出现乱码,没有什么概念,因为这两年来一直做swing,把web的东西给忘记了,现在备忘一下!
此文章借签于:http://blog.csdn.net/w627782664/article/details/7064442
新建一个Filter:
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 { private FilterConfig filterConfig = null; private String encoding = null; @Override public void destroy() { System.out.println("CharacterEncodingFilter destroy mehod!"); this.filterConfig = null; this.encoding = null; } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("CharacterEncodingFilter doFilter mehod!"); request.setCharacterEncoding(this.encoding); response.setCharacterEncoding(this.encoding); chain.doFilter(request, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("CharacterEncodingFilter init mehod!"); this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); } }
在web.xml里面,做如下配置:
<filter> <description>CharacterEncodingFilter</description> <display-name>CharacterEncodingFilter</display-name> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>com.may.dao.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
整个编码格式已经完成!