编程开发 > JSP > 文章内容

jsp用过滤器解决中文乱码问题的方法

2017-4-25编辑:ljnbset

jsp用过滤器解决中文乱码问题的方法

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文件中,配置过滤器

?

1

2

3

4

5

6

7

8

9

10

11

<filter>

 <filter-name>EncodingFilterfilter-name>

 <filter-class>pers.filter.EncodingFilterfilter-class>

 filter>

 

 

 

<filter-mapping>

 <filter-name>EncodingFilterfilter-name>

 <url-pattern>/*url-pattern>

 filter-mapping>

深入浅析Jsp中out.print和out.write的区别

热点推荐

登录注册
触屏版电脑版网站地图