为什么不执行Get process processRequest?

时间:2018-12-09 13:07:11

标签: java html jsp jdbc javadb

该程序的工作方式是在后台有一个JavaDB,该servlet可以从中提取该数据库。 Servlet从index.jsp获取一个关键字,并使用该关键字搜索数据库以获取与该关键字相关的信息。但是doGet方法甚至没有尝试在try / catch块中运行任何内容。我知道doGet方法正在运行,因为我尝试在其中加载测试代码,并且确实显示了它,只是不会通过try / catch运行。

DBConnector.java

package edu.uwf.cs.dsa;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author Bernd
 */

public class DBConnector extends HttpServlet {

    private Context env;

    @Override
    public void init(ServletConfig config)
            throws ServletException {
        super.init(config);
        try {
            env = (Context) new InitialContext().lookup("java:comp/env");
        } catch (NamingException e) {
            throw new ServletException(e.getMessage());
        }
    }

    private String getUrl() throws NamingException 
    {
            return (String)env.lookup("jdbc:derby://localhost:1527/JA112");
    }

    private Connection getConnection() 
            throws NamingException, ServletException, ClassNotFoundException, SQLException
    {
            Class.forName((String)env.lookup("org.apache.derby.jdbc.EmbeddedDriver"));
            return DriverManager.getConnection(getUrl());
    }

    private void getMovies(Connection conn, PrintWriter out) throws SQLException
    {
            Statement stmt = conn.createStatement();

            ResultSet rs = stmt.executeQuery("SELECT * from Student");
            while (rs.next()) {
                out.print(rs.getString(1)); out.println("<br />");
                out.print(" " + rs.getString(2)); out.println("<br />");
                out.print(" " + rs.getString(3)); out.println("<br />");
                out.print(" " + rs.getString(4)); out.println("<br />");
                out.println(" " + rs.getString(5)); out.println("<br />");
            }
    }
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NamingException, ClassNotFoundException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection conn = getConnection();

      //  out.print("<html>\n <head>\n <body>\n<h1>test</h1>\n</body>\n</head>\n</html>");

        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet DBConnector</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet DBConnector at " + request.getContextPath() + "</h1>");
            out.println((String)env.lookup("driver")); 
            out.println("<br />");
            out.println(getUrl()); 
            out.println("<br />");
            out.println(conn);
            getMovies(conn,out);
            out.println("<br />");
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (Exception e) {
            Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

index.jsp

<%-- 
    Document   : index
    Created on : Nov 21, 2012, 9:20:16 PM
    Author     : Bernd
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form method="GET" action="/DBServlet/DBConnector">
            <h1>Movie Search</h1>
            Enter Keyword <input type="text" name="keyword" id="keyword">
            <input type="submit" value="submit" id="button">
        </form>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

我最好的猜测是,env变量在正常测试中已初始化为null,您需要在processRequest()方法上添加catch块,这样就可以清楚地看出这是错误,对于现在您只有finally块,因此Exception隐藏且不清楚。

一旦添加了块并看到了异常,就需要检查配置文件中定义java:comp/env的位置,可能是拼写错误或类似的东西可能引起真正的问题。