Jetty and Maven HelloWorld

Source: Internet
Author: User

Jetty and Maven HelloWorld

ApacheMaven is a software project management and understanding tool. Based on the project Object Model (POM), Maven can manage a project building, report, and document through the information center. It is an ideal tool for building Web application projects. This project can run Web applications in deployment mode using the Jetty Maven plug-in.

You can use Maven to build embedded Jetty applications and standard Web-based applications.

To understand the basic operations for building and running Jetty, read:

1) Jetty HelloWorld tutorial

Http://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld

2) embedded Jetty tutorial

Http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty

Use Maven to configure embedded Jetty

Maven usage conventions take precedence over configuration, so it is best to use the Maven project structure, as recommended by Maven. You can use Archetypes to quickly set the Maven project, but in this tutorial, We will manually set the structure:

Mkdir JettyMavenHelloWorld

Cd JettyMavenHelloWorld

Mkdir-p src/main/java/org/example

Create a HelloWorld class

Use the editor to create a file src/main/java/org/example/HelloWorld. java with the following content:

Package org. example;

Importjavax. servlet. http. HttpServletRequest;

Importjavax. servlet. http. HttpServletResponse;

Import javax. servlet. ServletException;

Import java. io. IOException;

Import org. eclipse. jetty. server. Server;

Import org. eclipse. jetty. server. Request;

Importorg. eclipse. jetty. server. handler. AbstractHandler;

Public class HelloWorld extendsimplements acthandler

{

Public void handle (String target,

Request baseRequest,

HttpServletRequestrequest,

HttpServletResponseresponse)

Throws IOException, ServletException

{

Response. setContentType ("text/html; charset = UTF-8 ");

Response. setStatus (HttpServletResponse. SC _ OK );

BaseRequest. setHandled (true );

Response. getWriter (). println ("HelloWorld ");

}

Public static void main (String [] args) throws Exception

{

Server server = new Server (8080 );

Server. setHandler (new HelloWorld ());

Server. start ();

Server. join ();

}

}

Create POM description

Pom. xml declares the project name and its dependencies. Use the editor to create a pom. xml file with the following content:

Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"

Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0http://maven.apache.org/maven-v4_0_0.xsd">

4.0.0

Org. example

Hello-world

0.1-SNAPSHOT

Jar

Jetty HelloWorld

9.0.2.v20130417

Org. eclipse. jetty

Jetty-server

$ {JettyVersion}

Org. mortbay. jetty

Jetty-maven-plugin

$ {JettyVersion}

Org. codehaus. mojo

Exec-maven-plugin

1.1

Java

Org. example. HelloWorld

Note: The following classes can be found using 9.0.2.v20130417:

Importorg. eclipse. jetty. server. handler. AbstractHandler;

However, the latest Jetty version 9.2.3.v20140905 cannot be used to import this class.

Build and run embedded HelloWorld

Now you can use the following command to compile and execute the HelloWorld class.

Mvn clean compile exec: java

You can open http: // localhost: 8080 in the browser to view the Hello world page. You can use the mvndependency: tree Command to view what Maven has done for you. It exposes the dependencies between parsing and downloading, as follows:

> Mvn dependency: tree

[INFO] Scanning for projects...

[INFO] Searching repository for plugin withprefix: 'dependency '.

[INFO] ------------------------------------------------------------------------

[INFO] Building Jetty HelloWorld

[INFO] task-segment: [dependency: tree]

[INFO] ------------------------------------------------------------------------

[INFO] [dependency: tree {execution: default-cli}]

[INFO] org. example: hello-world: jar: 0.1-SNAPSHOT

[INFO] \-org. eclipse. jetty: jetty-server: jar: 7.0.1.v20091125: compile

[INFO] +-javax. servlet: servlet-api: jar: 2.5: compile

[INFO] +-org. eclipse. jetty: jetty-continuation: jar: 7.0.1.v20091125: compile

[INFO] \-org. eclipse. jetty: jetty-http: jar: 7.0.1.v20091125: compile

[INFO] \-org. eclipse. jetty: jetty-io: jar: 7.0.1.v20091125: compile

[INFO] \-org. eclipse. jetty: jetty-util: jar: 7.0.1.v20091125: compile

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESSFUL

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 4 seconds

[INFO] Finished at: Tue Feb 16 16:19:08 EST2010

