web.xmljava
<!-- 編碼過濾器 --> <filter> <filter-name>guolvqi</filter-name> <filter-class>com.prozj.filter.GuoLvQi</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>guolvqi</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
filter實現類web
private String coding=""; public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { HttpServletRequest req=(HttpServletRequest)request; HttpServletResponse resp=(HttpServletResponse)response; req.setCharacterEncoding(coding); resp.setCharacterEncoding(coding); chain.doFilter(req, resp); } public void init(FilterConfig filterConfig) throws ServletException { coding=filterConfig.getInitParameter("encoding")==null?"gbk":filterConfig.getInitParameter("encoding"); }