【问题标题】:How can I get my .JSP to print out my database data?如何让我的 .JSP 打印出我的数据库数据?
【发布时间】:2017-08-02 12:56:56
【问题描述】:

我正在尝试打印一个包含学生姓名、身份证、年龄和课程的表格。我已经在 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 上打印出来?我对此束手无策

【问题讨论】:

  • 在您的方法 processRequest 中,您正在使用您的数据库数据设置一个名为“index”的会话属性,并且在您的 jsp 中,在您的 foreach 中,您试图通过属性项恢复一个 bean名为“学生”。同样在 foreach 中,您应该使用属性 var 中所述的“student”而不是“students”。

标签: java html jsp servlets


【解决方案1】:

几个原因。

首先,你正在做

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}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 2021-08-03
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    相关资源
    最近更新 更多