问题描述
哪位大哥帮我看看我写了一个过滤器,首次登入保存数据到sqlserver2000出现乱码,以后就不会出现乱码了。代码如下:publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{//TODOAuto-generatedmethodstub//placeyourcodehere//HttpServletRequestreq=(HttpServletRequest)request;HttpServletRequestreq=(HttpServletRequest)request;HttpServletResponseres=(HttpServletResponse)response;res.setCharacterEncoding("UTF-8");req.setCharacterEncoding("UTF-8");//passtherequestalongthefilterchainchain.doFilter(req,res);}
解决方案
解决方案二:
这个过滤器过滤的url你没贴出来。。。可能是刚好就那么一个url没有经过这个过滤器的过滤
解决方案三:
你数出流设置不对吧应该是req.setContenType("text/html;charset=UTF-8");
解决方案四:
publicclassFilterEncodingimplementsFilter{privateFilterConfigfilterConfig=null;privateStringencoding=null;/*(non-Javadoc)*@seejavax.servlet.Filter#init(javax.servlet.FilterConfig)*/publicvoidinit(FilterConfigfilterConfig)throwsServletException{this.filterConfig=filterConfig;this.encoding=filterConfig.getInitParameter("encoding");}/*(non-Javadoc)*@seejavax.servlet.Filter#doFilter(javax.servlet.ServletRequest,javax.servlet.ServletResponse,javax.servlet.FilterChain)*/publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{Stringencoding=this.encoding;if(encoding!=null){request.setCharacterEncoding(encoding);}chain.doFilter(request,response);}/*(non-Javadoc)*@seejavax.servlet.Filter#destroy()*/publicvoiddestroy(){this.encoding=null;this.filterConfig=null;}}
解决方案五:
HttpServletRequestreq=(HttpServletRequest)request;HttpServletResponseres=(HttpServletResponse)response;res.setCharacterEncoding("UTF-8");req.setCharacterEncoding("UTF-8");添加一句:req.setContentType("text/html;charset=GBK");