使用Get方法和servlet。您可以在servlet中编写所有Java代码,如下所示:
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
String driverName = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost/";
String dbName = "db";
String userId = "root";
String password = "root";
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try{
connection = DriverManager.getConnection(connectionUrl+dbName, userId, password);
statement=connection.createStatement();
String sql ="SELECT * FROM table";
resultSet = statement.executeQuery(sql);
// Now convert this result set into a class having field id and data..
// like MyClass{ String id; String data;}
// make a list of Listlist = new ArrayList<>();
request.setAttribute("list",list);
this.getServletContext().getRequestDispatcher("/jsp/yourPageName.jsp").
include(request, response);
}
现在,使用在您的jsp文件中获取名为“ list”的此属性request.getAttribute("list");。将其输入列表。并对其进行迭代并进行相应的打印。