所以我有一个SQL查询,我想从数据库中选择数据,但我得到一个奇怪的例外,我对它知之甚少。请注意,我是JSPs的新手。
我有一个名为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>
请注意,我从项目包中的Database.java文件中获取了以下行&#34;&lt;%@ page extends =&#34; proiect.Database&#34;%&gt;&#34 ;。 该文件看起来像:
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时,我得到以下stacktrace:
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);
}
}
}
问题出在哪里?我不明白为什么我会收到错误。提前谢谢你,SO!
答案 0 :(得分:1)
问题出在这里
public abstract class Database extends HttpServlet implements HttpJspPage
您正在实现HttpJspPage接口。接口中的所有方法本质上都是公共抽象的,需要被实现类过度使用。因为来自HttpJspPage的两个方法init()和destroy()没有被过度使用,所以你得到一个例外。你可以摆脱我给这些方法实现的错误。
答案 1 :(得分:1)
在JSP中扩展类<%@ page extends="proiect.Database"%>
的问题。
公共抽象类数据库扩展了HttpServlet实现 HttpJspPage
不要在HttpJspPage
Servlet中实现Database
。