Jetty Servlet Integration
Jetty has been developed 100% free and open source project from Eclipse foundation which provides functionality of web server. You can embed Jetty with your stand alone jar application project with using any application server. In this demo I will show you how to integrate Jetty with Servlet.
- Create maven project name: JettyServletIntegration and full project structure is below:
- pom.xml:
<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.javahonk</groupId> <version>0.0.1</version> <artifactId>JettyServletIntegration</artifactId> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <log4j.version>1.2.16</log4j.version> <org.apache.log4j.version>2.1</org.apache.log4j.version> </properties> <dependencies> <!-- Jetty/Jersey dependency --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.9</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.8.8</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.8.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.0</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-server</artifactId> <version>8.1.12.v20130726</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>8.1.12.v20130726</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.1</version> </dependency> <!-- Apache HTTP client --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.6</version> </dependency> <!-- Log4j --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>${org.apache.log4j.version}</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>${org.apache.log4j.version}</version> </dependency> </dependencies> </project>
- JettyServlet.java:
package com.javahonk; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletHandler; public class JettyServlet { private static final Logger LOGGER = LogManager.getLogger(JettyServlet.class.getName()); public static void main(String[] args) throws Exception { LOGGER.info("Jetty server started"); Server server = new Server(8080); ServletHandler handler = new ServletHandler(); handler.addServletWithMapping(HelloServlet.class, "/JavaHonkJettyServlet"); server.setHandler(handler); server.start(); server.join(); } @SuppressWarnings("serial") public static class HelloServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = request.getParameter("name"); String password = request.getParameter("password"); response.setContentType("text/html"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println("<h1>Java Honk: Jetty Servlet Spring Integration</h1>"); response.getWriter().println("<h2>You entered below parameter</h2>"); response.getWriter().println("<h3>User name: "+userName+"</h3>"); response.getWriter().println("<h3>Password: "+password+"</h3>"); } public void doPost(HttpServletRequest request, HttpServletResponse res) throws IOException, ServletException { doGet(request, res); } } }
- That’s it. Start JettyServer you will see below out put:
As you see Jetty server is started and it’s ready to serve the request. Now let’s create java http client who send one post request to Jetty Server also process response from Servlet:
- HttpClientPost.java:
package com.javahonk; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.List; import javax.ws.rs.core.Response; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; public class HttpClientPost { public static void main(String[] args) { postHttpPost("http://localhost:8080/JavaHonkJettyServlet"); } private static void postHttpPost(String url) { try { HttpClient httpClient = HttpClientBuilder.create().build(); HttpPost postRequest = new HttpPost(url); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("name", "Java Honk")); params.add(new BasicNameValuePair("password", "Jetty Servlet")); postRequest.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpClient.execute(postRequest); if (response.getStatusLine().getStatusCode() != Response.Status.OK .getStatusCode()) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } BufferedReader br = new BufferedReader(new InputStreamReader( (response.getEntity().getContent()))); String output; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { System.out.println(output); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
- If you run HttpClientPost.java you will below which is request and processed response from server:
- You could also test from other tools. I will be using Postman which is very good extension in Google Chrome to test HTTP functionality:
- For more information please visit Eclipse Jetty here
Download Project: JettyServletIntegration