Servlet deployed to Jetty using Maven and Eclipse

That’s a quick guide to setup a test Servlet running on the lightweight Jetty web container, embedded and fired as a Maven plugin.

First off, create a New Maven Project in Eclipse.

Then define the following POM file:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.wordpress.dimitrisli</groupId>
  <artifactId>TestServletInJettyMaven</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <build>
  	 <finalName>TestServletInJettyMaven</finalName>
  	<plugins>
    <plugin>
    	<artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
  		<plugin>
  			<groupId>org.mortbay.jetty</groupId>
  			<artifactId>jetty-maven-plugin</artifactId>
  			<version>8.0.0.M3</version>
  			 <configuration>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <webAppConfig>
                        <contextPath>/TestServletInJettyMaven</contextPath>
                    </webAppConfig>
                </configuration>
  		</plugin>
  	</plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <dependency>
    	<groupId>javax.servlet</groupId>
    	<artifactId>javax.servlet-api</artifactId>
    	<version>3.0.1</version>
    	<scope>provided</scope>
    </dependency>
  </dependencies>
</project>

Then, it’s time for our Servlet class:

package servletService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class TestServletInJettyMaven extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("Get method called");
		out.println("parameters:"+parameters(request));
		out.println("headers:"+headers(request));
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.println("Post method called");
		out.println("parameters:"+parameters(request));
		out.println("headers"+headers(request));
	}
	
	private String parameters(HttpServletRequest request){
		StringBuilder builder = new StringBuilder();
		for(Enumeration<?> e = request.getParameterNames() ; e.hasMoreElements();){
			String name = (String)e.nextElement();
			builder.append("|"+name + "->"+request.getParameter(name));
		}
		return builder.toString();
	}
	
	private String headers(HttpServletRequest request){
		StringBuilder builder = new StringBuilder();
		for(Enumeration e = request.getHeaderNames() ; e.hasMoreElements();){
			String name = (String)e.nextElement();
			builder.append("|"+name +"->"+request.getHeader(name));
		}
		return builder.toString();
	}
}

And finally our web.xml that we need to define under the new folder structure: src/main/webapp/WEB-INF/web.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>Test Servlet In Jetty Maven</display-name>
        <description>Test Servlet In Jetty Maven Service
    </description>

    <servlet>
        <servlet-name>TestServletInJettyMaven</servlet-name>
        <servlet-class>servletService.TestServletInJettyMaven</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>TestServletInJettyMaven</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

To build the code and deploy to jetty, from the Run configurations of Eclipse, create a new Maven run configuration, make it point to your newly created project under the Base Directory section and set as Goals:

-Djetty.port=8081 jetty:run

Now connect to the following url to test the connection:

http://localhost:8081/TestServletInJettyMaven/test

For downloading the code of this sceletal bootstrap have a look at this Mercurial repository

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s