The largest Interview Solution Library on the web

Interview Questions
« Previous | 0 | 1 | 2 | 3 | 4 | Next »


The purpose of this article is to give you a quick idea of how to build and deploy your web application without really running a web server outside your application and without creating a WAR file for the deployment. At present, I am working on a project which is a background calculation engine and it provides a simple web interface for the support and business users to monitor the ongoing activities. While deploying our application, we just start Jetty web server within our application and we can access the web application easily.
If you visit the Jetty wiki, it is mentioned that Jetty has a slogan, "Don't deploy your application in Jetty, deploy Jetty in your application." In this article, we illustrate this slogan in a nice and simple application.

2.Download Jetty?

Before we can start creating our project, we need to download any stable version of Jetty JARs fromJetty@eclipse downloads site. In this article, I have used Jetty 7. The per Jetty site comes with following bundle.

  • Core Jetty Web Server (HTTP & Websocket)
  • Core Jetty Servlet Container
  • JNDI, JMX, OSGi, JASPI modules
  • HTTP Client
Apart from Jetty JAR, we need to have JSK 1.6 and Eclipse 3.5 installed on our system. Once these three things are ready, then we are good to create our application.

3.Create an Eclipse Project?

You just need to follow the below mentioned steps to create a Java project in Eclipse.

  • Go to Eclipse's File menu and create a Java project.
  • Keep the default JRE option if it is selected as JavaSE-1.6.
  • Click the Library tab and select the Jetty Jar files.
  • Once the project has been created, add two more folders in your project "page" and "WEB-INF".
Once we are done with the creation of project, the project structure should look like the following diagram:
As our project is now setup, we are ready to create our web application which is going to be launched within our application.

4.Create a Web Application?

Create a deployment descriptor (i.e. web.xml) in WEB-INF folder. This is a very basic descriptor only welcome file name is mentioned within it.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
Next we need to create an index.jsp page within page folder, which is going to be displayed when we hit the URL in our web browser.
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Embedding Jettye</title>
<h2>Running Jetty web server from our application!!</h2>

5.Create a Jetty Server Instance?

First, we build a WebAppContext in which we can define location of our deployment descriptor and set the application context path. This WebAppContext required to set the header of Jetty server which will be illustrated in a later part of this section. The following code describes how to build WebAppContext.

package blog.runjetty.context;

import org.eclipse.jetty.webapp.WebAppContext;

