如何修复HTTP状态500-内部服务器错误Servlet错误

时间:2018-07-24 17:19:54

标签: java eclipse jsp servlets

我正在尝试在jsp和servlet之间发送变量,但是出现了这个错误,但我仍然不知道为什么。它不断发送HTTP Status 500错误。

它发送HTTP状态500错误和java.lang.NullPointerException 但是当我在junit中测试Java类时,它运行良好。

这是jsp:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page import="model.StudentDao"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Student</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
    <h1>Student</h1>
        <table class="table">
            <tr>
                <th>Student ID</th>
                <th>Student Name</th>
            </tr>
            <c:forEach items="${requestScope.students}" var="student">
            <tr>
                <td><c:out value="${student.getStudentId()}"/></td>
                <td><c:out value="${student.getStudentName()}"/></td>
            </tr>
            </c:forEach>
        </table>
</body>
</html>

这是我的servlet:

package controller;

import...

@WebServlet("/StudentListServlet")
public class StudentListServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public StudentListServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DbConnect dbConnect = new DbConnect();
        Connection connection = dbConnect.connect();

        StudentDao studentDao = new StudentDao(connection);
        ArrayList<Student> students = null;

        try {
            students = studentDao.findAll();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        request.setAttribute("students", students);
        request.getRequestDispatcher("student.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

}

StudentDao:

package model;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class StudentDao {
    private Connection connection;

    public StudentDao(Connection connection) {
        super();
        this.connection = connection;
    }

    public ArrayList<Student> findAll() throws SQLException{
        ArrayList<Student> students = new ArrayList<>(); 
        Student student = null;

        Statement statement = this.connection.createStatement();
        String sqlText = "SELECT * FROM student";
        ResultSet resultSet = statement.executeQuery(sqlText);

        while(resultSet.next()){
            student = new Student();

            student.setStudentId(resultSet.getString("student_Id"));
            student.setStudentName(resultSet.getString("student_Name"));

            students.add(student);
        }

        resultSet.close();
        statement.close();

        return students;
    }
}

DbConnect:

    package model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConnect {
    private static final String URL = "jdbc:mysql://localhost:3306/university";
    private static final String USER = "root";
    private static final String PASSWORD = "";
    private Connection connection;

    public Connection connect() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            this.connection = DriverManager.getConnection(URL, USER, PASSWORD);
            if(!this.connection.isClosed())
                System.out.println("MySQL Connected");
            else
                System.out.println("MySQL Connect fail!");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return this.connection; 
    }

    public void close() throws SQLException {
        this.connection.close();
    }
}

堆栈跟踪:

    SEVERE: Servlet.service() for servlet [controller.StudentListServlet] in context with path [/MySQLDemo] threw exception
java.lang.NullPointerException
    at model.StudentDao.findAll(StudentDao.java:21)
    at controller.StudentListServlet.doGet(StudentListServlet.java:34)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

很抱歉,很长的帖子。 有人知道为什么会出错吗?

1 个答案:

答案 0 :(得分:1)

您没有在Servetlet中加载jdbc驱动程序

您可以按照以下方式使用jdbc驱动程序:

String driver = "com.mysql.jdbc.Driver";
 // Load the JDBC driver
     Class driver_class = Class.forName(driver);