我创建了一个程序,其中响应来自 ajax,它有两个值 testcode 和 testname,我的 jsp 页面中有三个文本字段,一个用于 testid,当我运行程序并在第一个文本字段中输入 testid 值并按 tab 然后ajax 响应即将出现,其中存在 testcode 和 testname。但问题是,在第二个和第三个文本字段中,两个值(testname,testcode)都聚集在一起,我想要一个特定的 testname 去特定的字段,而不是同一个字段中的两个值。
代码是:
index1.jsp(jsp文件)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" language="javascript" src="a.js">
</script>
</head>
<body>
<form method="post" action="hi">
test_id:<input type="text" name=testid onblur="mango1(this.value)">
test code:<input type="text" name="testCode" id="tc">
testName:<input type="text" name="testName" id="tn">
<br><input type="submit" value="submit">
</form>
<div id="k"> no test name!</div>
</body>
</html>
a.js(java脚本)
function mango1(testid)
{
alert("this is mango1 "+testid);
var xmlhttp;
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4 && xmlhttp.status==200)
{
/*document.getElementById("k").innerHTML=xmlhttp.responseText;*/
var item=xmlhttp.responseText;
document.getElementById("tc").value=xmlhttp.responseText;
document.getElementById("tn").value=xmlhttp.responseText;
}
};
/*xmlhttp.open("POST","hello?testid="+testid, true);*/
/*xmlhttp.open("POST","hello?testid=", true);*/
xmlhttp.open("POST","hi?testid="+testid, true);
xmlhttp.send();
}
hi.java(用于从数据库获取响应的 servlet)
package one;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DAO.TestDao;
/**
* Servlet implementation class hello
*/
@WebServlet("/hi")
public class hi extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public hi() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String testid=request.getParameter("testid");
System.out.println("testid is "+testid);
/*if(testid.equals("1"))
{
out.write("HIV");
}
else
{
out.write("Urinetest");
}*/
TestDao t1=new TestDao();
Vector vec=t1.getName(testid);
out.write((String) vec.get(0));
out.write((String) vec.get(1));
}
}
testDAo.java(用于数据库操作的 java 文件)
package DAO;
import java.sql.*;
import java.util.Vector;
//import beans.Test;
import DAO.DBCon;
public class TestDao {
public Vector getName(String testid)
{
Connection con=DBCon.getConnection();
System.out.println("got connection");
Vector temp=new Vector();
try
{
ResultSet rset=null;
PreparedStatement pst = con.prepareStatement("select testcode,testname from test where test_id=?");
System.out.println("got control here");
pst.setString(1,testid);
rset=pst.executeQuery();
while(rset.next()){
temp.add(rset.getString(1));
temp.add(rset.getString(2));
System.out.println("got control here in loop");
}
}
catch(Exception e)
{
e.printStackTrace();
}
return temp;
}
}