定義一過濾器,實現Filtter接口
public class EncodingFilter implements Filter { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest request=(HttpServletRequest)req; HttpServletResponse response=(HttpServletResponse)resp; request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); chain.doFilter(req, resp); } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub } }
web.xml文件中,配置過濾器
<filter> <filter-name>EncodingFilter</filter-name> <filter-class>pers.filter.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
以上這篇jsp用過濾器解決中文亂碼問題的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持。