AJAX没有从JSP返回值

时间:2013-11-28 11:51:56

标签: javascript ajax jsp netbeans jdbc

我正在尝试使用ajax从jsp中检索值,如此处所述here。但它没有从DB返回值。请帮助解决这个问题。

主页

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <title>Jsp Page</title>
        <script>
            function showuser(str)
            {
                var xreq;
                if (str == "")
                {
                    document.getElementById("showtext").innerHTML = "";
                    return;
                }
                if (window.XMLHttpRequest)
                {
                    xreq = new XMLHttpRequest();
                }
                else
                {
                    xreq = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xreq.onreadystatechange = function()
                {
                    if ((xreq.readyState == 4) && (xreq.status == 200))
                    {
                        document.getElementById("showtext").innerHTML
                                = xreq.responseText;

                    }
                }
                xreq.open("get", "getuser.jsp?q=" + str, "true");
                xreq.send();

            }
        </script>
    </head>
    <body>
        <form>
            <select name="user" onchange="showuser(this.value)" >
                <option value="">Select Student name....</option>
                <option value="abhi">abhi</option>
                <option value="alex">alex</option>
                <option value="adam">adam</option>
            </select>
        </form>
        <br/>
        <div id="showtext">The response will come here</div>
    </body>
</html>

数据库连接建立页面

<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.*,java.sql.*,java.io.*" %>
<%@page import="javax.servlet.*" %>
<%@page import="javax.servlet.http.*" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>jsp Page</title>
    </head>
    <body>
        <%! Connection con;%>
        <%! Statement s;%>
        <%! ResultSet rs;%>

        <% String name = request.getParameter("st");

            try {

                Class.forName("oracle.jdbc.driver.OracleDriver");
                con = DriverManager.getConnection("URL");
                s = con.createStatement();
                rs = s.executeQuery("select * from studentinfo where name='" + name + "'");
            } catch (Exception e) {
                e.printStackTrace();
            }
        %>

        <div id="dtl_table"><table border='3' cellpadding='5'
                                   cellspacing='2' width="400px">
                <tr bgcolor="66FF00">
                    <th>Name</th>
                    <th>Branch</th>
                    <th>Year</th>
                    <th>Email id</th>
                </tr>
                <tr>
                    <% while (rs.next()) {
                    %>
                    <td><%= rs.getString("NAME")%></td>
                    <td><%= rs.getString("BRANCH")%></td>
                    <td><%= rs.getString("YEAR")%></td>
                    <td><%= rs.getString("EMAIL")%></td>
                    <% }%>
                </tr>
            </table></div>
    </body>
</html>

表格结构

CREATE TABLE studentinfo(
   name VARCHAR2(30),
   branch VARCHAR2(20),
   year VARCHAR2(20),
   email VARCHAR2(80) 
);

1 个答案:

答案 0 :(得分:1)

请求对象

中没有参数st
String name=request.getParameter("st");

您传递的参数是q

xreq.open("get","getuser.jsp?q="+str,"true");