将数据从MySQL加载到JSP页面,而无需表单



我正在尝试使用Eclipse和Tomcat 8服务器将数据从MySQL数据库加载到JSP页面中。我的.jsp文件如下所示:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.ArrayList"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<jsp:forward page="/ProductDisplay" />
<table border="2">
<tr>
<td>Id</td>
<td>First Name</td>
<td>Last Name</td>
<td>Email</td>
<td>Phone Number</td>
</tr>
<c:forEach var="patients" items="${patients}">
<tr>
<td>${patients.getPatientId()}</td>
<td>${patients.getPatientfName()}</td>
<td>${patients.getPatientlName()}</td>
<td>${patients.getPatientEmail()}</td>
<td>${patients.getPatientPhone()}</td>
</tr>
</c:forEach>
</table>
</body>
</html>

.java servlet 文件:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ProductDisplay
*/
@WebServlet("/ProductDisplay")
public class ProductDisplay extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ProductDisplay() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws 
ServletException, IOException {
PatientsDao p = new PatientsDao();
ArrayList<Patient> patients = p.getPatients();
request.setAttribute("patients", patients);
request.getRequestDispatcher("/Patient.jsp").forward(request,  response);
}
}

数据库连接器类如下所示:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class PatientsDao {
DBConnection mt = new DBConnection();
Connection myConn = mt.myConn;
private class DBConnection {
public Connection myConn;
public DBConnection() {
try {
Class.forName("com.mysql.jdbc.Driver"); 
myConn = 
DriverManager.getConnection("jdbc:mysql://localhost:3306/PatientsDB", "root", "");
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
public ArrayList<Patient> getPatients() {
ArrayList<Patient> patients = new ArrayList<>();
try {
PreparedStatement pst = 
myConn.prepareStatement("select * from Patients");
ResultSet r = pst.executeQuery();
while(r.next()) {
Patient p = new Patient();
p.setId(r.getInt("id"));
p.setfName(r.getString("fName"));
p.setlName(r.getString("lName"));
p.setEmail(r.getString("Email"));
p.setPhone(r.getString("Phone"));
patients.add(p);
}
}
catch (SQLException exc) {
System.out.println("An error occured. Error: " + exc.getMessage());
}
return patients;
}
}

如果我错了,请纠正我,但我相信在运行 Tomcat 服务器时,会调用 .java servlet 类,并且数据库中的信息无需任何操作即可加载到 JSP 页面中。但是,在我的情况下,这不会发生。一旦我启动Tomcat服务器并导航到Patient.jsp页面,唯一显示的是表头(不是来自数据库(。不显示来自 MySQL 数据库的数据。

我知道这不是MySQL问题,因为如果我添加表单并使用按钮,我可以成功加载所有数据。单击按钮并调用 doGet 方法后,所有内容都会成功加载。

我的问题是,如何在不单击按钮或链接的情况下自动加载数据?我正在做研究,显然所有信息都应该自动加载。然而,这在我的情况下不会发生。我错过了什么吗?提前谢谢你!

不,你错了。

当您调用它时,第一次加载 Servlet(对于检查,请转到Patient.jsp(。

但是当你转到 Patient 时.jsp只工作servlet Patient.jsp(jsp 页面自动转换为 servlet 类(,而你的 servlet/ProductDisplay根本不起作用。

当您调用患者时,您的请求属性为空.jsp因为产品显示不起作用。

您有三种方式:

1(您应该从/产品显示页面开始

2(或添加索引.jsp页面并从此页面开始。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<jsp:forward page="/ProductDisplay" />
</body>
</html>

3(和坏的变体 - 将脚本添加到页面患者.jsp代码并开始此页面

PatientsDao p = new PatientsDao();
ArrayList<Patient> patients = p.getPatients();
request.setAttribute("patients", patients);

记住永远不要直接调用servlet。只能通过任何 servlet。 这是不好的做法

最新更新