jetty

Embedding Jetty with Servlet

This is an example of how to embed Jetty server with Servlet. The Jetty Web Server provides an HTTP server and Servlet container capable of serving static and dynamic content either from a standalone or embedded instantiations. Jetty has a rich history of being embedded into a wide variety of applications. Here we will see how to deploy a servlet into jetty. The simple servlet is deployed and mounted on a context so as to be able to process requests. Embedding Jetty with Servlet implies that you should:

  • Create a HelloServlet that extends javax.servlet.http.HttpServlet and overrides its doGet(HttpServletRequest request, HttpServletResponse response) method, to set the response status and the content-type.
  • Create a Server Object that will listen on port 8080.
  • Create a ServletContextHandler that is backed by an instance of a Servlet and register it with the Server object.
  • Register the HelloServlet to the Server and mount it on a given context path.
  • Start the server.

Let’s take a look at the code snippet that follows:  

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.javacodegeeks.snippets.enterprise;
 
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.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
 
public class EmbeddingJettyWithServlet {
     
    public static void main(String[] args) throws Exception {
         
        Server server = new Server(8080);
         
        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
 
  context.setContextPath("/hello");
 
  server.setHandler(context);
  
 
  context.addServlet(new ServletHolder(new HelloServlet()), "/*");
        server.start();
         
    }
     
    public static class HelloServlet extends HttpServlet {
         
        private static final long serialVersionUID = -6154475799000019575L;
         
        private static final String greeting = "Hello World";
 
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response) throws ServletException,
                IOException {
             
            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println(greeting);
        }
         
    }
 
}

URL:

http://myhost:8080/hello/

Output:

Hello World

  
This was an example of how to embed Jetty with Servlet in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest


This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button