1

我很难完成这项工作。我可以毫无问题地连接到数据库,但是我无法让它显示 html 页面。它不运行。

import java.io.*;
import java.sql.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ShowBedrock extends HttpServlet 
{
    public String getServletInfo()
    {
       return "Servlet connects to PostgreSQL database and displays result of a SELECT";
    }

    private Connection dbcon;  // Connection for scope of ShowBedrock

    // "init" sets up a database connection
    public void init(ServletConfig config) throws ServletException
    {
        String loginUser = "postgres";
        String loginPasswd = "supersecret";
        String loginUrl = "jdbc:postgresql://localhost/bedrock";

        // Load the PostgreSQL driver
        try 
        {
              Class.forName("org.postgresql.Driver");
              dbcon = DriverManager.getConnection(loginUrl, loginUser, loginPasswd);
        }
        catch (ClassNotFoundException ex)
        {
               System.err.println("ClassNotFoundException: " + ex.getMessage());
               throw new ServletException("Class not found Error");
        }
        catch (SQLException ex)
        {
               System.err.println("SQLException: " + ex.getMessage());
        }
    }

    // Use http GET

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");    // Response mime type

        // Output stream to STDOUT
        PrintWriter out = response.getWriter();

        out.println("<HTML><Head><Title>Bedrock</Title></Head>");
        out.println("<Body><H1>Bedrock</H1>");

        try
        {
                // Declare our statement
                Statement statement = dbcon.createStatement();

                String query = "SELECT name, dept, ";
                query +=       "       jobtitle ";
                query +=       "FROM   employee ";

                // Perform the query
                ResultSet rs = statement.executeQuery(query);

                out.println("<table border>");

                // Iterate through each row of rs
                while (rs.next())
                {
                   String m_name = rs.getString("name");
                   String m_dept = rs.getString("dept");
                   String m_jobtitle = rs.getString("jobtitle");
                   out.println("<tr>" + 
                               "<td>" + m_name + "</td>" +
                               "<td>" + m_dept + "</td>" +
                               "<td>" + m_jobtitle + "</td>" +
                               "</tr>");
                }

                out.println("</table></body></html>");
                statement.close();
        }
        catch(Exception ex)
        {
                out.println("<HTML>" +
                            "<Head><Title>" +
                            "Bedrock: Error" +
                            "</Title></Head>\n<Body>" +
                            "<P>SQL error in doGet: " +
                            ex.getMessage() + "</P></Body></HTML>");
                return;
        }
        out.close();
    }
}

我收到以下错误任何人都知道为什么?:

javax.servlet.ServletException: Class not found Error
    ShowBedrock.init(ShowBedrock.java:42)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    java.lang.Thread.run(Unknown Source)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.35 logs.
4

3 回答 3

1

我收到以下错误任何人都知道为什么?

您收到该错误正文的原因是您的处理程序ClassNotFoundException正在丢弃原始异常堆栈跟踪。您正在将原始异常的消息写入System.err但(显然)不会进入正常系统日志。(您可能会在“catalina.out”文件中找到该消息……取决于 Tomcat 的配置/启动方式。)

正确的方法是链接异常;例如

    throw new ServletException("Class not found Error", e);

或在您捕获它时明确记录它。如果您明确记录它,请确保记录异常,而不仅仅是异常消息。


除非我们看到原始异常堆栈跟踪,否则我们无法告诉您问题的实际根本原因......但两个最可能的原因是:

  • 类加载器找不到需要的类;例如,因为 JAR 文件不在正确的位置。
  • 在您尝试加载的类的初始化期间,某些类的类初始化失败。
于 2012-04-21T02:46:24.487 回答
1

Tomcat 期望您的 servlet 位于一个包中。您的似乎在默认包中;请添加一个并重新编译。我敢打赌,到时候你会变得更好。

Tomcat 6 和 7 期望在服务器 /lib 目录中找到 JDBC JAR。将 PostgreSQL JDBC JAR 添加到服务器 /lib 位置。

于 2012-04-21T01:33:35.897 回答
0

Seem you did't add Postgresql JDBC driver into classpath, as the following error suggest:

javax.servlet.ServletException: Class not found Error

Which is origin from:

throw new ServletException("Class not found Error");

You can download Postgresql JDBC driver and put in PROJECT/WEB-INF/lib folder.

Suggestion

One suggestion, your exception handling is wrong, you should change

throw new ServletException("Class not found Error");

to

throw new ServletException("Class not found Error", e);
于 2012-04-21T01:39:03.213 回答