Embed jetty server in a Spring Web Application

In this post, I am taking a very basic example of a spring web application. And, I use embedded jetty to run it.

You can watch me coding this app on youtube.

 

 

And, you can get the entire source code on github directory.

Make sure you have installed maven on your system.

Run this command ->

mvn archetype:generate -DgroupId=com.refactoredcodes -DartifactId=embed-jetty -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.4 -DinteractiveMode=false

embed-jetty  is the name of folder in which our application is going to live.

Open the pom.xml  file and add below mentioned xml in the plugins section.

<plugin>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>${jetty.version}</version>
</plugin>

You have to replace the jetty.version with the most stable version at the time of your coding.

The version, that I am using for my application is 10.0.0.beta2 .

After, you have made the changes then run the following command

mvn package => to make the war file

mvn jetty:run => to run the jetty server

Now, you can go to localhost:8080 and you will see some response like Hello World.

You should be able to run this program without any issue.

Now, you need to add below mentioned xml for getting spring-mvc dependency.

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spring.version}</version>
</dependency>

Add, the above xml in the dependencies section of the pom.xml.

Now, we need to create a controller and xmls so, that we can test the embedded jetty.

Now, I am going to create folder structure for my java controller code.

mkdir -p src/main/java/com/refactoredcodes/controllers

And, now i will switch to the controllers folder and write my controller file.

cd src/main/java/com/refactoredcodes/controllers

open your editor of choice and copy below java code in that java file.

package com.refactoredcodes.controllers;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

@Controller
public class JaiController{
    
    @ResponseBody
    @RequestMapping("/jai")
    public String jai(){ return "Jai Shree Ram!!!";  }
}

This controller returns a string, that is the only thing it does.

Now, we need to write configuration files so that jetty server can read that file and configure our controller.

change the directory into WEB-INF folder

cd src/main/webapp/WEB-INF/

open the web.xml file in your prefered editor and copy the below xml in that file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app 
   version="4.0" 
   xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
     http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">

     <welcome-file-list>
       <welcome-file>index.jsp</welcome-file>
     </welcome-file-list>

     <servlet>
        <servlet-name>jai</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <on-load-startup>1</on-load-startup>
     </servlet>

     <servlet-mapping>
       <servlet-name>jai</servlet-name>
       <url-pattern>/jai</url-pattern>
     </servlet-mapping>

   
</web-app>

In this xml file, we have configured the dispatcher servlet with name jai.

We need to create a jai-servlet.xml file to provide configuration for the spring servlet.

Copy the xml given belwo and paste that in jai-servlet.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<beans 
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.refactoredcodes.controllers"/>

</beans>

In the above xml file, we tell the spring framework that our useful classes are in the com.refactoredcodes.controllers folder.

It is necessary to tell the spring-framework location of our classes otherwise our beans won’t instantiate.

Now, go back to the root folder i.e embed-jetty.

And run this command again -> mvn package jetty:run

Open your browser and type the address to see the message localhost:8080/jai.

You can see my code on the github to see how the folders are structured.

And, I have uploaded the target folder to show, how compiled and generated files spread in the file.

Exceptions:

If you get an error of things like cannot find javax.servlet.HttpServlet. Then use lower version of jetty-maven-plugin. Chances are the version that you have used in the pom.xml is not stable and is still under development.

If you see 404 for /jai url, then check the web.xml and jai-servlet.xml to make sure you have correct bace-package name.

If you are typing the xmls in the editor make sure there is no gap between ‘?‘ and ‘xml‘ :- <? xml => <?xml 

There should not be any spelling mistakes in the xml.

If you have some typo in the xmls, then server will tell you something like , there should be a name  parameter in the xml, it is missing.