我已经搜索了答案,但我得到了相关的答案。虽然我的例外不是新的,但我无法找到答案。我收到异常,服务器遇到内部错误,导致无法完成此
request.Exception:java.lang.NullPointerException
com.src.pkg.DepositServlet.doPost(DepositServlet.java:82)
javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
注意Apache Tomcat / 7.0.37日志中提供了根本原因的完整堆栈跟踪。
我也会发送我的代码。它指向DepositServlet的doPost()方法,它也向下面的行显示错误 - Statement st =((java.sql.Connection)con).createStatement(); 请帮我解决这个问题。
//DepositServlet.java
package com.src.pkg;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.sun.corba.se.pept.transport.Connection;
/**
* Servlet implementation class DepositServlet
*/
public class DepositServlet extends HttpServlet {
Connection con;
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=(Connection) DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","rekha");
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
catch(SQLException e)
{
System.out.println(e);
}
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
try
{
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
int ano = Integer.parseInt(request.getParameter("accno"));
float amt = Float.parseFloat(request.getParameter("amount"));
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
try
{
Statement st = ((java.sql.Connection) con).createStatement();
int n = st.executeUpdate("update account set balance = balance + "+amt+" where accno = "+ano);
if(n==1)
{
out.println("<body bgcolor=green>");
out.println("<h1> Successfully deposited </h1>");
}
if(n==0)
{
out.println("<body bgcolor=wheat>");
out.println("<h1> Wrong a/c no </h1>");
}
st.close();
}
catch(SQLException e)
{
out.println("<body bgcolor=red>");
out.println("<h1> Server problem </h1>");
}
out.println("</body>");
out.println("</html>");
out.close();
}
}
//web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<welcome-file-list>
<welcome-file>deposit.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>DepositServlet</servlet-name>
<servlet-class>com.src.pkg.DepositServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DepositServlet</servlet-name>
<url-pattern>/DepositServlet</url-pattern>
</servlet-mapping>
</web-app>
//deposit.html
<!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>Insert title here</title>
</head>
<body bgcolor="cyan">
<center>
<h1> Deposit screen</h1>
<form action = "./DepositServlet" method = "post">
A/C No <input type="text" name="accno"> <br> <br>
Amount <input type="text" name="amount"> <br> <br>
<input type="submit" value="deposit">
</form>
</center>
</body>
</html>
答案 0 :(得分:1)
问题部分:
首先,您需要知道这是危险且不稳定的事情。
&#34; init &#34;当 servlet加载时,该方法称为一次。 &#34; doPost&#34;在数周,数月或数年的时间内,方法可能被称为多次数百或数千次
。几乎所有数据库都会超时&#34;并且断开打开连接,如果它们打开时间过长。 如果你只打开&#34; init&#34;甚至可能超时&#34; 之前你可以使用它。这也意味着您无法重新打开 ga关闭连接而无需重新加载servlet ,即重新启动您的网络服务器。
总而言之,打开数据库连接是&#34; init&#34; 坏事要做。
===================
现在,解决方案部分。
你应该在一个监听器中实现这一点,更具体地说是一个ServletContextListener。
感谢@stdunbar 发表评论。
OR
答案 1 :(得分:0)
您需要查看Tomcat中的数据库connection pooling docs。最终你需要三件事。第一个是Web应用程序根目录下的META-INF / context.xml文件。这看起来像是:
<?xml version='1.0' encoding='utf-8'?>
<Context path="">
<Resource name="jdbc/someName"
auth="Container"
type="javax.sql.DataSource"
username="system"
password="rekha"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:xe"/>
</Context>
然后,在Web应用程序的WEB-INF / lib目录中,您需要放置Oracle瘦驱动程序jar / zip文件。最后,当您需要数据库连接时,您的代码将类似于:
import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
private static final String dbJNDIName = "jdbc/someName"; // matches context.xml name
public static Connection getConnection() {
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup( "java:comp/env/" + dbJNDIName );
if( ds != null ) {
return ds.getConnection();
}
else {
// log error somehow
}
}
catch( Exception e ) {
// log error
}
return( null );
}
最后,当您需要在代码中建立数据库连接时,您可以通过调用此方法来实现。您的代码中有一个java.sql.Connection强制转换,无需使用正确的导入。确保仍然关闭你得到的连接 - 池将做正确的事。