All Products
Search
Document Center

Elastic Compute Service:Deploy a Java web environment (Jetty)

Last Updated:Jan 09, 2025

Java Web is a technology stack that integrates the Java programming language and various technologies and frameworks to facilitate the development of dynamic web applications. Developers can use Java Web to develop complex, high-performance web applications that can be deployed across platforms. Jetty is an open source Servlet container, which is mainly used to run Java web applications. Compared with Apache Tomcat, Jetty is more lightweight and more suitable for lightweight applications, high-concurrency scenarios, and distributed environments. This topic describes how to deploy a Java web environment on an Elastic Compute Service (ECS) instance by using Jetty.

Prerequisites

Before you deploy a Java web environment on an ECS instance, make sure that the instance meets the following requirements:

  • The ECS instance is assigned a static public IP address (also called system-assigned or auto-assigned public IP address) or associated with an elastic IP address (EIP). For more information, see EIPs.

  • Rules are added to a security group of the instance to allow inbound traffic on remote connection and Jetty ports, such as ports 22, 3389, and 8080. For information about how to add a security group rule, see Add a security group rule.

  • A Java environment is deployed on the instance. For more information, see Deploy a Java environment.

Procedure

Perform the following operations based on the operating system of your ECS instance.

Linux

In this example, Jetty 12.0.16 is installed. If you install another version of Jetty or use other directories, replace the version and directories in the following commands with the actual version and directories.

  1. Connect to your Linux instance. For more information, see Use Workbench to connect to a Linux instance over SSH.

  2. Run the following commands to download and decompress the Jetty 12 installation package.

    Important
    • If you want to install another version of Jetty or the following download URL is invalid, visit the official Jetty website to obtain the latest download URL and replace the URL in the following wget command with the URL that you obtained.

    • Different versions of Jetty support different Java Development Kit (JDK) versions. Select an appropriate version of Jetty based on the JDK version that is installed when you deploy the Java environment. For more information, visit the official Jetty website.

      image

    wget https://repo1.maven.org/maven2/org/eclipse/jetty/jetty-home/12.0.16/jetty-home-12.0.16.tar.gz --no-check-certificate
    tar -zxvf jetty-home-12.0.16.tar.gz
  3. Run the following command to move the Jetty installation files to the /usr/local/jetty/ directory:

    sudo mv jetty-home-12.0.16 /usr/local/jetty/
  4. Run the following command to create the jetty-base directory:

    sudo mkdir /usr/local/jetty-base
  5. Configure environment variables.

    1. Run the following command to modify the ~/.bashrc file:

      sudo vim ~/.bashrc
    2. Press the I key to enter Insert mode and add the following content to the file.

      Note

      Set the JETTY_HOME parameter to the actual directory in which you want to install Jetty. Set the JETTY_BASE parameter to the jetty-base directory.

      export JETTY_HOME=/usr/local/jetty
      export JETTY_BASE=/usr/local/jetty-base
    3. Press the Esc key to exit Insert mode. Enter :wq and press the Enter key to save and close the file.

    4. Run the following command for the changes to take effect:

      source ~/.bashrc
  6. Start the Jetty module and initialize the jetty-base directory.

    1. Run the following command to go to the jetty-base directory:

      cd /usr/local/jetty-base
    2. Run the following command to initialize the jetty-base directory. After the command is run, the $JETTY_BASE/start.d/ directory and other directories that contain server configuration information are created.

      Note
      • You can run the java -jar $JETTY_HOME/start.jar --list-modules=* command to view which modules are available.

      • If you use Jetty 9, replace --add-module with --add-to-start.

      sudo java -jar $JETTY_HOME/start.jar --add-module=server,http,deploy

      image

  7. Check the environment configuration.

    Important

    The following commands must be run in the jetty-base directory.

    1. Run the following command to deploy the sample web applications provided by Jetty.

      • If you use Jetty 10 or 11, replace demos in the following command with demo.

      • If you use Jetty 9, run the sudo mv $JETTY_HOME/demo-base/webapps/ROOT $JETTY_BASE/webapps command to deploy the sample web applications.

      Note

      You can also upload the WAR package of an on-premises test project to the $JETTY_BASE/webapps directory for testing. For more information, see Upload or download files.

      sudo java -jar $JETTY_HOME/start.jar --add-module=demos
    2. Run the following command to start Jetty.

      Note

      By default, Jetty listens on port 8080. You can run the sudo netstat -tunlp | grep 8080 command to check whether the port is already in use. If the port is already in use, you can specify another port to remove the port conflict when you start Jetty. For example, run the nohup sudo java -jar $JETTY_HOME/start.jar jetty.http.port=8081 & command to specify a port for Jetty.

      nohup sudo java -jar $JETTY_HOME/start.jar &
    3. In the address bar of a web browser on your on-premises device, enter http://<Public IP address of the ECS instance>:8080. If the Jetty test page appears, the Java web environment is configured as expected.

      Note
      • If you changed the Jetty port number, replace 8080 with the new Jetty port number that you specified and confirm that a security group of the instance allows inbound traffic on the port. For information about how to add a security group rule, see Add a security group rule.

      • If you uploaded the WAR package of an on-premises test project, visit http://<Public IP address of the ECS instance>:8080/<Package name> to check whether you can access the project.

      image

