天天看點

java web 中文亂碼問題,上傳元件傳參亂碼解決

1 response.setHeader("Content-type", "text/html;charset=UTF-8");//設定浏

覽器以什麼編碼顯示資料,注意分号

 response.setCharacterEncoding("UTF-8");

Java中文使用Unicode編碼。在Cookie中使用Unicode字元時需要對Unicode字元進行編

碼,否則會亂碼。

編碼使用java.net.URLEnoder類的encode(String str,String encoding)方法。

解碼使用java.net.URLDecoder類的decode(String str,String encoding)方法。

看如下代碼:

List<FileItem> list =upload.parseRequest(request);

Iterator<FileItem> it = list.iterator();

String username=null;

String password=null;

while (it.hasNext()) {

 FileItem item = (FileItem) it.next();// 每一個item就代表一個表單輸入項

       /*isFormField方法用于判斷FileItem類對象封裝的資料是否屬于一個普通表單字段,還是屬于一個檔案表單字段,

 如果是普通表單字段則傳回true,否則傳回false。

 */

 if (item.isFormField()) {

  String name = item.getFieldName();// input的名稱

  String value = item.getString();// input的值

  if(item.getFieldName().equals("username")){

  username=new String(item.getString().getBytes("iso-8859-1"),"utf-8"); //注意這裡的username不能直接等于item.getString()會亂碼的 因為檔案上傳元件的表單有個enctype="multipart/form-data"

 }

 if(item.getFieldName().equals("password")){

 password=new String(item.getString().getBytes("iso-8859-1"),"utf-8");//這裡和前面username一樣

 } 

4 URL傳中文參數 和 表單傳中文參數

後者,如果是POST方法 則 隻需在服務端寫上request.setCharactorEncodeing("utf-8");即可(看JAVAWEB課件第三章是這麼寫的,不知道是不是僅僅如此就可以了)

前者,需要對URL進行兩次編碼 如果是js則 encodeURI(encodeURI())即可 如果是伺服器端  java.net.URLEncoder.encode()方法吧

URL傳中文參數 在 xmlHttpRequest.open()中,如果是GET方法,那麼需要編輯,如果是POST好像不用編碼吧,但我看課件有這麼說的。

/*發送包含使用者輸入資訊的請求體

encodeURIComponent() 函數可把字元串作為URI 元件進行編碼。保證各種特殊字

符能被正确發送。

*/

xmlHttp.send("name=" + encodeURIComponent(name)+ "&value=" +

encodeURIComponent(value));

可能POST還需要編碼,唉!

1  DBConnection.class.getResourceAsStream("Conn.properties")

位址怎麼表示的呢? 是目前目錄 還是什麼的,為什麼蘭冬跑不出來。

2  牢記 存入圖檔需要開戶事務,并需要送出 才可以。先牢記吧。

3   圖檔位址指向一個servlet,servlet轉向圖檔,則原頁面可以顯示圖檔。

4 <script type="text/javascript">

 var xmlHttp; //聲明XMLHttpRequest對象

 /*建立XMLHttpRequest對象*/

 function createXMLHttpRequest() {

  if (window.ActiveXObject) { //IE浏覽器

   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

  } else if (window.XMLHttpRequest) { //非IE浏覽器 

   xmlHttp = new XMLHttpRequest();

  }

  if (!(xmlHttp)) { //異常,建立對象失敗

   window.alert("建立XMLHttpRequest異常!");

 /*輸出驗證結果*/

 function validateResult() { 

  //alert(xmlHttp.readyState); //輸出狀态碼

  if (xmlHttp.readyState == 4) {

    //alert(xmlHttp.status); //輸出響應代碼

    if (xmlHttp.status == 200) {

    if (xmlHttp.responseText == "error") {

     alert("使用者名已經存在!");

    }

 /*執行Ajax驗證*/

 function userNameValidate() { 

  createXMLHttpRequest(); //建立XMLHttpRequest對象

  var username = document.getElementById("username").value;

  xmlHttp.open("post", "RegisterServlet.servlet", true);//初始化XMLHttpRequest對象

  xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//采用POST送出方式時要加上這句

  xmlHttp.onreadystatechange = validateResult; //設定回調函數

  xmlHttp.send("username="+username); //發送請求

 注意ajax用post和get方式不一樣 在于第三個函數裡的 setRequestHeader和 傳參的方式不一樣send()裡面寫參數

   Iterator<FileItem> it = list.iterator();

            String username=null;

            String password=null;

   while (it.hasNext()) {

    FileItem item = (FileItem) it.next();// 每一個item就代表一個表單輸入項

                /*isFormField方法用于判斷FileItem類對象封裝的資料是否屬于一個普通表單字段,還是屬于一個檔案表單字段,

    如果是普通表單字段則傳回true,否則傳回false。

    */

    if (item.isFormField()) {

     String name = item.getFieldName();// input的名稱

     String value = item.getString();// input的值

     if(item.getFieldName().equals("username")){

     username=new String(item.getString().getBytes("iso-8859-1"),"utf-8"); //注意這裡的username不能直接等于item.getString()會亂碼的 因為檔案上傳元件的表單有個enctype="multipart/form-data"

     }

     if(item.getFieldName().equals("password")){

      password=new String(item.getString().getBytes("iso-8859-1"),"utf-8");//這裡和前面username一樣

6

response.setContentType("image/jpeg"); 圖檔輸出時設定

document.getElementById("").style.display="block";可以但不能寫成document.getElementById("").style="display:block"因為style是個樣式對象,但是在火狐裡就可以 悲催了。。

AJAX

1

/*執行Ajax驗證*/

注意POST方法送出時,需要加上xmlHttp.setRequestHeader();方法

轉自 csdn http://blog.csdn.net/frona_lee/article/details/7939520