我正在使用 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)