Jetty Hello World

Source: Internet
Author: User

URL: http://wiki.eclipse.org/Jetty/Tutorial/Jetty_HelloWorld

This section describes how to use the jetty API provided by the jetty class under classpath to develop code. If you want to use Maven or a standard web application, refer to the jetty and mavenhelloworld tutorials.

Download the jar package

Jetty is divided into many jar and dependencies, and the minimum memory usage is obtained by selecting the smallest jar set. It is usually best to use Maven to manage jar packages. (Refer to the jetty and Maven helloworld tutorials ). However, in this tutorial, we use a centralized jar package to include all jetty classes in a jar package. Wget:

Mkdir demo

CD demo

Jetty_version = 9.2.3.v20140905
Wget-u none accept

Wget-u nonehttp: // repo1.maven.org/maven2/javax/servlet/servlet-api/2.5/servlet-api-2.5.jar

Compile a helloworld example

The embedded jetty tutorial contains many examples written using jettyapi. In this tutorial, we will use a simple helloworld processor in the main method to run the service. In the compiler or IDE, edit the helloworld. Java file and add the following content:

Package CN. uway. jetty;

 

Import java. Io. ioexception;

 

Import javax. servlet. servletexception;

Import javax. servlet. http. httpservletrequest;

Importjavax. servlet. http. httpservletresponse;

 

Import org. Eclipse. Jetty. server. request;

Import org. Eclipse. Jetty. server. server;

Importorg. Eclipse. Jetty. server. handler. abstracthandler;

 

Public class helloworld extendsimplements acthandler {

 

@ Override

Publicvoid handle (string target, request baserequest, httpservletrequest request,

Httpservletresponseresponse) throws ioexception, servletexception {

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

Response. setstatus (httpservletresponse. SC _ OK );

Baserequest. sethandled (true );

Response. getwriter (). println ("

}

Publicstatic void main (string [] ARGs) throws exception {

Serverserver = new server (8080 );

Server. sethandler (newhelloworld ());

Server. Start ();

Server. Join ();

}

}

Compile helloworld example

The following command compiles the helloworld class:

Java-cpservlet-api-2.5.jar: jetty-all-$ jetty_version.jar helloworld. Java

Run processors and services

The following command runs helloworld as an example:

Java-CP.: servlet-api-2.5.jar: jetty-all-$ jetty_version.jar helloworld

You can open http: // localhost: 8080 in the browser to view the hello World page.

Next step

1) learn more about jetty/tutorial/embeddinjetty

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

2) browse the complete jettyjavadoc.

Http://download.eclipse.org/jetty/stable-7/apidocs/

Http://download.eclipse.org/jetty/stable-8/apidocs/

Http://download.eclipse.org/jetty/stable-9/apidocs/

3) Consider using jetty and menven to manage your jar and dependencies.

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

4) learn other options: "How to Use jetty for development ".

Http://wiki.eclipse.org/Jetty/Howto/Develop

Http://wiki.eclipse.org/Jetty


Jetty Hello World

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.