Java 严重:servlet [SqlGatewayServlet] 的 Servlet.service() 在路径为 [/DEV-JDBC] 的上下文中引发异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18393998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SEVERE: Servlet.service() for servlet [SqlGatewayServlet] in context with path [/DEV-JDBC] threw exception
提问by Eric Huang
My question is very similar to [Link]: (Why Servlet.service() for servlet [XYZ] in context with path [/ABC] threw exception) But I don't think it was resolved.
我的问题与 [Link] 非常相似:(Why Servlet.service() for servlet [XYZ] in context with path [/ABC] throw exception)但我认为它没有得到解决。
This is a simple SQL gateway application, on my personal computer localhost it works fine. However, when I upload the war file to server it throws this exception
这是一个简单的 SQL 网关应用程序,在我的个人计算机 localhost 上运行良好。但是,当我将战争文件上传到服务器时,它会引发此异常
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SqlGatewayServlet] in context with path [/DEV-JDBC] threw exception
java.lang.NullPointerException
at sql.SqlGatewayServlet.do
org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [SqlGatewayServlet] in context with path [/DEV-JDBC] threw exception
java.lang.NullPointerException
at sql.SqlGatewayServlet.do
java.lang.NullPointerException
sql.SqlGatewayServlet.doPost(SqlGatewayServlet.java:34)
java.lang.NullPointerException
sql.SqlGatewayServlet.doPost(SqlGatewayServlet.java:34)
Line: 34 is Statement statement = connection.createStatement();
线路:34是 Statement statement = connection.createStatement();
SqlGatewayServlet
SqlGatewayServlet
public class SqlGatewayServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String sqlStatement = request.getParameter("sqlStatement");
String sqlResult = "";
try{
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
// create a statement
Statement statement = connection.createStatement();
// parse the SQL string
sqlStatement = sqlStatement.trim();
if(sqlStatement.length() > 6){
String sqlType = sqlStatement.substring(0, 6);
if(sqlType.equalsIgnoreCase("select")){
// create the HTML for the result set
ResultSet resultSet = statement.executeQuery(sqlStatement);
sqlResult = SQLUtil.getHtmlTable(resultSet);
resultSet.close();
}else{
int i = statement.executeUpdate(sqlStatement);
// a DDL statement
if(i == 0){
sqlResult = "The statement executed successfully";
}else{
sqlResult = "The statment executed sucessfully.<br>"
+ i + "row(s) affected.";
}
}
statement.close();
connection.close();
}
}catch( SQLException e){
sqlResult = "Error executing the SQL statement: <br>"
+ e.getMessage();
}
HttpSession session = request.getSession();
session.setAttribute("sqlResult", sqlResult);
session.setAttribute("sqlStatement", sqlStatement);
String url = "/index.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
context.xml
上下文.xml
<Context path="/DEV-JDBC">
<Resource name="jdbc/uplink_project" auth="Container"
maxActive="100" maxIdle="30" maxWait="10000"
username="xxxxx" password="xxxxxx"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:2082/uplink_project?autoReconnect=true"
logAbandoned="true" removeAbandoned="true"
removeAbandonedTimeout="60" type="javax.sql.DataSource" />
Is there something wrong with my setup? or its the server problem? Please let me know what other information you need.
我的设置有问题吗?还是服务器问题?请让我知道您需要什么其他信息。
Thank in advance
预先感谢
采纳答案by Juned Ahsan
If there is a null pointer exception in the following statement:
如果以下语句中出现空指针异常:
Statement statement = connection.createStatement();
then it means that your connection object is null.
那么这意味着您的连接对象为空。
As you are initializing your connection object from the ConnectionPool class. So make sure its getConnection method returns a valid connection object:
当您从 ConnectionPool 类初始化连接对象时。所以确保它的 getConnection 方法返回一个有效的连接对象:
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
You need to debug your ConnectionPool class.
您需要调试 ConnectionPool 类。
回答by Paul Vargas
May the problem is in the class ConnectionPool
on the method getConnection()
becase connection
is null
.
五月的问题是在类ConnectionPool
的方法getConnection()
becase的connection
是null
。