MVC, Servlets, JSP, No Output

2019-09-09 12:19发布

问题:

I'm trying to learn Servlets/JSP. When I tried the following code I dont get any output. Can someone please help me? When I run the code I get only the header and I dont see the values showing up in the dropdown.

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

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;

}
}

Bean:

 public class VactionTrackerBean {

private String dates;
private String approver;
public String getDate() {
    return dates;
}
public void setDate(String dates) {
    this.dates = dates;
}
public String getApprover() {
    return approver;
}
public void setApprover(String approver) {
    this.approver = approver;
}



}

Servlet:

public class VacationTracker extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

    doPost(request, response);

}


protected void doPost(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException {

    try{


    List<VactionTrackerBean> vacTracker=VacationTrackerDAO.list();
    request.setAttribute("vacTracker", vacTracker);
    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 Page:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!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>Vacation Tracker</title>  
</head>
<body>

<h1> Vacation Tracker </h1>

 <div id="main">

<c:forEach items="${vacTracker}" var="vacTracker"> 
<select id="myselect" name="mydata" size="10"> 
<option value="${vacTracker.approver}">${vacTracker.approver}</option>
</select>   
</c:forEach>

</div>


</body>
</html>

回答1:

Check you are getting values in list.Put some S.O.P after while loop in DAO class.

Also,There is a mistake in code. It should be

<select id="myselect" name="mydata" size="10"> 
<c:forEach items="${vacTracker}" var="vacTracker"> 
  <option value="${vacTracker.approver}">${vacTracker.approver}</option>
</c:forEach>
</select>   

Otherwise, It will create new select for each item.