1

当我得到以下输出时:

System.out.print(name);

我无法正确看到字符。

我该如何解决?

这是我的代码:

插入.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>

索引.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>
4

2 回答 2

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" />

所以试试这个。

于 2013-02-03T16:12:09.293 回答
1

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

于 2013-02-03T16:20:11.200 回答