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

时间:2017-03-12 13:17:11

标签: java html jsp servlets

我正在尝试打印出一张学生姓名,身份证,年龄和课程表。我已经在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;
    }



}

getStudents.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>

任何人都可以帮我解释为什么数据不会在jsp上打印出来?我的智慧结束了这个

1 个答案:

答案 0 :(得分:2)

有几个原因。

首先,你正在做

catch (SQLException e)
    {

    }

这意味着如果try块中的代码抛出异常,你就无法了解它。不要那样做。

将其替换为

catch (SQLException e) {
    throw new RuntimeException(e);
}

其次,你在会话中存储学生(虽然没有理由将它们存储在会话中:它们应该存储在请求中),使用

session.setAttribute("index", students);

但是您的JSP使用

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

因此,您将它们存储在名为“index”的属性中,并在属性名称“students”中检索它们。

最后,

${students.Name}

应该是

${student.name}

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