Tomcat和weblogic中文乱码问题解决方案乱码的根本原因是编码和解码的不一致,所以只要把它们置成一样的就可以了。解决乱码最好的方法是增加过滤器。关键代码只有三行:response.setCharacterEncoding(UTF-8);response.setContentType(text/html;charset=UTF-8);request.setCharacterEncoding(UTF-8);使用tomcat服务器可以用一下过滤器先添加一个java类packagecom;importjava.io.IOException;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletRequestWrapper;publicclassTomcatFormFilterimplementsFilter{classRequestextendsHttpServletRequestWrapper{publicRequest(HttpServletRequestrequest){super(request);}/***转换由表单读取的数据的内码.*从ISO字符转到GBK.*/publicStringtoChi(Stringinput){try{//byte[]bytes=input.getBytes(ISO8859-1);//returnnewString(bytes,GBK);byte[]bytes=input.getBytes(ISO8859-1);returnnewString(bytes,GBK);}catch(Exceptionex){}returnnull;}/***ReturntheHttpServletRequestholdedbythisobject.*/privateHttpServletRequestgetHttpServletRequest(){return(HttpServletRequest)super.getRequest();}/***读取参数--修正了中文问题.*/publicStringgetParameter(Stringname){returntoChi(getHttpServletRequest().getParameter(name));}/***读取参数列表-修正了中文问题.*/publicString[]getParameterValues(Stringname){Stringvalues[]=getHttpServletRequest().getParameterValues(name);if(values!=null){for(inti=0;ivalues.length;i++){values[i]=toChi(values[i]);}}returnvalues;}}publicvoiddestroy(){}publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainchain)throwsIOException,ServletException{HttpServletRequesthttpreq=(HttpServletRequest)request;if(httpreq.getMethod().equals(POST)){request.setCharacterEncoding(GBK);}else{request=newRequest(httpreq);}chain.doFilter(request,response);}publicvoidinit(FilterConfigfilterConfig)throwsServletException{}}再在web.xml中注册这个过滤器,注意红色字体为添加的部分?xmlversion=1.0encoding=UTF-8?web-appxmlns=:xsi==2.4xsi:schemaLocation=://java.sun.com/xml/ns/j2ee/web-app_2_4.xsdservletservlet-nameaction/servlet-nameservlet-classorg.apache.struts.action.ActionServlet/servlet-classinit-paramparam-nameconfig/param-nameparam-value/WEB-INF/struts-config.xml/param-value/init-paraminit-paramparam-namedebug/param-nameparam-value3/param-value/init-paraminit-paramparam-namedetail/param-nameparam-value3/param-value/init-paramload-on-startup0/load-on-startup/servletfilterfilter-nameTomcatFormFilter/filter-namefilter-classcom.TomcatFormFilter/filter-class/filterfilter-mappingfilter-nameTomcatFormFilter/filter-nameurl-pattern/*/url-pattern/filter-mappingservlet-mappingservlet-nameaction/servlet-nameurl-pattern*.do/url-pattern/servlet-mappingcontext-paramparam-nameweblogic.httpd.inputCharset./*/param-nameparam-valueGBK/param-value/context-paramwelcome-file-listwelcome-fileUserSearch/UserSearch.jsp/welcome-file/welcome-file-list/web-app然而当我们将项目一到weblogic的时候,又有可能出现乱码,那么如何调整过滤器呢?weblogic中的过滤器如下:packagecom;importjava.io.IOException;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;publicclassweblogicfilterimplementsFilter{publicvoidinit(FilterConfigarg0)throwsServletException{}publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,FilterChainfilterChain)throwsIOException,ServletException{response.setCharacterEncoding(UTF-8);response.setContentType(text/html;charset=UTF-8);request.setCharacterEncoding(UTF-8);filterChain.doFilter(request,response);filterChain.doFilter(request,response);}publicvoiddestroy(){}}然后再web.xml中注册过滤器?xmlversion=1.0encoding=UTF-8?web-appxmlns=:xsi==2.4xsi:schemaLocation=://java.sun.com/xml/ns/j2ee/web-app_2_4.xsdservletservlet-nameaction/servlet-nameservlet-classorg.apache.struts.action.ActionServlet/servlet-classinit-paramparam-nameconfig/param-nameparam-value/WEB-INF/struts-config.xml/param-value/init-paraminit-paramparam-namedebug/param-nameparam-value3/param-value/init-paraminit-paramparam-namedetail/param-nameparam-value3/param-value/init-paramload-on-startup0/load-on-startup/servlet!--filterfilter-nameTomcatFormFilter/filter-namefilter-classcom.TomcatFormFilter/filter-class/filterfilter-mappingfilter-nameTomcatFormFilter/filter-nameurl-pattern/*/url-pattern/filter-mapping--filterfilter-nameweblogicfilter/filter-namefilter-classcom.weblogicfilter/filter-class/filterservlet-mappingservlet-nameaction/servlet-nameurl-pattern*.do/url-pattern/servlet-mappingcontext-paramparam-nameweblogic.httpd.inputCharset./*/param-nameparam-val