Jetty and Maven helloworld

Source: Internet
Author: User

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 ("

}

 

Public static void main (string [] ARGs) throws exception

{

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: <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.0http://maven.apache.org/maven-v4_0_0.xsd">

 

<Modelversion> 4.0.0 </modelversion>

<Groupid> org. Example </groupid>

<Artifactid> hello-world </artifactid>

<Version> 0.1-Snapshot </version>

<Packaging> jar </packaging>

<Name> jetty helloworld </Name>

 

<Properties>

<Jettyversion> 9.0.2.v20130417 </jettyversion>

</Properties>

 

<Dependencies>

<Dependency>

<Groupid> org. Eclipse. jetty </groupid>

<Artifactid> jetty-server </artifactid>

<Version >$ {jettyversion} </version>

</Dependency>

</Dependencies>

 

<Build>

<Plugins>

<Plugin>

<! -- This plugin is needed for the servlet example -->

<Groupid> org. mortbay. jetty </groupid>

<Artifactid> jetty-Maven-plugin </artifactid>

<Version >$ {jettyversion} </version>

</Plugin>

<Plugin>

<Groupid> org. codehaus. Mojo </groupid>

<Artifactid> Exec-Maven-plugin </artifactid>

<Version> 1.1 </version>

<Executions>

<Execution> <goals> <goal> JAVA </goal> </goals> </execution>

</Executions>

<Configuration>

<Mainclass> org. example. helloworld </mainclass>

</Configuration>

</Plugin>

</Plugins>

</Build>

</Project>

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:

<H1> Hello World webapp

<A href = "/Hello"> helloservlet </a>

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 ("

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.

<? XML version = "1.0" encoding = "ISO-8859-1"?>

<Web-app

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">

<Servlet>

<Servlet-Name> Hello </servlet-Name>

<Servlet-class> org. example. helloservlet </servlet-class>

</Servlet>

<Servlet-mapping>

<Servlet-Name> Hello </servlet-Name>

<URL-pattern>/Hello/* </url-pattern>

</Servlet-mapping>

</Web-app>

Build and run a web application:

Copy Pom. xml and run the following command to build and run the web application.

MVN jetty: Run

You can view static and dynamic content in http: // localhost: 8080/Hello-World/hello. 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 = 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.

Jetty and Maven helloworld

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.