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

[经验分享] JSP+MySQL+Tomcat+Apache乱码解决一例

[复制链接]

尚未签到

发表于 2015-8-10 15:45:37 | 显示全部楼层 |阅读模式
  
  本文以最常见的JSP+MySQL+Tomcat+Apache乱码解决为例,望能为你的环境配置起到抛砖引玉之效!
  乱码问题已历来已久,在开源环境下,乱码问题更是令程序员措手不及。本人在Unix(Freebsd)下的一次乱码经历可谓经典,故撰以此文以滋效尤!
  我将本次所遇乱码归为三类:
  1.页面字符乱码
  2.记录显示乱码
  3.request传递乱码
  以下将对上述三类乱码进行解析:
一.页面字符乱码:
  1.大小写不一致:
org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html;charset=gb2312, new: text/html;charset=GB2312)
  2.间隔不一致:
org.apache.jasper.JasperException: /top.jsp(1,1) Page directive: illegal to have multiple occurrences of contentType with different values (old: text/html; charset=GB2312, new: text/html;charset=GB2312)
*解决方案:
首先,在Apache中增加AddDefaultCharset GB2312或AddDefaultCharset GBK
其次,统一使用页面编码定义,如:<%@page contentType="text/html;charset=GB2312"%>
*注:GB2312为GBK之子集。
二.记录显示乱码:
  1.MySQL默人语言为latin1_swedish_ci,即拉丁语,所以取出的中文全是乱码。
*解决方案:
  1.将charset设为8859_1即:<%@page contentType="text/html;charset=8859_1"%>
  这个方法只能暂时缓解字符显示问题,并权益之计。因为8859_1为字节型字库,并非字型字库,故在非全角状态下,将出现半字乱码,表现为&#8220;?&#8221;。
  2.在数据库连接语句中加上?useUnicode=true;characterEncoding=GBK,如:
jdbc:mysql://localhost/dbname?useUnicode=true;characterEncoding=GBK
*注:一般教科书上都会加上localhost:3306,因为默认端口为3306,故可舍去!同时,请使用连接池的朋友注意,在注册xml文件时,是不可以单独出现&#8220;;&#8221;的,所以必须使用&#8220;&amp;&#8221;,即:jdbc:mysql://localhost/dbname?useUnicode=true&amp;characterEncoding=GBK。
  否则提示出错:
Parse Fatal Error at line 213 column 91: The reference to entity "characterEncoding" must end with the ';' delimiter.
org.xml.sax.SAXParseException: The reference to entity "characterEncoding" must
end with the ';' delimiter.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Un
known Source)
  也曾有人提意:在MySQL的my.ini文件中加入default-character-set=gbk,本人不赞同此法,因为这样破坏了原有的环境,除非这是MySQL的第一个站点。
三.request传递乱码:
  1.也许,此时你已经可以正常使用系统了,那么恭喜~乱码问题已经离开你了!但是,大伙通常都没那么走运,乱码问题依旧存在。也许,这时你向数据库添加了一条记录以测试系统,可是此时显示出的还是乱码。那么可以肯定是Request参数传递出错!那么先写个测试语句:<%= request.getParameter(&#8220;Para&#8221;) %>,OK,果然是乱。那么,现在有两种解决方法。
*解决方案:
  1.加上这条语句:request.setCharacterEncoding("gbk");
在一/两页上可行,但此法也非权益之计。
  2.注册SetCharacterEncodingFilter类:
  首先,编写SetCharacterEncodingFilter.java文件,代码如下:
package cn.com.jsp;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;
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 {
        // Conditionally select and set the character encoding to be used
        if (ignore || (request.getCharacterEncoding() == null)) {
            String encoding = selectEncoding(request);
            if (encoding != null) {
                request.setCharacterEncoding(encoding);
            }
        }
        // Pass control on to the next filter
        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);
    }
}
  此文件为request过滤类,在全局编译前需进行注册。
  注册文件为:<%wwwroot%>/WEB-INF/web.xml。
  在此文件中加入如下代码即可:
<web-app>
  <display-name>wwwroot</display-name>
  <description>MySQL Test App</description>
  <filter>
    <filter-name>setCharacterEncodingFilter</filter-name>
    <display-name>setCharacterEncodingFilter</display-name>
    <description>setCharacterEncodingFilter</description>
    <filter-class>cn.com.jsp.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>/*</url-pattern>
  </filter-mapping>
&#8230;&#8230;
</web-app>

  

运维网声明 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-97084-1-1.html 上篇帖子: 运行 tomcat 错误 : java.lang.Exception: Socket bind failed: [730048] 解决方法: 下篇帖子: tomcat中虚拟目录的设置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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