[INFO] Final Memory: 11 M/68 M

[INFO] ------------------------------------------------------------------------

Use Jetty and Maven to develop standard webapps

The preceding example shows how to use the embedded Jetty processor to run the Hello world example. The following example shows how to use Maven and Jetty to develop a standard WebApp. First, create the Maven structure.

Mkdir JettyMavenHelloWarApp

Cd JettyMavenHelloWarApp

Mkdir-p src/main/java/org/example

Mkdir-p src/main/webapp/WEB-INF

Create static content:

A Web application can contain static content. Therefore, the src/main/webapp/index.html file is created with the following content:

Hello World Webapp

HelloServlet

Create a Servlet

Use the editor to create src/main/java/org/example/HelloServlet. java. The content is as follows:

Package org. example;

Import java. io. IOException;

Import javax. servlet. ServletException;

Import javax. servlet. http. HttpServlet;

Importjavax. servlet. http. HttpServletRequest;

Import javax. servlet. http. HttpServletResponse;

Public class HelloServlet extendsHttpServlet

{

Protected void doGet (HttpServletRequest request, HttpServletResponseresponse) throws ServletException, IOException

{

Response. setContentType ("text/html ");

Response. setStatus (HttpServletResponse. SC _ OK );

Response. getWriter (). println ("HelloServlet ");

Response. getWriter (). println ("session =" + request. getSession (true). getId ());

}

}

The Servlet needs to be declared in the deployment description, so compile src/main/webapp/WEB-INF/web. xml and add the following content.

Xmlns = "http://java.sun.com/xml/ns/javaee"

Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"

Xsi: schemaLocation = "http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

Version = "2.5">

Hello

Org. example. HelloServlet

Hello

Http://blog.csdn.net/hello /*

Build and run a Web application:

Copy pom. xml and run the following command to build and run the Web application.

Mvn jetty: run

In http: // localhost: 8080 bytes. The content path is part of the url (hello-world) and is from the manual ID in the pom. xml file.

Create a WAR file:

You can create a WebApplication Archive (WAR) file for the project by running the following command:

Mvn package

The generated war file is located in the target directory. You can deploy it to the standard servlet service or Jetty.

Enable SSL

The process of enabling SSL in Embedded Jetty 9 is as follows:

1) First, create an HttpConfiguration using the HTTP configuration that explains the security solution and port.

HttpConfiguration http_config = newHttpConfiguration ();

Http_config.setSecureScheme ("https ");

Http_config.setSecurePort (8443 );

Then, we create another HttpConfiguration for the https extended from the above configuration, but add a securerequestmimizer.

HttpConfiguration https_config = newHttpConfiguration (http_config );

Https_config.addCustomizer (newSecureRequestCustomizer ());

3) create an SslContexFactory pointing to your JavaKeystore file.

SslContextFactory sslContextFactory = newSslContextFactory ("/its_dir/cert. keystore ");

SslContextFactory. setKeyStorePassword ("password ");

Note: You can use the OBF prefix password if you plan to use the Jetty fuzzy password.

4) Next we will create a ServerConnector and pass it to its Server class, SslConnectorFactory and HttpConnectionFactory. As follows:

Serverconnehtthttpsconne= newserverconne( server,

New SslConnectionFactory (sslContextFactory, "http/1.1 "),

New HttpConnectionFactory (https_config ));

Httpsconnetor. setPort (8443 );

HttpsConnector. setIdleTimeout (50000 );

Finally, use this connection to the Service, which is the same as the normal HTPP ServerConnector.

Server. setConnectors (new Connector [] {httpsconne });

Used with Jersey

Official Website: https://jersey.java.net/

Document URL: https://jersey.java.net/documentation/latest/index.html

Download URL:

You can mix two Jersey versions in the Code, from Jersey2.x (org. glassfish. servletContainer in jersey * package) and from Jersey 1.x( package prefix com. sum. jersey..

To use Jersey2.x to develop your application, modify the following two lines:

H. setInitParameter ("com. sun. jersey. config. property. resourceConfigClass", "com. sun. jersey. api. core. PackagesResourceConfig ");

H. setInitParameter ("com. sun. jersey. config. property. packages", "resources ");

From the main method to the following function:

H. setInitParameter (ServerProperties. PROVIDER_PACKAGES, "resources ");

Check other ServerProperties, and you may find useful ones.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.