首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

使用过滤器解决乱码有关问题

2012-12-24 
使用过滤器解决乱码问题此情况只使用与Servlet+jsp,对struts不适合public class CharacterFilter implemen

使用过滤器解决乱码问题

此情况只使用与Servlet+jsp,对struts不适合

public class CharacterFilter 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 stubrequest.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=UTF-8");MyRequest myrequest = new MyRequest((HttpServletRequest) request);chain.doFilter(myrequest, response);}public void init(FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub}}class MyRequest extends HttpServletRequestWrapper {HttpServletRequest request;public MyRequest(HttpServletRequest request) {super(request);this.request = request;}@Overridepublic String getParameter(String name) {String value = super.getParameter(name);if (request.getMethod().equalsIgnoreCase("get")) {try {value = new String(value.getBytes("iso8859-1"), "utf-8");} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return value;}}

热点排行