2013-02-03 70 views
1

當我得到以下輸出:JSP腳本無法顯示阿拉伯字符正確

System.out.print(name); 

我看不到正確的字符。

我該如何解決?

這裏是我的代碼:

insert.jsp

<%@page import="dao.DaoAdmin"%> 
<%@ page import="java.sql.*"%> 
<%@ page import="java.sql.PreparedStatement;"%> 
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
    <% request.setCharacterEncoding("UTF8"); %> 
<% 
    String name = request.getParameter("name"); 
    String city = request.getParameter("city"); 
    String phone = request.getParameter("phone"); 
    System.out.print(name); 
%> 


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
    <script type="text/javascript"> 

    alert("dsfg"); 
    </script> 
</body> 
</html> 

的index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Sita Ajax test</title> 
<script type="text/javascript" charset="UTF-8"> 
    function ajaxRequest() { 
     var activexmodes = [ "Msxml2.XMLHTTP", "Microsoft.XMLHTTP" ]; //activeX versions to check for in IE 
     if (window.ActiveXObject) { //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken) 
      for (var i = 0; i < activexmodes.length; i++) { 
       try { 
        return new ActiveXObject(activexmodes[i]); 
       } catch (e) { 
        //suppress error 
       } 
      } 
     } else if (window.XMLHttpRequest) // if Mozilla, Safari etc 
      return new XMLHttpRequest(); 
     else 
      return false; 
    } 
    // JavaScript Document 
    function ajaxget() { 
     var mygetrequest = new ajaxRequest(); 
     mygetrequest.onreadystatechange = function() { 
      if (mygetrequest.readyState == 4) { 

       document.getElementById("insert_response").innerHTML = mygetrequest.responseText; 

      } 
     }; 
     var name = document.getElementById("name").value; 
     var city = document.getElementById("city").value; 
     var phone = document.getElementById("phone").value; 

     mygetrequest.open("GET", "insert.jsp?name=" + name + "&city=" + city 
       + "&phone=" + phone + "", true); 
     mygetrequest.send(null); 

    } 
</script> 

</head> 
<body> 
    <h1>Welcome!</h1> 

    <form action="javascript:ajaxget()" ; method="post" 
     accept-charset="UTF-8"> 
     <table style="background-color: #ECE5B6;" width="30%"> 

      <tr> 
       <th width="50%">Name</th> 
       <td width="50%"><input type="text" name="name" id="name"> 
       </td> 
      </tr> 
      <tr> 
       <th width="50%">City</th> 
       <td width="50%"><input type="text" name="city" id="city"> 
       </td> 
      </tr> 
      <tr> 
       <th width="50%">Phone</th> 
       <td width="50%"><input type="text" name="phone" id="phone"> 
       </td> 
      </tr> 

      <tr> 
       <th></th> 
       <td width="50%"><input type="submit" value="submit"></td> 

      </tr> 
     </table> 
    </form> 
    <br /> 
    <br /> 
    <br /> 
    <br /> 

    <!-- Show Message for AJAX response --> 
    <div id="insert_response"></div> 

</body> 
</html> 

回答

2

我不知道這是否會幫助你或沒有,但我注意到你的代碼insert.jsp是你寫這個代碼:

<% 
    String name = request.getParameter("name"); 
    String city = request.getParameter("city"); 
    String phone = request.getParameter("phone"); 
    System.out.print(name); 
%> 

甚至在開始您的<html>代碼之前。此代碼應位於<body> ...</body>標記中的某處。

二,爲什麼你要寫多個<%@page ..>具有不同屬性的標籤。

<%@page import="dao.DaoAdmin"%> 
<%@ page import="java.sql.*"%> 
<%@ page import="java.sql.PreparedStatement;"%> 
<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<% request.setCharacterEncoding("UTF8"); %> 

而是寫這樣的:

<%@page import="dao.DaoAdmin;java.sql;java.sql.PreparedStatement" language="java" 
contentType="text/html; charset=UTF-8" /> 

所以嘗試了這一點。

1

嘗試在開始時加入response.setCharacterEncoding ("UTF-8");