今天來介紹一下JSP亂碼之解決方式

 

這是我原本的程式碼:

輸入中文後會亂碼

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%   
 String zx01 = request.getParameter("zx01");      
%> 
    <html> 
      <body> 
       <form action = "n5.jsp" method = "post"> 
      <center> <font size="5">
     name : <input type = "text" name = "zx01" size = "10"> 
     <input type = "submit" name = "insert" value = "ok"> 
       </center>
      </form> 
      <div id="aa">
      <center></br><%= zx01%> </center>
       </div>
        </body> 
    </html> 

會有以下結果

n1.JPG

 

亂碼,是幾次都一樣,只要你打的是中文.....

 

所以要這樣改

1. 把UTF-8改big-5,改這2行

<%@ page contentType="text/html; charset=Big5" %>

 <head>
<meta http-equiv="Content-Type" content="text/html; charset=big5"></head>

2.字元編碼轉換,改成以下

String zx01 = new String(request.getParameter("zx01").getBytes("8859_1")); 

 

所以完整程式碼變成以下:

<%@ page contentType="text/html; charset=Big5" %>

<!DOCTYPE html>
<%   
     
 String zx01 = new String(request.getParameter("zx01").getBytes("8859_1")); 
%> 
<html> 
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=big5"></head>
 <body> 
 <form action = "n5.jsp" method = "post">  
   name : <input type = "text" name = "zx01" size = "10"> 
   <input type = "submit" name = "insert" value = "ok"> 
   </form> 
<div id="aa"><%= zx01%></div>
 </body> 
 </html> 

成功囉!!現在沒亂碼了

n2.JPG

arrow
arrow
    文章標籤
    JSP 中文亂碼 big5
    全站熱搜

    kyo 發表在 痞客邦 留言(0) 人氣()