[Jweb] Tomcat 解决编码, 乱码问题

Know其他 : tomcat 解决乱码问题
  b.jsp 源码
<%@ page contentType="text/html;charset=GBK"%>  <!-- 解决 get 方式乱码问题,这句话 -->
<%request.setCharacterEncoding("GBK");%>  <!-- 解决 post 方式乱码问题,这句话 -->
<!-- 以上两句话加上,非常好,可以兼顾不出乱码 -->
<%=request.getParameter("user") %>
<%String s = "哈哈哈";%>
<%=s%>

具体核心 :见 csdn, robby_chan 本博。

a1.html

<form action=b.jsp method=get >
	<input name=user value=我们>
	<input type=submit value=get>
</form>

<form action=b.jsp method=post >
	<input name=user value=他们>
	<input type=submit value=post>
</form>

<a href="/my/charset//b.jsp?user=你们">this is a link</a>
TestB.java  张志宇带你详细讲解 编码,乱码问题。
public class TestB {
    static String zhongwen = "中文";
    static byte[] bytes = null;

    public static void main(String[] args) throws Exception {
        // GBK
        // main1();
        // utf-8
        // main2();
        // GBK-->utf-8-->GBK
        // main3();
        // GBK-->iso8859-1-->utf-8-->iso8859-1-->GBK
        main4();

    }

    // GBK
    public static void main1() throws Exception {
        bytes = zhongwen.getBytes("GBK");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }
        System.out.println(new String(bytes, "GBK"));
    }

    // utf-8
    public static void main2() throws Exception {
        bytes = zhongwen.getBytes("utf-8");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }
        System.out.println(new String(bytes, "utf-8"));
    }

    // GBK-->utf-8-->GBK
    public static void main3() throws Exception {

        bytes = zhongwen.getBytes("GBK");
        System.out.println("--------GBK----------");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }

        bytes = zhongwen.getBytes("utf-8");
        byte[] bytes_copy = bytes;
        System.out.println("-------utf-8--------");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }

        // 错误的组装方法
        System.out.println("-------error GBK--------");
        zhongwen = new String(bytes, "GBK");
        System.out.println(zhongwen);
        bytes = zhongwen.getBytes("GBK");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }
        // 正确的组装方法
        System.out.println("-------GBK--------");
        System.out.println(new String(bytes_copy, "utf-8"));
    }

    // GBK-->iso8859-1-->utf-8-->iso8859-1-->GBK
    public static void main4() throws Exception {

        // 最开始是GBK
        bytes = zhongwen.getBytes("GBK");
        System.out.println("--------GBK----------");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }

        // 被组装成了iso8859-1,new出的字符串对象有问题,但是字节内容没变
        System.out.println("-------iso8859-1--------");
        zhongwen = new String(zhongwen.getBytes("GBK"), "iso8859-1");
        System.out.println(zhongwen);
        bytes = zhongwen.getBytes("iso8859-1");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }
        // 转换成utf-8在网络上传输,一个byte转换为两个字节,所以一共8个字节
        bytes = zhongwen.getBytes("utf-8");
        System.out.println("-------utf-8--------");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }

        // server端接收到utf-8,首先组装成iso8859-1,new出的字符串对象有问题,显示4个问号
        System.out.println("-------server--iso8859-1------");
        zhongwen = new String(bytes, "utf-8");
        System.out.println(zhongwen);
        bytes = zhongwen.getBytes("iso8859-1");
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(Integer.toHexString(bytes[i]));
        }

        // 以iso8859-1得到字节,并组装成GBK
        System.out.println("-------GBK--------");
        System.out.println(new String(bytes, "GBK"));
    }

    public static void ____________________main() throws Exception {
        System.out.println(System.getProperty("file.encoding"));
        System.out.println(System.getProperty("user.language"));
        System.out.println(System.getProperty("user.region"));
    }

}
SayHelloBean.html
<html>
<head>
<title>数据输入</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<div align="center">
  <p>请输入数据</p>
  <form method="post" action="SayHelloBean.jsp" >
    <p>姓名
      <input type="text" name="name">   
      性别
      <select name="sex">
        <option value="先生">先生</option>
        <option value="女士">女士</option>
      </select>
    </p>
    <p>
      <input type="submit" name="Submit" value="提交">
    </p>
  </form>
  <p> </p>
  <p> </p>
</div>
</body>
</html>
SayHelloBean.jsp
<%@ page language="java" import="bean.HelloBean;" %>
<%@ page contentType="text/html;charset=gb2312" %>
<%
//request.getParameter("name");
//request.setCharacterEncoding("gb2312");
%>
<jsp:useBean id="hello" class="bean.HelloBean" scope="request" >
    <jsp:setProperty name="hello" property="*" />
</jsp:useBean>
<html>
<head>
<title>HelloBean</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body bgcolor="#FFFFFF">
<p> </p>
<p align="center">
<font size="4">欢迎
<font color="#0000FF"><b>
    
<%--    
<%=new String(hello.getName().getBytes("ISO8859_1"),"GBK")%>
--%>
</b></font>
<%--
<%=new String(hello.getSex().getBytes("ISO8859_1"),"GBK")%>
--%>
</font></p>

<jsp:getProperty name="hello" property="name"/>
<jsp:getProperty name="hello" property="sex"/>

</body>
</html>



posted @ 2014-01-02 22:46  小尼人00  阅读(135)  评论(0编辑  收藏  举报