我有一个带有 doPost 的表单:
<form id="frmData" name="frmData" action="ProcessReg" method="post">
<td>Full Name</td>
<td><input type="text" id="stFullName" name="stFullName" value=""></td>
我收到 HTTP 500 错误,并且 Tomcat 日志将命中request.getParameter
部分的行指示为问题。
java.lang.NullPointerException
ProcessReg.processRequest(ProcessReg.java:86)
ProcessReg.doPost(ProcessReg.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
我的小服务程序:
public class ProcessReg extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
PreparedStatement ps = null;
Connection con = null;
ResultSet rs = null;
Statement stmt = null;
String strName = "";
strName = request.getParameter("stFullName");
try {
DBConnect a = new DBConnect();
stmt = (Statement) DBConnect.getConnection();
rs = stmt.executeQuery("SELECT * FROM members");
我希望有人能在它完美运行之前告诉我出了什么问题,我真的迷失了错误在哪里。
完整的堆栈跟踪:
SEVERE: Servlet.service() for servlet [ProcessReg] in context with path [/Web1] threw exception
java.lang.NullPointerException
at ProcessReg.processRequest(ProcessReg.java:86)
at ProcessReg.doPost(ProcessReg.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
还有我的 DBConnect.java :
public class DBConnect {
private static Connection con;
public static void createConnection(String dbUrl,String dbusername,String dbPassword){
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:practODBC, 231, abc");
} catch (Exception ex) {
}
}
public static Connection getConnection(){
return con;
}
public static void closeConnection(){
if(con!=null){
try {
con.close();
} catch (SQLException ex) {
}
}