0
我在做一個自動完成。它很好地工作。但我想把第二個jsp文件當作.java文件。我已經盡力了。我不能改變它,請幫助我。主要問題是request.getparameter();想要將.jsp文件更改爲.java
JSP Ajax的自動完成框
autocomplete.jsp:
<%@page import="java.sql.*"%>
<html>
<head>
<script type="text/javascript">
function showData(value){ ``
xmlHttp=GetXmlHttpObject()
var url="auto.jsp";
url=url+"?name="+value;
xmlHttp.onreadystatechange=stateChanged
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}
function stateChanged() {
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
var showdata = xmlHttp.responseText;
document.getElementById("mydiv").innerHTML= showdata;
}
}
function GetXmlHttpObject(){
var xmlHttp=null;
try {
xmlHttp=new XMLHttpRequest();
}
catch (e) {
try {
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
</head>
<body>
<form name="employee">
<input type="text" name="name" id="name" onkeyup="showData(this.value);"><br>
<div id="mydiv"></div>
</table>
</body>
</html>
auto.jsp (second jsp file)
<%@ page import="java.sql.*" %>
<%
String name = request.getParameter("name").toString();
String buffer="<div>";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from data where name like '"+name+"%'");
while(rs.next())
{
buffer=buffer+rs.getString("name")+"<br>";
}
buffer=buffer+"</div>";
response.getWriter().println(buffer);
}
catch (Exception e) {
System.out.println(e);
}
%>