设为首页 收藏本站
查看: 553|回复: 0

[经验分享] 设置Tomcat及Weblogic字符编码,解决数据提交的乱码问题

[复制链接]
发表于 2017-2-7 10:31:48 | 显示全部楼层 |阅读模式
本文原文来自: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);
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-338702-1-1.html 上篇帖子: Tomcat 7 新特性学习之二 改变JSessionID来防止固定SessionID攻击 下篇帖子: tomcat应用中使用aspectj输出服务器端的访问日志
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表