【问题标题】:JSP JasperExceptionJSP 贾斯珀异常
【发布时间】:2015-03-10 01:30:16
【问题描述】:

所以我有一个 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 文件中获取内容。 该文件如下所示:

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();
    }

}

当我尝试运行该 selectform.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\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\Project\org\apache\jsp\selectform_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\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\Project\org\apache\jsp\selectform_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\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\Project\org\apache\jsp\selectform_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\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\Project\org\apache\jsp\selectform_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("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<title>Select</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body>\r\n");
      out.write("\t");

        String query = "SELECT * FROM arhiva ORDER BY id";
        try {
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery(query);

      out.write("\r\n");
      out.write("\r\n");
      out.write("\t<table border=1>\r\n");
      out.write("\t\t<tbody>\r\n");
      out.write("\t\t\t<tr>\r\n");
      out.write("\t\t\t\t<th>ID</th>\r\n");
      out.write("\t\t\t\t<th>Nume</th>\r\n");
      out.write("\t\t\t\t<th>Prenume</th>\r\n");
      out.write("\t\t\t</tr>\r\n");
      out.write("\t\t\t");

                while (rs.next()) {

      out.write("\r\n");
      out.write("\t\t\t<tr>\r\n");
      out.write("\t\t\t\t<td>");
      out.print(rs.getString("id"));
      out.write("</td>\r\n");
      out.write("\t\t\t\t<td>");
      out.print(rs.getString("nume"));
      out.write("</td>\r\n");
      out.write("\t\t\t\t<td>");
      out.print(rs.getString("prenume"));
      out.write("</td>\r\n");
      out.write("\t\t\t</tr>\r\n");
      out.write("\t\t\t");

                }
                } catch (Exception e) {
                    e.printStackTrace();
                }

      out.write("\r\n");
      out.write("\r\n");
      out.write("\t\t</tbody>\r\n");
      out.write("\t</table>\r\n");
      out.write("\r\n");
      out.write("</body>\r\n");
      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);
    }
  }
}

问题出在哪里?我不明白为什么我会出错。提前谢谢你!

【问题讨论】:

  • @Sharp edge 这是从select_form.jsp 生成的java 文件。 jsp 文件是 java servlet 的“较新”形式。
  • 我的 selectform.jsp 在里面 ...\Project\WebContent。
  • @AlexandruMitu 将jsp 文件转换为servlet,从而生成该jsp 页面的.java file..
  • 你必须检查从jsp生成的java文件,然后在其中找到没有[20]的行。
  • @atishshimpi 我发布了生成的文件。

标签: java mysql eclipse jsp servlets


【解决方案1】:

在你的 JSP 中扩展类 &lt;%@ page extends="proiect.Database"%&gt; 的问题。

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

不要在Database Servlet 中实现HttpJspPage

【讨论】:

  • HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET
  • 这个方法是在表单提交时出现的,检查你的&lt;form&gt;方法和servlet方法。参考stackoverflow.com/questions/23624442/…
  • 但是为什么你将你的 servlet 声明为 abstract 这让我很困惑。
【解决方案2】:

问题出在这里

public abstract class Database extends HttpServlet implements HttpJspPage

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    相关资源
    最近更新 更多