This is an old revision of the document!
Table of Contents
this is under construction
Setting up a simple Java Jetty Server
Java web programming includes a servlet architecture for making server applications in java and having the nitty gritty of managing the web server part of the application to a web server that functions as a servlet container. One popular embedded server for Java projects is Jetty.
You can create a java server project that embeds Jetty and run it on the MetaArray on your assigned port. Below is a very simple example on how to get started with that. It doesn't use any IDE, so it's a bit manual. You can find more detailed directions at the external project links below.
There are lots of different java frameworks that center around using the Jetty server, including a few micro frameworks. I am going to show an example using Javalin, which is a microframework that is a lighter load that Spring Boot and other more mainline frameworks. It has a simple tutorial that was used and modified a bit here to show you how to set up a Java Jetty server. To make things quick to set up, we'll use Maven (mvn
).
Create Project Space
First, we'll create your maven-style project folder.
- Go to an appropriate project folder that you want to work in
- make the java source folder in the maven style
mkdir -p src/main/java/myproject
Create Project Object Model
Next, we'll set up the POM file, which maven will use to pull all the dependencies for our framework and let us build it.
- Create a new file in “myproject”, call it
pom.xml
nano pom.xml
- Paste in the following:
<project> <!-- model version - always 4.0.0 for Maven 2.x POMs --> <modelVersion>4.0.0</modelVersion> <!-- project coordinates - values which uniquely identify this project --> <groupId>myproject.helloWorld</groupId> <artifactId>helloWorld</artifactId> <version>1.0.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.target>1.8</maven.compiler.target> </properties> <!-- library dependencies --> <dependencies> <dependency> <groupId>io.javalin</groupId> <artifactId>javalin</artifactId> <version>4.3.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.31</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.0</version> <configuration> <source>1.8</source> <target>1.8</target> <showDeprecation/> <showWarnings/> <compilerArgs> <arg>-Xlint:unchecked</arg> <arg>-Xlint:all</arg> <arg>-Xlint:-serial</arg> </compilerArgs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.19.1</version> <configuration> <systemPropertyVariables> <java.util.logging.config.file>src/main/resources/logging.properties</java.util.logging.config.file> </systemPropertyVariables> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>myproject.helloWorld</mainClass> </configuration> </plugin> </plugins> </build> </project>
- Save the file
Add Your Java Code
- Using an editor again, create your hello world class file, in its proper folder for maven:
- ''nano src/main/java/myproject/helloWorld.java
- paste in the following:
package myproject; import io.javalin.Javalin; public class helloWorld { private static int MY_JETTY_PORT= 8388; //replace 8388 with your port as explained in the wiki public static void main(String[] args){ Javalin app = Javalin.create().start(MY_JETTY_PORT); app.get("/", ctx -> ctx.result("Hello World, this is my Java App on Jetty!")); } }
- move the cursor up and change the value of MY_JETTY_PORT to be your Port on the MetaArray (that is, replace 8388 with your UID number).
- save the file
Build Your Project
Here we go. Back at the command line, type
mvn package
You will see a bunch of log info scroll by, but at the end, you should see something like this:
[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.537 s [INFO] Finished at: 2022-01-20T21:04:04Z [INFO] ------------------------------------------------------------------------