【问题标题】:Java, JSP, Eclipse - The server encountered an unexpected condition that prevented it from fulfilling the request [duplicate]Java,JSP,Eclipse - 服务器遇到了阻止它完成请求的意外情况[重复]
【发布时间】:2019-09-19 11:15:06
【问题描述】:

我正在使用 Eclipse 创建一个“动态 Web 项目”。我用MVC结构写了一些简单的代码。

我的LoaiBean类:

package bean;
public class LoaiBean {
    private String maLoai;
    private String tenLoai;
    public String getMaLoai() {
        return maLoai;
    }
    public void setMaLoai(String maLoai) {
        this.maLoai = maLoai;
    }
    public String getTenLoai() {
        return tenLoai;
    }
    public void setTenLoai(String tenLoai) {
        this.tenLoai = tenLoai;
    }
    public LoaiBean(String maLoai, String tenLoai) {
        super();
        this.maLoai = maLoai;
        this.tenLoai = tenLoai;
    }
}

我的LoaiDao课:

package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import bean.LoaiBean;
public class LoaiDao {
    private DBConnection dbConnection = null;
    public LoaiDao() {
        super();
        this.dbConnection = new DBConnection("bookdb");
    }
    public LoaiDao(DBConnection dbConnection) {
        super();
        this.dbConnection = dbConnection;
    }
    /**
     * get list of categories
     * @return {@link ArrayList}
     */
    public ArrayList<LoaiBean> getLoai(){
        Connection connection = this.dbConnection.getConnection(true);
        ArrayList<LoaiBean> loais = new ArrayList<LoaiBean>();
        try {
            Statement stmt = connection.createStatement();
            ResultSet rs =  stmt.executeQuery("select * from loai");
            while(rs.next()) {
                loais.add(new LoaiBean(
                    rs.getString("maloai"),
                    rs.getString("tenloai")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return loais;
    }
}

我的 DBConnection 类:

package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConnection {
    private String server = "localhost";
    private String port = "1433";
    private String databaseName = null;
    private String user = "sa";
    private String password = "123";
    private Connection connection = null;
    public String getServer() {
        return server;
    }
    public void setServer(String server) {
        this.server = server;
    }
    public String getPort() {
        return port;
    }
    public void setPort(String port) {
        this.port = port;
    }
    public String getDatabaseName() {
        return databaseName;
    }
    public void setDatabaseName(String databaseName) {
        this.databaseName = databaseName;
    }
    public String getUser() {
        return user;
    }
    public void setUser(String user) {
        this.user = user;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public Connection getConnection(boolean autoConnect) {
        if(autoConnect && this.connection == null) {
            this.connect();
        }
        return connection;
    }
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
    public DBConnection(String databaseName) {
        super();
        this.databaseName = databaseName;
    }
    public void connect() {
        if(this.databaseName == null) {
            return;
        }
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String st = "jdbc:sqlserver://"+this.server+":"+this.port+";databaseName="+this.databaseName+";user="+this.user+";password="+this.password;
            try {
                this.connection = DriverManager.getConnection(st);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public void reconnect() {
        this.disconnect();
        this.connect();
    }
    public void disconnect() {
        if(this.connection != null) {
            try {
                this.connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

我的LoaiBo课:

package bo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import bean.LoaiBean;
import dao.LoaiDao;
public class LoaiBo {
    private LoaiDao dao = null;
    public LoaiDao getDao() {
        return dao;
    }
    public void setDao(LoaiDao dao) {
        this.dao = dao;
    }
    public LoaiBo() {
        super();
        this.dao = new LoaiDao();
    }
    public LoaiBo(LoaiDao dao) {
        super();
        this.dao = dao;
    }
    public ArrayList<LoaiBean> getLoai(){
        return this.dao.getLoai();
    }
}

还有我的 JSP 文件 tc.jsp

<%@page import="bean.LoaiBean"%>
<%@page import="java.util.ArrayList"%>
<%@page import="bo.LoaiBo"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
    <%
    LoaiBo bo = new LoaiBo();
    ArrayList<LoaiBean> loais = bo.getLoai(); // error is showed here
    %>
    <div class="row justify-content-center">
        <div class="col-md-4 col-sm-6 col-xs-12">
            <ul class="list-group">
                <% for (LoaiBean loaiBean : loais) { %>
                <li class="list-group-item"><%= loaiBean.getMaLoai()  %></li>
                <% } %>
            </ul>       
        </div>
    </div>
</body>
</html>

当我运行上面的文件 tc.jsp 时,出现错误: enter image description here

HTTP Status 500 – Internal Server Error


Type Exception Report

Message An exception occurred processing [/tc.jsp] at line [17]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception
org.apache.jasper.JasperException: An exception occurred processing [/tc.jsp] at line [17]

14: <body>
15:     <%
16:     LoaiBo bo = new LoaiBo();
17:     ArrayList<LoaiBean> loais = bo.getLoai();
18:     %>
19:     <div class="row justify-content-center">
20:         <div class="col-md-4 col-sm-6 col-xs-12">


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:625)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:514)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Root Cause
java.lang.NullPointerException
    dao.LoaiDao.getLoai(LoaiDao.java:39)
    bo.LoaiBo.getLoai(LoaiBo.java:40)
    org.apache.jsp.tc_jsp._jspService(tc_jsp.java:140)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:476)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs.

当我通过带有main函数的java核心类进行测试时,这些函数运行正常,但不在jsp文件中运行。

我正在使用 eclipse、tomcat 9、java 8。

我不知道如何解决这个问题。我希望人们会帮助我。谢谢。

【问题讨论】:

  • 在LoaiBo.java里面加个debug我觉得this.dao.getLoai()有问题
  • 感谢您的回复,我尝试测试该功能,它运行正常。 i.imgur.com/LS6L4N6.png
  • 欢迎,如果您的问题得到解决,请批准
  • jsp文件还没有运行,我刚刚测试了函数运行正常。
  • 嗨。您能否删除您的错误图像并将其粘贴为代码?如果有人遇到与您相同的错误,他们将无法通过 Google 搜索该问题,因为 Google 不会读取图像文本 :)

标签: java eclipse jsp


【解决方案1】:

你可以尝试像下面这样从 LoaiDao.class 获取列表对象

<%
    LoaiDao loaiDao = new LoaiDao();
    ArrayList<LoaiBean> loais = loaiDao.getLoai(); 
%>

【讨论】:

  • 感谢您的回复。我试图从 LoaiDao 获取列表,但它仍然是同样的错误。 i.imgur.com/lGyrvij.png
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 2016-08-29
  • 1970-01-01
相关资源
最近更新 更多