Jetty WebServlet

These are just notes on how the jetty web service is set up

ServerMain

In the app.yaml file, the class with the main for the service is given for the appengine (also the specification for the JAVA  version:

# [START gae_java11_firestore_yaml]

instance_class: F2

runtime: java11

entrypoint: 'java -cp "*" info.esblurock.background.services.ServerMain'

# [END gae_java11_firestore_yaml]

from App Engine app.yaml reference (Java)


entrypoint

Optional. Overrides the default startup behavior by executing the entrypoint command when your app starts. For your app to receive HTTP requests, the entrypoint element should contain a command which starts a web server that listens on port 8080.

For more information, see Application startup.



Similar to Google (github): simple-jetty-main

public static void main(String[] args) throws Exception {

   // Create a server that listens on port 8080.

   Server server = new Server(8080);

   WebAppContext webAppContext = new WebAppContext();

   server.setHandler(webAppContext);

   // Enable annotations so the server sees classes annotated with @WebServlet.

   webAppContext.setConfigurations(new Configuration[]{

     new AnnotationConfiguration(),

     new WebInfConfiguration(),

   });

   // Look for annotations in the classes directory (dev server) and in the

   // jar file (live server)

   webAppContext.setAttribute(

       "org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",

       ".*/target/classes/|.*\\.jar");

   webAppContext.addServlet(DefaultServlet.class, "/");

   webAppContext.setResourceBase(

           ServerMain.class

           .getClassLoader()

           .getResource("webapp")

           .toExternalForm());

   ARQ.init();

   // Start the server! 🚀

   server.start();

   System.out.println("Server started!");

   // Keep the main thread alive while the server is running.

   server.join();

 }


Server port

// Create a server that listens on port 8080.

   Server server = new Server(8080);

   WebAppContext webAppContext = new WebAppContext();

   server.setHandler(webAppContext);


@WebServlet

   // Enable annotations so the server sees classes annotated with @WebServlet.

   webAppContext.setConfigurations(new Configuration[]{

     new AnnotationConfiguration(),

     new WebInfConfiguration(),

   });

the web service entry points are designated by @WebServlet

for example, all the services have this pattern:

@WebServlet(name = "BackgroundTransaction", urlPatterns = { "/transaction" })

public class BackgroundTransaction extends HttpServlet {

...

}

For this reason, the servlets are not defined in the web.xml file

The services available are:

ResourceBase

webAppContext.setResourceBase(

           ServerMain.class

           .getClassLoader()

           .getResource("webapp")

           .toExternalForm());

This sets the position of the resource base, i.e.

webAppContext.setResourceBase("/path/to/your/webapp"); // Set the base directory for static resources.

This is where, for example, the index.html file is. When the Angular application is to be added to the application, this is the directory it should be put in.


In Jetty, the ResourceBase is a configuration property used to specify the base directory from which static resources will be served. It is typically used in conjunction with the DefaultServlet to serve static content, such as HTML files, images, CSS files, JavaScript files, etc.

When a request for a static resource (e.g., a web page, an image, a stylesheet) is received by the Jetty server, the DefaultServlet is responsible for locating and serving that resource. It looks for the requested resource within the ResourceBase directory. If the requested resource is found within this directory, Jetty serves it to the client.