设置Tomcat及Weblogic字符编码,解决数据提交的乱码问题
本文原文来自:http://www.cublog.cn/u/32768/showart_360298.html
设置Tomcat及Weblogic字符编码,解决数据提交的乱码问题
【关键字】Tomcat容器中文字符编码乱码Weblogic容器服务器
【正文】因为编码方式的不同在使用Tomcat容器时会出现提交到Servlet的中文是乱码的方式,而且Tomcat5.x对于POST和GET的方式处理似乎还有不同,POST方式是采用Filter的方式即可,怎样能够处理GET方式中文提交乱码的问题呢?
可以采用配置服务器字符编码的方法,具体操作如下:
1、打开Tomcat安装目录中的conf目录
2、修改server.xml中的connector一个子项,具体可能类似如下:
<Connector port="8001" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" />
在其中添加URIEncoding="GBK",或者是其他的编码方式,变成如下:
<Connector port="8001" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" redirectPort="8443" acceptCount="100" debug="0" connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK" />
3、停止Tomcat服务,重新启动Tomcat服务即可。
【另附Weblogic容器处理编码的方式】
处理Weblogic容器的编码比较简单,只需要在站点的web.xml中配置一行如下的代码即可。
<context-param> <param-name>weblogic.httpd.inputCharset./*</param-name> <param-value>GBK</param-value> </context-param>
【另附Tomcat处理POST提交乱码的方式】
1、首先在web.xml中配置过滤器
<filter> <filter-name>SetCharacterEncodingFilter</filter-name> <filter-class> cn.cublog.jedliu.SetCharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>SetCharacterEncodingFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping>
2、在cn.cublog.jedliu包中SetCharacterEncodingFilter是用于来实现编码的过滤器。
public class SetCharacterEncodingFilter implements Filter { protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void destroy() { this.encoding = null; this.filterConfig = null; } public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException { if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); } chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else if (value.equalsIgnoreCase("yes")) this.ignore = true; else this.ignore = false; } protected String selectEncoding(ServletRequest request) { return (this.encoding); } }
相关推荐
yiyilanmei 2020-08-03
liusarazhang 2020-06-28
89411051 2020-06-14
mjshldcsd 2020-06-14
王艺强 2020-06-12
FORYAOSHUYUN 2020-06-11
88540591 2020-06-01
LUOPING0 2020-05-18
webliyang 2020-05-07
81214051 2020-04-25
84590091 2020-04-22
88384957 2020-03-27
angqiuli 2020-02-16
SDUTACM 2020-02-15
Airuio 2020-01-28
jling 2020-01-28
nan00zzu 2020-01-06