如何直接调用 servlet 以在 JSP 站点上显示输出?
How to invoke the servlet direclty to show the output on the JSP site?
我的问题是如何让它工作,以便我 运行 Eclipse JEE 中的 servlet 显示一个 jsp 站点,其中包含一个数据 table 作为结果?通常,整个页面都是空的,或者我只看到 table 列名称。我同时连接到我的数据库,这应该不是问题。
这是 servlet。我想问题一定出在这里。
@WebServlet("/StudentListService")
public class StudentListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ArrayList<Student> studentList = new ArrayList<Student>();
try {
StudentDAO studentDAO = new StudentDAO();
studentList = studentDAO.getAllStudents();
request.setAttribute("studentList", studentList);
request.getRequestDispatcher("StudentList.jsp").forward(request, response);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
这是相关的DAO方法。这个DAO我在之前的练习中用过class,应该可以的
public ArrayList<Student> getAllStudents() throws SQLException {
ArrayList<Student> studentList = new ArrayList<Student>();
Connection dbConnection = null;
try {
dbConnection = openConnection();
String sqlText = "SELECT id, firstname, lastname, streetaddress, postcode, postoffice FROM Student";
PreparedStatement preparedStatement = dbConnection.prepareStatement(sqlText);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String firstName = resultSet.getString("firstname");
String lastName = resultSet.getString("lastname");
String street = resultSet.getString("streetaddress");
String postCode = resultSet.getString("postcode");
String postOffice = resultSet.getString("postoffice");
studentList.add(new Student(id, firstName, lastName, street, postCode, postOffice));
}
return studentList;
} catch (SQLException sqle) {
throw sqle; // Let the caller decide what to do with the exception
} finally {
closeConnection(dbConnection);
}
}
这是jsp。我不确定它是否正确,这只是其中的 jsp 部分,它应该显示在 html table.
中
<c:forEach items="${ studentList }" var="studentObject">
<tr>
<td><c:out value="${ studentObject.id}" /></td>
<td><c:out value="${ studentObject.lastName }" /></td>
<td><c:out value="${ studentObject.firstName }" /></td>
<td><c:out value="${ studentObject.street }" /></td>
<td><c:out value="${ studentObject.postCode}" /></td>
<td><c:out value="${ studentObject.postOffice }" /></td>
</tr>
</c:forEach>
要直接访问 servlet,请像这样从浏览器向您的 servlet 发出获取请求:localhost:8080/your-porject-name/StudentListService
我的问题是如何让它工作,以便我 运行 Eclipse JEE 中的 servlet 显示一个 jsp 站点,其中包含一个数据 table 作为结果?通常,整个页面都是空的,或者我只看到 table 列名称。我同时连接到我的数据库,这应该不是问题。
这是 servlet。我想问题一定出在这里。
@WebServlet("/StudentListService")
public class StudentListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ArrayList<Student> studentList = new ArrayList<Student>();
try {
StudentDAO studentDAO = new StudentDAO();
studentList = studentDAO.getAllStudents();
request.setAttribute("studentList", studentList);
request.getRequestDispatcher("StudentList.jsp").forward(request, response);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
这是相关的DAO方法。这个DAO我在之前的练习中用过class,应该可以的
public ArrayList<Student> getAllStudents() throws SQLException {
ArrayList<Student> studentList = new ArrayList<Student>();
Connection dbConnection = null;
try {
dbConnection = openConnection();
String sqlText = "SELECT id, firstname, lastname, streetaddress, postcode, postoffice FROM Student";
PreparedStatement preparedStatement = dbConnection.prepareStatement(sqlText);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String firstName = resultSet.getString("firstname");
String lastName = resultSet.getString("lastname");
String street = resultSet.getString("streetaddress");
String postCode = resultSet.getString("postcode");
String postOffice = resultSet.getString("postoffice");
studentList.add(new Student(id, firstName, lastName, street, postCode, postOffice));
}
return studentList;
} catch (SQLException sqle) {
throw sqle; // Let the caller decide what to do with the exception
} finally {
closeConnection(dbConnection);
}
}
这是jsp。我不确定它是否正确,这只是其中的 jsp 部分,它应该显示在 html table.
中<c:forEach items="${ studentList }" var="studentObject">
<tr>
<td><c:out value="${ studentObject.id}" /></td>
<td><c:out value="${ studentObject.lastName }" /></td>
<td><c:out value="${ studentObject.firstName }" /></td>
<td><c:out value="${ studentObject.street }" /></td>
<td><c:out value="${ studentObject.postCode}" /></td>
<td><c:out value="${ studentObject.postOffice }" /></td>
</tr>
</c:forEach>
要直接访问 servlet,请像这样从浏览器向您的 servlet 发出获取请求:localhost:8080/your-porject-name/StudentListService