Showing posts with label jetty. Show all posts
Showing posts with label jetty. Show all posts

Monday, January 19, 2015

Embedding Jetty Server: HelloWorld example

Jetty has a slogan, "Don't deploy your application into Jetty. Deploy Jetty into your application". This means that instead of bundling you application as a standard WAR deployed in Jetty, you can use Jetty server as a software component in a Java program just like any POJO.

1. Download the following JAR files:

- Jetty aggregate jar file from http://central.maven.org/maven2/org/eclipse/jetty/aggregate/jetty-all/9.3.0.M1/jetty-all-9.3.0.M1.jar Jetty is decomposed into many different dependencies or JAR files. The aggregate JAR file contains all classes of the dependencies. It should not be used for production. All dependencies should be managed by Maven.

- Servlet API 3.0 from http://central.maven.org/maven2/org/eclipse/jetty/orbit/javax.servlet/3.0.0.v201112011016/javax.servlet-3.0.0.v201112011016.jar Please note that Jetty 9 is compatible with Servlet 3.0 so it won't work with Servlete 2.5.


2. Create handler class, HelloWorld.java

import java.io.IOException;
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 {

    @Override
    public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletExce$
        response.setContentType("text/html;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        baseRequest.setHandled(true);
        response.getWriter().println("<h1>Hello World</h1>");
    }

    public static void main(String[] args) throws Exception {
        Server server = new Server(8080);
        server.setHandler(new HelloWorld());
        server.start();
        server.join();
    }
}

3. Compiling

> javac -cp javax.servlet-3.0.0.v201112011016.jar:jetty-all-9.3.0.M1.jar HelloWorld.java

4. Running

> java -cp .:javax.servlet-3.0.0.v201112011016.jar:jetty-all-9.3.0.M1.jar HelloWorld.java
2015-01-04 21:15:28.066:INFO::main: Logging initialized @140ms
2015-01-04 21:15:28.175:INFO:oejs.Server:main: jetty-9.3.0.M1
2015-01-04 21:15:28.248:INFO:oejs.ServerConnector:main: Started ServerConnector@7afc02c8{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2015-01-04 21:15:28.250:INFO:oejs.Server:main: Started @329ms

Open browser and type http://localhost:8080 and then you should see "Hello World".








Thursday, January 8, 2015

Downloading and Installing Jetty

Jetty is a web server and servlet container. In 2009, it moved its core components to the projects of Eclipse Foundation, in order to broaden licensing and community of the project.


Installation

1. Download the latest distribution here http://download.eclipse.org/jetty/
2. Extract the distribution file jetty-distribution-9.2.6.v20141205.zip, for example.
3. Start Jetty:
> cd jetty-distribution-9.2.6.v20141205
> java -jar start.jar
2015-01-02 20:51:35.971:INFO::main: Logging initialized @1044ms
2015-01-02 20:51:36.063:WARN:oejs.HomeBaseWarning:main: This instance of Jetty is not running from a separate {jetty.base} directory, this is not recommended.  See documentation at http://www.eclipse.org/jetty/documentation/current/startup.html
2015-01-02 20:51:36.351:INFO:oejs.Server:main: jetty-9.2.6.v20141205
2015-01-02 20:51:36.370:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/Users/vathanakmao/workspace/jetty-distribution-9.2.6.v20141205/webapps/] at interval 1
2015-01-02 20:51:36.424:INFO:oejs.ServerConnector:main: Started ServerConnector@69121bc1{HTTP/1.1}{0.0.0.0:8080}
2015-01-02 20:51:36.425:INFO:oejs.Server:main: Started @1497ms
4. Open browser and type http://localhost:8080/ You will see error 404 page below. It means Jetty is running but there is no any default page to serve. You can check in the jetty-distribution-9.2.6.v20141205/webapps directory. It's empty. In previous versions of Jetty, there are default page included.