Java 如何让我的.JSP打印出我的数据库数据?

Java 如何让我的.JSP打印出我的数据库数据?,java,html,jsp,servlets,Java,Html,Jsp,Servlets,我正在打印一张学生姓名、身份证、年龄和课程的表格。我已经在WAMP服务器中创建了所有这些数据,我的想法是从数据库中获取数据,并使用.JSP文件将其打印出来 这是我的密码: 学生班级: package Servlets; import java.io.Serializable; /** * * @author Harry */ public final class Students implements Serializable{ public String Name;

我正在打印一张学生姓名、身份证、年龄和课程的表格。我已经在WAMP服务器中创建了所有这些数据,我的想法是从数据库中获取数据,并使用.JSP文件将其打印出来

这是我的密码:

学生班级:

package Servlets;

import java.io.Serializable;

/**
 *
 * @author Harry
 */
public final class Students implements Serializable{

     public String Name;
     public String gender;
     public int age;
     public int idnumber;



    public Students(String Name, String gender, int age, int idnumber) {


    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public int getID(){
        return idnumber;
    }
    public void setID(int idnumber){
        this.idnumber = idnumber;
    }
    public String getGender(){
        return gender;
    }
    public void setGender(String gender){
        this.gender = gender;
    }



}
java类,该类应从数据库中获取数据并将其转发到.JSP

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



@WebServlet(name = "getStudents", urlPatterns =
{
    "/getStudents"
})
public class getStudents extends HttpServlet
{

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        List<Students> students = new ArrayList<Students>();
        try
        {
            String dbURL = "jdbc:mysql://localhost:3306/students";
            String username = "root";
            String password = "Nitram8151";
            Connection connection = (Connection)   DriverManager.getConnection(
                    dbURL, username, password);

            Statement statement = (Statement) connection.createStatement();
            ResultSet rs = statement.executeQuery("SELECT * FROM students");

            while (rs.next())
            {
                String Name = rs.getString("Name");
                String gender = rs.getString("Gender");
                int idnumber = rs.getInt("ID Number");
                int age = rs.getInt("Age");

                Students s = new Students(Name, gender, idnumber, age);
                students.add(s);
            }
            connection.close();
        } catch (SQLException e)
        {

        }
        HttpSession session = request.getSession();
        session.setAttribute("index", students);

        RequestDispatcher dispatcher = request.getRequestDispatcher("listStudents.jsp");
        dispatcher.forward(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }


    @Override
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>

}
listStudents.jsp应获取从getStudents.java类转发的数据,并以表格格式打印数据:

<%-- 
    Document   : listStudents
    Created on : 07-Mar-2017, 12:23:12
    Author     : Martin Laptop
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Students</title>
    </head>
    <body>
        <h1>Student Information</h1>
        <table style="border: 4px solid #dddddd;padding: 8px;width: 25%;">
            <thead>
                <tr>
                    <td style="border: 2px solid #dddddd;padding: 4px;"><b><u>Student Name </u></b></td> 
                    <td style="border: 2px solid #dddddd;padding: 4px;"><b><u> Student Gender</u></b></td>
                    <td style="border:2px solid #dddddd;padding: 4px; "><b><u>Student ID Number</u></b></td>
                    <td style="border:2px solid #dddddd;padding: 4px; "><b><u>Student Age</u></b></td>

        </tr>
    </thead>
    <c:forEach var="student" items="${students}">
        <tr>
            <td>${students.Name}</td>
            <td>${students.gender}</td>
            <td>${students.idnumber}</td>
            <td>${students.age}</td>

        </tr>
        </c:forEach>
    </table>
  </body>
</html>

学生
学生信息
学名
学生性别
学生证号码
学生年龄
${students.Name}
${students.gender}
${students.idnumber}
${students.age}
有人能帮我解释一下为什么数据没有打印在jsp上吗?我对此束手无策有几个原因

首先,你在做什么

catch (SQLException e)
    {

    }
这意味着,如果try块中的代码抛出异常,您将无法了解任何有关该异常的信息。永远不要那样做

换成

catch (SQLException e) {
    throw new RuntimeException(e);
}
其次,您在会话中存储学生(尽管没有理由将他们存储在会话中:他们应该存储在请求中),使用

但是您的JSP使用

<c:forEach var="student" items="${students}">
应该是

${student.name}

您希望获取循环中当前学生的name属性,而不是列表的名称。

在方法processRequest中,您使用db数据设置名为“index”的会话属性,在jsp中,在foreach中,您试图通过名为“students”的bean的属性项恢复。同样在foreach中,您应该使用属性var中所述的“student”而不是“students”。
${students.Name}
${student.name}