简体   繁体   English

在远程服务器上加载Web应用程序(Tomcat上的Servlet)

[英]Load web app (servlet on Tomcat) in a remote server

I developed a web application that runs on my computer on localhost. 我开发了一个在本地计算机上运行的Web应用程序。 Then I loaded the war file into catalina home on a remote server. 然后,我将战争文件加载到远程服务器上的catalina主页中。 Web app runs but it stops when it try to connect to database on server. Web应用程序可以运行,但是在尝试连接到服务器上的数据库时会停止。 The connection is a jbdc connection on localhost, the database is mysql. 该连接是localhost上的jbdc连接,数据库是mysql。 When I do a connection on my computer, no problems occour. 当我在计算机上进行连接时,没有出现问题。

    String connectionString="jdbc:mysql://192.168.0.100:3306/"+request.getSession().getAttribute("dbname"); 
    Connection con=null;
    try {
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                response.sendRedirect("Errore.html");
                return;
            };
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    try {
            con=(Connection) DriverManager.getConnection(connectionString,"root","root");
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

The Connection con is null,DriveManager.getConnection doesn't work and I don't know why. Connection con为null,Dri​​veManager.getConnection不起作用,我也不知道为什么。 I also tried with postgresql connection but the problem is the same. 我也尝试使用postgresql连接,但问题是相同的。 Must I configure something in remote server? 我必须在远程服务器中配置一些东西吗? The Server is debian 9.2 like my computer. 服务器就像我的计算机一样是debian 9.2。

我认为您需要将连接字符串定义为不是本地主机,而是完整的IP: String connectionString="jdbc:mysql://xx.xx.xx.xx:3306/"+request.getSession().getAttribute("dbname");

Try the following code to test the connection. 请尝试以下代码来测试连接。 Its a Maven project. 它是一个Maven项目。

The pom.xml pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.test</groupId>
    <artifactId>mavenproject2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>App</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

and the src/main/java/App.java (please replace the connection data) 和src / main / java / App.java(请替换连接数据)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class App {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String host = "localhost";
        String db = "mysql";
        String user = "root";
        String password = "root";
        String connectionString = String.format("jdbc:mysql://%s:3306/%s", host, db);
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(connectionString, user, password);
        System.out.println("Everthing looks alright!");
    }

}

If you compile this project you will get a mavenproject2-0.0.1-SNAPSHOT-jar-with-dependencies.jar Upload the JAR to your remote system and try it there 如果编译此项目,则将获得mavenproject2-0.0.1-SNAPSHOT-jar-with-dependencies.jar将JAR上载到远程系统并在此处尝试

java -jar mavenproject2-0.0.1-SNAPSHOT-jar-with-dependencies.jar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM