Simple Java App with Embedded Jetty

This example illustrates the steps to build a simple Java app with a Jetty server embedded in it. Eclipse Jetty is a lightweight Java web server as well as a servlet container. This application will then be compiled, bundled, and associated with an AppSail service as a Java SE type, and deployed to the console.

  1. Create a new Java project from an IDE of your choice.

  2. Include the following libraries in your source directory. We have created a new folder in this example and named it deps. These JAR files include all the servlets, utility files, and other dependencies that will embed the Jetty server into your application. You can fetch these dependencies from the official Maven repository. You can also add these as Maven dependencies.

    
copy
javax.servlet-api-3.1.0.jar jetty-io-9.4.50.v20221201.jar jetty-server-9.4.50.v20221201.jar jetty-util-9.4.50.v20221201.jar jetty-http-9.4.50.v20221201.jar jetty-security-9.4.50.v20221201.jar jetty-servlet-9.4.50.v20221201.jar jetty-util-ajax-9.4.50.v20221201.jar

  1. To create an embedded Jetty application, you will need to create an instance of org.eclipse.jetty.server.Server class and refer to the listening port with the environment variable X_ZOHO_CATALYST_LISTEN_PORT in your main code. You must add a Servlet handler to handle these requests. Create the following Java files in your main directory and add the sample code snippets given below:

CustomJettyServer.java

    
copy
import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; public class CustomJettyServer { public static void main( String[] args ) throws Exception { String port = System.getenv("X_ZOHO_CATALYST_LISTEN_PORT"); int listenPort; if(port != null && !port.isEmpty()) { listenPort = Integer.parseInt(System.getenv("X_ZOHO_CATALYST_LISTEN_PORT")); } else { listenPort = 9000; } Server server = new Server(listenPort); ServletContextHandler handler = new ServletContextHandler(); handler.addServlet(ServletHandler.class.getName(), "/"); server.setHandler(handler); server.start(); } }

ServletHandler.java

    
copy
import java.io.IOException; import java.nio.charset.StandardCharsets; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ServletHandler extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String path = req.getServletPath(); System.out.println(path); try { resp.getWriter().write("Hello ABC"); resp.getWriter().close(); } catch (Exception e) { e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } }

  1. Compile the application by executing the command given below:
    
copy
javac -cp ".:./*:./deps/*" *.java

  1. You can now initialize an AppSail service in the same directory from the CLI or add it in an existing project directory. The app’s source must be your application’s directory. Provide the following values while initializing the app service:

    Stack: Java 8
    Platform: Java SE

  2. Ensure the compiled file, along with the JAR files in the deps folder, are added in the build directory you specify during initialization. Catalyst will automatically ZIP your app files during deployment to the remote console.

  3. Deploy the app service to the console.

  4. You can then configure the startup command given below from the console:

    
copy
java -cp ".:./*:./deps/*" *.java

You can also configure this in the app-config.json file before deploying.


Access the deployed app service from its endpoint URL.

Last Updated 2023-12-14 16:25:23 +0530 +0530

ON THIS PAGE