我有JSP页面有链接(当人们点击链接它应该去一个servlet然后Bean类和DB Connection类来拉取数据)..当用户点击我试过的时候使用AJAX显示页面...但是我目前没有获取任何数据/页面。有人可以解释如何实现这个目标吗?
这是我在HTML页面中的链接..但是当我点击它时没有显示任何内容。
<a href="myservlet" onclick="grabfile(this.href); return false;">VT </a>
感谢您的帮助
对不起,这是我的代码
public class VacationTrackerDAO {
public static List<VactionTrackerBean> list() throws SQLException{
List<VactionTrackerBean> appr= new ArrayList<VactionTrackerBean>();
try{
DBConnection conObj=new DBConnection();
Connection dbCon= conObj.getCon();
Statement stmt=conObj.getStmt();
String queryCPList="select * from Capacity_Plan";
String queryApprList="Select First_Last_Name from Vacation_Approvers";
PreparedStatement preStmtCPList=dbCon.prepareStatement(queryCPList);//get metadat
PreparedStatement preStmtApprList=dbCon.prepareStatement(queryApprList);//get names
ResultSet rsCPList=preStmtCPList.executeQuery();
ResultSet rsApprList=preStmtCPList.executeQuery();
ResultSetMetaData metaCPList=rsCPList.getMetaData();
VactionTrackerBean vtBean=new VactionTrackerBean();
while(rsApprList.next()){
vtBean.setApprover((rsApprList.getString("First_Last_Name")));
appr.add(vtBean);
}
}catch(Exception e){
System.out.println("In the Vacation TrackerDAO.java class:"+e);
}
return appr;
}
}
这是我的Servlet获取请求。
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
List<VactionTrackerBean> vacBean=VacationTrackerDAO.list();
request.setAttribute("vacTracker", vacBean);
request.getRequestDispatcher("WEB-INF/VacationTracker.jsp").forward(request,response);
}catch (SQLException e){
System.out.println("Error in VacationTracker.java due to VactionTrackerDA.java:"+e);
request.getRequestDispatcher("WEB-INF/Error.jsp").forward(request,response);
}
这是我在JSP页面中的代码。
<li> <a href="Main.jsp" > home </a> </li>
<li><a href="VacationTracker" onclick="grabfile(this.href); return false;">Vacation Tracker </a> </li>
</ul>
以下是我的Javascript代码
function getHTTPObject(){
var xhr=false;
if(window.XMLHttpRequest){
xhr= new XMLHttpRequest();
} else if(window.ActiveXObject){
try{
xhr= new ActiveXObject("Msxml2.XMLHTTP");
} catch(e){
try{
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
xhr=false;
}
}
}
return xhr;
}
function grabfile(file){
var request=getHTTPObject();
if(request){
request.onreadystatechange=function(){ displayResponse(request);};
}
request.open("POST",file,true);
request.send(null);
}
function displayResponse(request){
if(request.readyState==4){
if(request.status==200){
document.getElementById ("middle_sub").innerHTML=request.responseText;
}
}
}