Windows

Important

This tutorial applies only to Jetty 12, Jetty 11, and Jetty 10.

In this example, Jetty 12.0.16 is installed. If you install another version of Jetty or use other directories, replace the version and directories in the following steps with the actual version and directories.

  1. Connect to your Windows instance. For more information, see Use Workbench to connect to a Windows instance over RDP.

  2. Download the Jetty installation package that you want to install.

    Different versions of Jetty support different JDK versions. Select an appropriate version of Jetty based on the JDK version that is installed when you deploy the Java environment. For more information, visit the official Jetty website.

    image

    image

  3. Decompress the downloaded Jetty installation package.

    Decompress the downloaded installation package to the directory in which you want to install Jetty. In this example, the C:\download directory is used.

    Important

    You must decompress the Jetty installation package to a path that does not contain spaces.

  4. Configure the JETTY_HOME environment variable.

    1. Right-click This PC and select Properties. image

    2. On the About page, scroll down to the bottom and click Advanced system settings.image

    3. Click Environment Variables. image

    4. In the System variables section, click New to create a system variable and configure the parameters. Then, click OK. Set the variable name to JETTY_HOME and the variable value to the directory in which Jetty is installed, such as C:\download\jetty-home-12.0.16.

      image

    5. Click OK to save the environment variable settings.

  5. Create the jetty-base folder. In this example, the folder is created in the C:\download directory.

    image

  6. Initialize the jetty-base folder.

    1. Go to the jetty-base folder, enter cmd in the address bar, and then press the Enter key to open the command prompt.

      image

    2. Run the following command to initialize the jetty-base folder. After the command is run, the start.d directory and other directories that contain server configuration information are created.

      java -jar %JETTY_HOME%/start.jar --add-module=http,server,deploy

      image

  7. Verify Jetty.

    1. Run the following command to deploy the sample web applications provided by Jetty.

      If you use Jetty 10 or 11, replace demos in the following command with demo.

      Note

      You can also upload the WAR package of an on-premises test project to the <jetty-base>/webapps directory for testing. Replace <jetty-base> with the actual path of the jetty-base folder, such as C:\download\jetty-base. For more information, see Upload or download files.

      java -jar %JETTY_HOME%/start.jar --add-module=demos
    2. Run the following command to start Jetty. Do not close the command prompt window after Jetty is started.

      Note

      By default, Jetty listens on port 8080. You can run the netstat -ano | findstr :8080 command to check whether the port is already in use. If the port is already in use, you can specify another port to remove the port conflict when you start Jetty. For example, run the java -jar %JETTY_HOME%/start.jar jetty.http.port=8081 command to specify a port for Jetty.

      java -jar %JETTY_HOME%/start.jar
    3. In the address bar of a web browser on your on-premises device, enter http://<Public IP address of the ECS instance>:8080. If the Jetty test page appears, the Java web environment is configured as expected.

      Note
      • If you changed the Jetty port number, replace 8080 with the new Jetty port number that you specified and confirm that a security group of the instance allows inbound traffic on the port. For information about how to add a security group rule, see Add a security group rule.

      • If you uploaded the WAR package of an on-premises test project, visit http://<Public IP address of the ECS instance>:8080/<Package name> to check whether you can access the project.

      image

What to do next

  • After you deploy a Java web environment on an ECS instance, you can configure a website on the instance and bind a domain name to the public IP address of the instance. For more information, see Build a website.

    Select an appropriate development tool to develop web applications. If an application requires a database to store application data, add database configurations to the relevant configuration file of the corresponding web project, such as the Properties file, and connect to the database. After the development is complete, you can deploy the Java web applications to Jetty, such as to the $JETTY_BASE/webapps directory. After the Java web applications are deployed and started, you can access your applications over the web interface. For more information about Jetty, visit the official Jetty website.

    Note

    If you use a different version of Jetty, replace the version number in the URL with the corresponding version number.

    You can upload on-premises files to the ECS instance. For more information, see Upload a file to or download a file from a Linux instance.

    If you want to use a database to store and manage the data of a web application, you can use an ApsaraDB RDS for MySQL instance or deploy a MySQL database on the ECS instance. For more information, see Database overview.

  • You can use a Server Load Balancer (SLB) instance to improve the availability and performance of your website. For more information, see Getting Started.

FAQ

What do I do if I cannot access http://<Public IP address of the ECS instance>:8080 from my on-premises computer after I deploy a Java web environment by using Jetty on an ECS instance?

After you deploy a Java web environment on an ECS instance by using Jetty, you cannot access http://<Public IP address of the instance>:8080 from a web browser. This issue may occur due to the following causes:

  • Cause 1:

    The system firewall or Security-Enhanced Linux (SELinux) is enabled on the instance and rules are configured to block external access. As a result, you cannot access the Jetty website. You can use the following solutions to resolve the issue:

  • Cause 2:

    When you started Jetty, you changed the listening port of Jetty. You can use the following solution to resolve the issue:

    Replace 8080 in http://<Public IP address of the ECS instance>:8080 with the port number that you specified, and check the security group rules of the instance to confirm that the port is allowed. For information about how to add a security group rule, see Add a security group rule.