JSP JasperException



所以我有一个SQL查询,我想从数据库中选择数据,但我得到一个奇怪的异常,我对它知之甚少。请注意,我是 JSP 的新手。

我有一个名为 selectform 的文件.jsp我想从数据库中获取数据并将其显示在表中:

<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@ page extends="proiect.Database"%>
<html>
<head>
<title>Select</title>
</head>
<body>
    <%
        String query = "SELECT * FROM arhiva ORDER BY id";
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
    %>
    <table border=1>
        <tbody>
            <tr>
                <th>ID</th>
                <th>Nume</th>
                <th>Prenume</th>
            </tr>
            <%
                while (rs.next()) {
            %>
            <tr>
                <td><%=rs.getString("id")%></td>
                <td><%=rs.getString("nume")%></td>
                <td><%=rs.getString("prenume")%></td>
            </tr>
            <%
                }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            %>
        </tbody>
    </table>
</body>
</html>

请注意,我正在从 proiect 包中的 Database.java 文件中获取内容,其中包含以下行"<%@ page extends="proiect。数据库"%>"。该文件如下所示:

package proiect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.UnavailableException;
import javax.servlet.http.HttpServlet;
import javax.servlet.jsp.HttpJspPage;
public abstract class Database extends HttpServlet implements HttpJspPage {
    private static final long serialVersionUID = 1L;
    protected Connection con;
    protected String driver = "org.postgresql.Driver";
    protected String url = "jdbc:postgresql://localhost:5200/postgres";
    protected String user = "postgres";
    protected String pass = "test";
    Connection connection = null;
    ResultSet resultSet = null;
    PreparedStatement preparedStatement = null;
    String query = null;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url, user, pass);
        } catch (Exception e) {
            throw new UnavailableException(e.getMessage());
        }
        jspInit();
    }
}

当我尝试运行该选择表单.jsp时,我得到以下堆栈跟踪:

Jan 12, 2015 11:58:28 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Project] threw exception [Unable to compile class for JSP: 
An error occurred at line: [20] in the generated java file: [E:Eclipse.metadata.pluginsorg.eclipse.wst.server.coretmp0workCatalinalocalhostProjectorgapachejspselectform_jsp.java]
The type selectform_jsp must implement the inherited abstract method JspPage.jspInit()
An error occurred at line: [20] in the generated java file: [E:Eclipse.metadata.pluginsorg.eclipse.wst.server.coretmp0workCatalinalocalhostProjectorgapachejspselectform_jsp.java]
The type selectform_jsp must implement the inherited abstract method JspPage.jspDestroy()
Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP: 
An error occurred at line: [20] in the generated java file: [E:Eclipse.metadata.pluginsorg.eclipse.wst.server.coretmp0workCatalinalocalhostProjectorgapachejspselectform_jsp.java]
The type selectform_jsp must implement the inherited abstract method JspPage.jspInit()
An error occurred at line: [20] in the generated java file: [E:Eclipse.metadata.pluginsorg.eclipse.wst.server.coretmp0workCatalinalocalhostProjectorgapachejspselectform_jsp.java]
The type selectform_jsp must implement the inherited abstract method JspPage.jspDestroy()
Stacktrace:
    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    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)

selectform_jsp.java:

/*
 * Generated by the Jasper component of Apache Tomcat
 * Version: Apache Tomcat/7.0.56
 * Generated at: 2015-01-12 09:58:28 UTC
 * Note: The last modified time of this file was set to
 *       the last modified time of the source file after
 *       generation to assist with modification tracking.
 */
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.http.*;
import javax.servlet.*;
public final class selectform_jsp extends proiect.Database
    implements org.apache.jasper.runtime.JspSourceDependent {
  private static final javax.servlet.jsp.JspFactory _jspxFactory =
          javax.servlet.jsp.JspFactory.getDefaultFactory();
  private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
  private javax.el.ExpressionFactory _el_expressionfactory;
  private org.apache.tomcat.InstanceManager _jsp_instancemanager;
  public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
    return _jspx_dependants;
  }
  public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
    _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
  }
  public void _jspDestroy() {
  }
  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {
    final javax.servlet.jsp.PageContext pageContext;
    javax.servlet.http.HttpSession session = null;
    final javax.servlet.ServletContext application;
    final javax.servlet.ServletConfig config;
    javax.servlet.jsp.JspWriter out = null;
    final java.lang.Object page = this;
    javax.servlet.jsp.JspWriter _jspx_out = null;
    javax.servlet.jsp.PageContext _jspx_page_context = null;

    try {
      response.setContentType("text/html");
      pageContext = _jspxFactory.getPageContext(this, request, response,
                null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;
      out.write("rn");
      out.write("rn");
      out.write("rn");
      out.write("rn");
      out.write("rn");
      out.write("<html>rn");
      out.write("<head>rn");
      out.write("<title>Select</title>rn");
      out.write("</head>rn");
      out.write("<body>rn");
      out.write("t");
        String query = "SELECT * FROM arhiva ORDER BY id";
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);
      out.write("rn");
      out.write("rn");
      out.write("t<table border=1>rn");
      out.write("tt<tbody>rn");
      out.write("ttt<tr>rn");
      out.write("tttt<th>ID</th>rn");
      out.write("tttt<th>Nume</th>rn");
      out.write("tttt<th>Prenume</th>rn");
      out.write("ttt</tr>rn");
      out.write("ttt");
                while (rs.next()) {
      out.write("rn");
      out.write("ttt<tr>rn");
      out.write("tttt<td>");
      out.print(rs.getString("id"));
      out.write("</td>rn");
      out.write("tttt<td>");
      out.print(rs.getString("nume"));
      out.write("</td>rn");
      out.write("tttt<td>");
      out.print(rs.getString("prenume"));
      out.write("</td>rn");
      out.write("ttt</tr>rn");
      out.write("ttt");
                }
                } catch (Exception e) {
                    e.printStackTrace();
                }
      out.write("rn");
      out.write("rn");
      out.write("tt</tbody>rn");
      out.write("t</table>rn");
      out.write("rn");
      out.write("</body>rn");
      out.write("</html>");
    } catch (java.lang.Throwable t) {
      if (!(t instanceof javax.servlet.jsp.SkipPageException)){
        out = _jspx_out;
        if (out != null && out.getBufferSize() != 0)
          try {
            if (response.isCommitted()) {
              out.flush();
            } else {
              out.clearBuffer();
            }
          } catch (java.io.IOException e) {}
        if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
        else throw new ServletException(t);
      }
    } finally {
      _jspxFactory.releasePageContext(_jspx_page_context);
    }
  }
}

问题出在哪里?我不明白为什么我会收到错误。提前谢谢你,所以!

问题就在这里

public abstract class Database extends HttpServlet implements HttpJspPage

您正在实现 HttpJspPage 接口。接口中的所有方法本质上都是公共抽象的,需要由实现类覆盖。 由于来自 HttpJspPage 的两个方法 init() 和 destroy() 没有被覆盖,因此您会出现异常。你可以摆脱我给这些方法一个实现的错误。

JSP 中扩展类<%@ page extends="proiect.Database"%>的问题。

公共抽象类数据库扩展 HttpServlet 实现 HttpJspPage

不要在 Database Servlet 中实现HttpJspPage

最新更新