以下是我的JSP页面:
<%@page import="connection.getConnection"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="connection.getConnection.*" %>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Create Account</title>
</head>
<body>
<center><b>Create Account</b></center>
<div>
<table align="center">
<tr>
<td>
Full Name
</td>
<td>
<input type="text" name="fullname"></input>
</td>
</tr>
<% String fullname=request.getParameter("fullname"); %>
<tr>
<td>
Account Open Date
</td>
<td>
<input type="text" name="accopndate"></input>
</td>
</tr>
<% String accopndate=request.getParameter("accopndate"); %>
<tr>
<td>
CIF ID
</td>
<td>
<input type="text" name="cifid"></input>
</td>
</tr>
<% String cifid=request.getParameter("cifid"); %>
<tr>
<td>
Address
</td>
<td>
<input type="text" name="address"></input>
</td>
</tr>
</table>
</div>
</body>
</html>
<% String address=request.getParameter("address");
System.out.println(fullname+accopndate+cifid+address);%>
<%
Connection con=getConnection.getConnectionBuilder();
PreparedStatement pstmt=con.prepareStatement("Insert into account_master(name,acct_opn_date,cif_id,address) values(?,?,?,?)");
pstmt.setString(1,fullname);
pstmt.setString(2,accopndate);
pstmt.setString(3,cifid);
pstmt.setString(4,address);
int updatecount=pstmt.executeUpdate();
pstmt.close();
%>
我收到JAVA空指针异常。 HTML页面根本没有出现。当我在Apache Tomcat服务器上运行时,它直接显示我的堆栈跟踪,错误是Java空指针。但是如果HTML页面没有来,我无法输入值,异常是一定会来的。
生成Stacktrace:
nullnullnullnull
Error loading driver
15 Aug, 2014 6:22:36 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/Bank_Web] threw exception [An exception occurred processing JSP page /Create_Account.jsp at line 61
58:
59: <%
60: Connection con=getConnection.getConnectionBuilder();
61: PreparedStatement pstmt=con.prepareStatement("Insert into account_master(name,acct_opn_date,cif_id,address) values(?,?,?,?)");
62: pstmt.setString(1,fullname);
63: pstmt.setString(2,accopndate);
64: pstmt.setString(3,cifid);
Stacktrace:] with root cause
java.lang.NullPointerException
at org.apache.jsp.Create_005fAccount_jsp._jspService(Create_005fAccount_jsp.java:128)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
我认为您的jsp文件不符合您的要求。当你这样做时:
<tr>
<td>
Full Name
</td>
<td>
<input type="text" name="fullname"></input>
</td>
</tr>
<% String fullname=request.getParameter("fullname"); %>
您正在创建输入字段,但是从请求参数读取字符串。因此,如果您在输入字段中键入“my-full-name”,则字符串仍然会有null
或空值,如果未在请求中设置,请执行以下操作:www.my-url.com?fullname=my-name
。
要做我认为你想做的事,你可以创建两个文件。第一个接收表单,第二个接收表单并解析其信息。
(但我不认为这会导致异常,这只是我告诉你的,除非我真的错了或你做的事情真的不同,我认为你的代码可能不会起作用。)< / p>
==编辑==
除此之外,我认为你没有以正确的方式连接到你的数据库,我从未见过connection.getConnection()
。我通常使用的是:
try {
System.out.println("Loading db driver...");
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
dbDriverLoaded = true;
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT VERSION()");
if (rs.next()) {
System.out.println("Db driver loaded! Version " + rs.getString(1));
}
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot find the driver in the classpath!", e);
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
但为了实现这一点,您需要将mysql-connector-java放在WebContents/WEB-INF/lib
文件夹中。