java web项目本地运行正常发布到阿里云服务器上之后出错
 <%@ page language="java" import="java.util.*,db.DBLink,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>

  <body>
    <%
    DBLink db=new DBLink("my_test_db");
    Connection con=db.getDefaultCon();
    Statement sta=con.createStatement();
    ResultSet rs=sta.executeQuery("select * from users");
    if(rs.next()){
        out.print(rs.getString(1)+" "+rs.getString(2));
    }
     %>
  </body>
</html>

//数据库连接
DBLink.java
package db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DBLink {

    Connection con;
    Statement sta;
    ResultSet rs;
    PreparedStatement psta;
    ResultSetMetaData rsm;
    //119.23.240.242
    private String user = null, pwd = null, dataBaseName = "my_test_db", id = "localhost", port = "3306";

    public DBLink(String dataBaseName) {
        this.user = "root";
        this.pwd = "abc";
        this.dataBaseName = dataBaseName;
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
        }
    }

    public Connection getDefaultCon() {
        String uri = "jdbc:mysql://localhost:3306/" + dataBaseName+"?useUnicode=true&characterEncoding=utf-8&useSSL=false";
        setUser("root");
        setPwd("lfy");
        try {
            con = DriverManager.getConnection(uri, "root", "abc");
            return con;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

}

java web在本地可以正常运行

图片说明

但是发布到阿里云服务器上就不能运行
图片说明

错误信息:

HTTP Status 500 - An exception occurred processing JSP page /index.jsp at line 27

type Exception report

message An exception occurred processing JSP page /index.jsp at line 27

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 27

24: <%
25: DBLink db=new DBLink("my_test_db");
26: Connection con=db.getDefaultCon();
27: Statement sta=con.createStatement();
28: ResultSet rs=sta.executeQuery("select * from users");
29: if(rs.next()){
30: out.print(rs.getString(1)+" "+rs.getString(2));

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:567)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:469)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(index_jsp.java:126)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:431)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.18 logs.

0

3个回答

你的阿里云服务器是不是也是linux服务器?如果是,有可能是db路径出问题了。我之前遇到过类似问题,项目同样是在本地windows运行没问题,但到liunx服务器上就出问题了。原因是一个配置文件因为路径差异没找到。

1
shen931205
shen931205 回复不抓老鼠的猫li: 数据库配置,这个问题很明显是数据库路径不对,你应该把你的数据库配置贴一下
接近 2 年之前 回复
ilifeiyun
不抓老鼠的猫li 回复weixin_38869880: 没有,要改哪个配置文件
接近 2 年之前 回复
weixin_38869880
weixin_38869880 回复不抓老鼠的猫li: 解决了么?我之前也遇到过,在配置文件绝对路径上加个/ 就好使了。。不知道你是不是这样
接近 2 年之前 回复
ilifeiyun
不抓老鼠的猫li 对,就是linux的服务器,要改哪个配置?查了好久没查到解决办法
接近 2 年之前 回复

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 27
java.lang.NullPointerException
27: Statement sta=con.createStatement();
报错为27这一行,空指针异常
26:Connection con=db.getDefaultCon();
这一行的con,返回值为null,到27行,null.createStatement();当然是不行的,所以报了空指针异常。
问题出在:数据库建立连接失败

0
s_p_y_s
cckpspys 回复不抓老鼠的猫li: 不需要改成公网,还是本地的
一年多之前 回复
ilifeiyun
不抓老鼠的猫li 发布到云服务器的时候,连接数据库的ip地址不是应该改成公网ip吗?我的是能在本地运行,发布到云服务器的时候就改了下连接数据库的ip地址其他的都没有改
接近 2 年之前 回复

你这个是数据库访问问题吧,我之前也出现这个问题

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!