JSP脚本无法正确显示阿拉伯字符

时间:2013-02-03 16:08:01

标签: jsp character-encoding jsp-tags

当我得到以下输出时:

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 个答案:

答案 0 :(得分: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 :(得分:1)

尝试在开头添加response.setCharacterEncoding ("UTF-8");