User Tools

Site Tools


java_jetty_server_on_metaarray

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
java_jetty_server_on_metaarray [2022/01/20 23:07] – fixed pom to not include troublesome things that they'll need later but not now, and added the exec plugin so it runs easily from maven peteyboyjava_jetty_server_on_metaarray [2024/09/05 04:25] (current) – al” hc9
Line 1: Line 1:
-FIXME  this is under construction 
- 
- 
 ====== Setting up a simple Java Jetty Server  ====== ====== 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 [[https://www.eclipse.org/jetty/ |Jetty]]. 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 [[https://www.eclipse.org/jetty/ |Jetty]].
Line 9: Line 5:
 You can create a java server project that embeds Jetty and run it on [[metaarray|the MetaArray]] on your [[meta_port_allocation|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. You can create a java server project that embeds Jetty and run it on [[metaarray|the MetaArray]] on your [[meta_port_allocation|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 frameworksI am going to show an example using [[https://javalin.io | 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'').+There are lots of different java web frameworks that center around using the [[https://www.eclipse.org/jetty/ |Jetty]] server, including a few microframeworksThis page is going to go through an example using [[https://javalin.io | Javalin]], which is a microframework that has a lighter footprint and is easier to configure than 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 ===== ===== Create Project Space =====
  
-First, we'll create your maven-style project folder.+First, let'create your maven-style project folder.
   - Go to an appropriate project folder that you want to work in   - Go to an appropriate project folder that you want to work in
   - make the java source folder in the maven style   - make the java source folder in the maven style
     * ''mkdir -p src/main/java/myproject''     * ''mkdir -p src/main/java/myproject''
- 
  
 ===== Create Project Object Model ===== ===== 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.+Next, let'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''+  - Create a new file in myproject, call it ''pom.xml''
     * ''nano pom.xml''     * ''nano pom.xml''
   - Paste in the following: <code>   - Paste in the following: <code>
Line 79: Line 74:
       </plugin>       </plugin>
       <plugin>       <plugin>
 +        <!-- Exec plugin lets us run app directly from maven and not deal with classpaths -->
        <groupId>org.codehaus.mojo</groupId>        <groupId>org.codehaus.mojo</groupId>
          <artifactId>exec-maven-plugin</artifactId>          <artifactId>exec-maven-plugin</artifactId>
Line 90: Line 86:
          </executions>          </executions>
          <configuration>          <configuration>
 +           <!-- This is what exec will run as project main, *CHANGE THIS IF YOU ARE USING A DIFFERENT MAIN CLASS-->
            <mainClass>myproject.helloWorld</mainClass>            <mainClass>myproject.helloWorld</mainClass>
          </configuration>          </configuration>
Line 102: Line 99:
 ===== Add Your Java Code ===== ===== Add Your Java Code =====
  
- - Using an editor again, create your hello world class file, in its proper folder for maven:+  - Using an editor again, create your hello world class file, in its proper folder for maven:
     - ''nano src/main/java/myproject/helloWorld.java''     - ''nano src/main/java/myproject/helloWorld.java''
     - paste in the following: <code java>     - paste in the following: <code java>
Line 134: Line 131:
   [INFO] Finished at: 2022-01-20T21:04:04Z   [INFO] Finished at: 2022-01-20T21:04:04Z
   [INFO] ------------------------------------------------------------------------   [INFO] ------------------------------------------------------------------------
-   +
-  +
 ===== Run Your Server ===== ===== Run Your Server =====
  
-Now, let's run the server. We've set up an "exec goalin the pom so we can run the resulting project from maven, like so:+Now, let's run the server. We've set up an exec goal” in the POM so we can run the resulting project from maven, like so:
  
   mvn exec:java   mvn exec:java
      
-You should see something like this:+You should see something like this (hopefully any warnings you get are ignorable):
  
   WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations   WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
Line 167: Line 163:
   [myproject.helloWorld.main()] INFO io.javalin.Javalin - Listening on http://localhost:32056/   [myproject.helloWorld.main()] INFO io.javalin.Javalin - Listening on http://localhost:32056/
   [myproject.helloWorld.main()] INFO io.javalin.Javalin - Javalin started in 263ms \o/   [myproject.helloWorld.main()] INFO io.javalin.Javalin - Javalin started in 263ms \o/
 +  
 +If you get here, then your server is running! Now try it out: point your web browser to //http://yourusername.yourSDFmetarraydomain.org:yourMetaArrayPort // and see it if works!
  
-Now try it out! point your web browser to //http://yourusername.yourSDFmetarraydomain.org:yourMetaArrayPort // and see it if works!+Press ''ctl-c'' to stop the server
  
-If it does it will look like this:+The rest is up to your imagination!
  
java_jetty_server_on_metaarray.1642720068.txt.gz · Last modified: 2022/01/20 23:07 by peteyboy