public class AppContextBuilder {

private WebAppContext webAppContext;

public WebAppContext buildWebAppContext(){
webAppContext = new WebAppContext();
webAppContext.setDescriptor(webAppContext + "/WEB-INF/web.xml");
return webAppContext;
Once we created WebAppContext; we create a class which is responsible to manipulateorg.eclipse.jetty.server.Server class's instance. For this, I have built a wrapper JettyServer. It defines the running post (default 8585), sets context header (i.e. setting the WebAppContext), starts/stops the server and checks whether the server is running or not.
package blog.runjetty.server;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;

public class JettyServer {

private Server server;

public JettyServer() {
public JettyServer(Integer runningPort) {
server = new Server(runningPort);

public void setHandler(ContextHandlerCollection contexts) {

public void start() throws Exception {

public void stop() throws Exception {

public boolean isStarted() {
return server.isStarted();

public boolean isStopped() {
return server.isStopped();

6.Create a User Interface to Start and Stop Server?

Now we are going to build a simple swing based user interface which has a start/stop button. Depending on the server status, it either starts the server or turns it off.
Create an ActionListener which actually checks the current status of the server and toggles its state whenever any action is performed. In our application, we have createdServerStartStopActionListner which implements ActionListener interface and overrides actionPerformed method.

package blog.runjetty.ui.listener;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import blog.runjetty.server.JettyServer;

public class ServerStartStopActionListner implements ActionListener {

private final JettyServer jettyServer;

public ServerStartStopActionListner(JettyServer jettyServer) {
this.jettyServer = jettyServer;

public void actionPerformed(ActionEvent actionEvent) {
JButton btnStartStop = (JButton) actionEvent.getSource();
btnStartStop.setCursor(new Cursor(Cursor.WAIT_CURSOR));
try {
} catch (Exception exception) {
(new Cursor(Cursor.DEFAULT_CURSOR));
else if(jettyServer.isStopped()){
btnStartStop.setCursor(new Cursor(Cursor.WAIT_CURSOR));
try {
} catch (Exception exception) {
(new Cursor(Cursor.DEFAULT_CURSOR));
Once the ActionListener has been created, we need to create a JFrame. Our ServerRunner classextends JFrame. It has a JButton, which has been registered to ServerStartStopActionListner to perform start or stop oparation when clicked.
package blog.runjetty.ui;
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

import blog.runjetty.server.JettyServer;
import blog.runjetty.ui.listener.ServerStartStopActionListner;

public class ServerRunner extends JFrame{
private static final long serialVersionUID = 8261022096695034L;

private JButton btnStartStop;

public ServerRunner(final JettyServer jettyServer) {
super("Start/Stop Server");
btnStartStop = new JButton("Start");
(new ServerStartStopActionListner(jettyServer));
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
if(jettyServer.isStarted()) {
try {
} catch (Exception exception) {
},"Stop Jetty Hook"));
In ServerRunner, I have added a ShutdownHook. So, whenever the user clicks on the close button on the top right corner of the window, it will first check whether the server is running or not. If running, it will stop the server before exiting.

7.Create a Main Method?

Now once everything is ready, we are good to create a main method which triggers the user interface. The following code shows how to launch the interface.

package blog.runjetty.main;

import java.awt.EventQueue;

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;

import blog.runjetty.context.AppContextBuilder;
import blog.runjetty.server.JettyServer;
import blog.runjetty.ui.ServerRunner;

public class JettyFromMain {

public static void main(String[] args) {
ContextHandlerCollection contexts = new ContextHandlerCollection();

contexts.setHandlers(new Handler[]
{ new AppContextBuilder().buildWebAppContext()});

final JettyServer jettyServer = new JettyServer();
Runnable runner = new Runnable() {
public void run() {
new ServerRunner(jettyServer);
Here you can find that I am fist creating ContextHandlerCollection and adding our WebAppContext and later setting the handler into our server. In the run() method, I am creating an instance of ServerRunner. After the following implementation, our project structure should look like the following diagram:

8.Run the Application?

It's time now to test our application. Perform the following actions step by step to run the application and find out for yourself how it works.

  • Run the application from Eclipse.
  • You should see the following window:
  • Click on Start button. Wait until the text of the button changes to Stop as mentioned in the diagram below:
  • Open a web browser and hit the URL as http://localhost:8585/runJetty/. Your web application should be up and running as demonstrated in the figure below:
  • Now, click the Stop button or close the window directly.
  • If you click on the Stop button, wait for text appears as Start. Then refresh your browser window to verify that your web application is not running anymore as shown below:

9.Explain Jetty Embedded Hello World Application?

We used to deploy our application in server. In some cases, we need to deploy server in our application. In this post, I am going to explain you, how to deploy Jetty in your application.
I am going to use following Maven dependency.


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;

public class HelloWorld extends AbstractHandler {

public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html; charset=utf-8");

Welcome to Jetty Application


public static void main(String[] args) throws Exception {
Server server = new Server(8080);
server.setHandler(new HelloWorld());
Run above application and hit the url ‘http://localhost:8080/’ in browser. You will get following output.

Welecome to Jetty Application

Above statements create and run an HTTP server on port 8080. If you don’t set any handlers to this server, then it returns 404-error message for every request.

public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
target: Represents the target of the request, usually it is an URI.
baseRequest: Jetty mutable request object
request: Immutable request object, can be wrapped by a filter or servlet.
response: response send to user, can be wrapped by a servlet/filter.

server.setHandler(new HelloWorld());
Once we add the handler to server instance, it can able to handle HTTP requests.











« Previous | 0 | 1 | 2 | 3 | 4 | Next »

copyright © 2014 - all rights riserved by