0

我正在使用 jsp 和 servlet 开发注册系统,当我在本地调整时,我没有得到 NullPointerException。但是当我将它部署到远程服务器时,我得到了这个 NullPointerException。

我的代码中是否有任何错误。

package com.app.base;

//imported necessary packages. removes for readability.

public class RegisterServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    PrintWriter out = null;
    // Connection connection = null;
    // Statement statement;
    // ResultSet rs;

    resp.setContentType("text/html");
    out = resp.getWriter();

    String fname = req.getParameter("fname");
    String lname = req.getParameter("lname");
    String email = req.getParameter("email");
    String password = req.getParameter("password");
    String city = req.getParameter("city");
    String country = req.getParameter("country");

    Connection connection = null;
    try {
        // Load the JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        // Create a connection to the database
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306,/main",
                "root", "root");

    } catch (SQLException ex) {
        resp.sendRedirect("index.jsp?error=SQLError");
    } catch (ClassNotFoundException e) {
        resp.sendRedirect("index.jsp?error=Class Not Found");
    }

    PreparedStatement statement;
    try {
        statement = connection
                .prepareStatement("INSERT INTO main.users(first_name, last_name, email, password, "
                        + "city, country, registered_time)VALUES(?,?,?,?,?,?, NOW())");
        statement.setString(1, fname);
        statement.setString(2, lname);
        statement.setString(3, email);
        statement.setString(4, password);
        statement.setString(5, city);
        statement.setString(6, country);

        int rs = statement.executeUpdate();

        if (rs == 1) {
            resp.sendRedirect("index.jsp?registered=true");
            // String destination = "index.jsp?registered=true";
            // RequestDispatcher rd =
            // getServletContext().getRequestDispatcher(destination);
            // rd.forward(req, resp);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        resp.sendRedirect("index.jsp?registered=false");
    }

}

}

堆栈跟踪:

  SEVERE: Servlet.service() for servlet [Register] in context with path [] threw exception
  java.lang.NullPointerException
at com.app.base.RegisterServlet.doPost(RegisterServlet.java:79)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:306)
at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:322)
at org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1688)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
4

2 回答 2

2

在所有可能的引擎盖中找不到驱动程序类,这意味着对的调用会Class.forName()抛出ClassNotFoundException. 您发送重定向(顺便说一句,不记录异常,这不是一个好主意,您现在已经可以看到很难找到错误),但您没有从通话中返回。null当您尝试调用时,保留连接并稍后在您的代码中导致 NPE prepareStatement)。

return ;在您发送重定向后添加一个以停止您的呼叫。

当然,您应该找到一种或另一种方法在您的 Tomcat 上提供该驱动程序。

于 2012-04-20T22:54:51.510 回答
0

你错误地把 extra , 在你的字符串

connection = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306,/main",
            "root", "root");

注意,/main

相反,它应该是

connection = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/main",
            "root", "root");

对不起,如果这不是正确的答案,我不是像上面所有人一样的专家

于 2012-10-16T08:36:17.020 回答