gpt4 book ai didi

javascript - AJAX 不返回 JSP 形式的值

转载 作者:行者123 更新时间:2023-11-28 01:46:19 25 4
gpt4 key购买 nike

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

首页

<%@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)
);

最佳答案

请求对象中没有参数st

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

您传递的参数是q

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

关于javascript - AJAX 不返回 JSP 形式的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20265479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com