zoukankan      html  css  js  c++  java
  • jsp中文乱码完美解决方案(原创) java程序员

    关于UTF-8B编码:

    UTF-8是UNICODE的一种变长字符编码又称万国码,由Ken Thompson于1992年创建。现在已经标准化为RFC 3629。UTF-8用1到6个字节编码UNICODE字符。用在网页上可以同一页面显示中文简体繁体及其它语言(如日文,韩文)

    我们只关注UTF-8能支持中文,详细请参考相关书籍。

    关于jsp编码的说明:


     

    <%@ page language="java" pageEncoding="gb2312"%><!--第一处-->

    <%@ page contentType="text/html;charset=iso8859-1"%><!--第二处-->

    <html>

    <head>

    <title>JSP的中文处理</title>

    <meta http-equiv="Content-Type" content="text/html charset=gb2312"><!--第三处-->

    </head>

    <body>

    <%out.print("JSP的中文处理");%>

    </body>

    </html>

    第一处<%@ page language="java" pageEncoding="gb2312"%>的编码格式为jsp文件的存储格式。Eclipse会根据这个编码格式保存文件。并编译jsp文件,包括里面的汉字。
    第二处编码为解码格式。因为存为gb2312的文件被解码为iso8859-1,这样如有中文肯定出乱码。也就是必须一致。而第二处所在的这一行,可以没有。缺省也是使用iso8859-1的编码格式。所以如果没有这一行的话,也会出现乱码。必须一致才可以。
    第三处编码为控制浏览器的解码方式。如果前面的解码都一致并且无误的话,这个编码格式用不用设置都可以。有的网页出现乱码,就是因为浏览器不能确定使用哪种编码格式。因为页面有时候会嵌入页面,导致浏览器混淆了编码格式出现了乱码。

    解决乱码的基本思路:
    所有的编码和解码都用UTF-8,因为只有UTF-8才能支持中文,再也不用
    String s=new String(request.getParameter("name").getBytes("ISO-8859-1"),"gb2312") 
    这样繁琐的转换了。
    解决乱码的基本步骤:

    1 、建立如下的过滤器类:
    package com.util;
    //public class CharacterEncodingFilter {
    //}
    /*
     * 设置网页的编码过滤类;
     */
    import java.io.IOException;
    import javax.servlet.*;
    //import javax.servlet.http.*;
    //import java.io.*;
    //import java.util.*;
    /**
     *
     * @author
     */
    public class CharacterEncodingFilter implements Filter{
        protected String encoding = null;
        protected FilterConfig filterConfig = null;
        public void init(FilterConfig filterConfig) throws ServletException {
            this.filterConfig = filterConfig;
            this.encoding = filterConfig.getInitParameter("encoding");
        }
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            //if (encoding != null) {
                request.setCharacterEncoding(encoding);
                response.setContentType("text/html; charset="+encoding);
           // }
            chain.doFilter(request, response);
        }
        public void destroy() {
            this.encoding = null;
            this.filterConfig = null;
        }
    }
    2、在web.xml配置过滤器,即所有的请求和服务器输出都用UTF-8编码:
    <filter>
      <display-name>charencoding</display-name>
      <filter-name>CharacterEncodingFilter</filter-name>
      <filter-class>com.util.CharacterEncodingFilter</filter-class>
      <init-param>
       <param-name>encoding</param-name>
       <param-value>UTF-8</param-value>
      </init-param>
     </filter>
     <filter-mapping>
      <filter-name>CharacterEncodingFilter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>FORWARD</dispatcher>
      <dispatcher>INCLUDE</dispatcher>
      <dispatcher>REQUEST</dispatcher>
     
     </filter-mapping>
    3、在所有jsp页面都加入如下的代码:
     
      在page指令出加入:
        <%@ page contentType="text/html; charset=UTF-8"%>
       <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    4、在<head></head>之间加入如下代码:
    <meta http-equiv="Content-Type" content="text/html charset=UTF-8">
    5、在tomcat\conf\目录的server.xml中作如下设置,在<Connector项加入URIEncoding="UTF-8":
    如下所示:

    <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443"
                   URIEncoding="UTF-8"/
    6、最后,重启tomcat ,浏览网页一切正常。
    特别说明:
    如果用MySql数据库,MySql的编码也必须用UTF-8编码,否则中文不但在MySql会显示乱码,而且在JSP中也会显示乱码的。

    转载请注明出处:http://blog.163.com/sxzyh1688@126/blog/static/162300265201222034032696/
  • 相关阅读:
    mysql分组查询
    (三)分布式数据库tidb-隔离级别详解
    (二)分布式数据库tidb-事务
    (一)ArrayList集合源码解析
    (一)分布式数据库tidb-简介
    (二)LinkedList集合解析及手写集合
    电商数仓中需要统计的指标
    实时推荐模型的算法设计
    数据库需要掌握到什么程度可以应付工作?
    Mysql的万能优化方法
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215695.html
Copyright © 2011-2022 走看看