0

我有具有链接的 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;
    }

}

}
4

0 回答 0