java中文內部亂碼處理

來源:互聯網
上載者:User

在jsp中,中文亂碼常會讓人心亂如麻。一般的解決方案
一、本頁顯示問題
"<%@ page contentType="text/html;charset=GB2312"%>"
二、提交問題
鑒於此問題,你可以將讀出來的字串進行轉換,這很簡單:
str = request.getParameter("s");
str = new String(str.getBytes("ISO-8859-1"),"GBK");
或者設定
request.setCharacterEncoding("GBK"); 

然而,你遇到這種情況嗎,使用out.println是中文,Systemout.println是亂碼,看下面的例子

jsp1.jsp建立

<%@ page contentType="text/html;charset=GB2312"%>
<html>
<head>
<title>
mm
</title>
</head>
<body background="">
<form method="POST" action="servlet1">
<input type="text" name="myt" size="30" value ="&nbsp;">
<br>
<input type="text" name="myedit" size="30" value ="">
<input type="submit" value="保  存" name="B5" >
</form>
</body>
</html>

-----

servlet1 建立

package untitled2;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Servlet1 extends HttpServlet {

    public void doPost(HttpServletRequest request,
                       HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html;charset=GBK") ;
        request.setCharacterEncoding("GBK");
        PrintWriter out = response.getWriter() ;

       out.println(request.getParameter("myedit"));
       System.out.println(request.getParameter("myedit"));
 
    }

}

編譯後在ie運行,myedit中輸入漢字,就會出現我說亂碼,可別小看,對你有用System.out.println的輸出,本人經過反覆的調試,發現將

<input type="text" name="myt" size="30" value ="&nbsp;">中"&nbsp;"修改為"",就沒有問題了。"&nbsp;"我們經常用就是空,到底是什麼問題導致,我還不清楚。

我用的jbuild7.0,jdevelop9.03都有此問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.