Monday, December 12, 2016

Simple Maven and Jetty Application

Today I have successfully developed a simple Java Application that uses Maven and Jetty.

The project was called "MaveJettyApp“ and the file structure was the following:

The pom.xml file:

    4.0.0
    com.zygimantus
    MavenJettyApp
    war
    1.0-SNAPSHOT
    
        
            javax.servlet
            javax.servlet-api
            3.1.0
        
    
    
        
            
                org.mortbay.jetty
                jetty-maven-plugin
                8.1.16.v20140903
            
        
    

The MainServlet.java file:
package com.zygimantus;

import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {""})
public class MainServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.getWriter().write("Hello World!");
        } catch (IOException e) {
        }
    }
}

In this example Jetty is used as Maven plugin. Using the command mvn jetty:run runs the server. And after opening http://localhost:8080/ in a browser a successful message "Hello World" was shown.

Full source code can be found here: https://gist.github.com/zygimantus/104ee4cba0571388234dab989b86cba0

No comments:

Post a Comment