ilifeiyun
不抓老鼠的猫li
采纳率16.7%
2017-06-30 01:07 阅读 3.8k

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • cai100 君子如狼 2017-06-30 01:34

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

    点赞 1 评论 复制链接分享
  • CORA_S_T 千杯不醉476 2017-06-30 01:11

    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();当然是不行的,所以报了空指针异常。
    问题出在:数据库建立连接失败

    点赞 评论 复制链接分享
  • qq_38595638 我菜的一批 2017-09-09 04:24

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

    点赞 评论 复制链接分享

相关推荐