Jetty practices-embedded running Jetty

Source: Internet
Author: User

Address: http://blog.csdn.net/kongxx/article/details/7218776

Jetty is most commonly used to embed jetty into its own Java application.ProgramAt this time, Jetty runs as a servlet container in the background and accepts users' HTTP requests. The following is the simplest method to embed jetty.

1. First create a Java project using Maven

MVN archetype: generate-dgroupid = com. Google. Code. garbagecan. jettystudy-dartifactid = jettystudy-darchetypeartifactid = Maven-Archetype-Quickstart-dinteractivemode = false2. Modify the POM file and add or modify the compilation and dependency sections.

<Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <br/> <modelversion> 4.0.0 </modelversion> <br/> <groupid> COM. google. code. garbagecan. jettystudy </groupid> <br/> <artifactid> jettystudy </artifactid> <br/> <packaging> jar </packaging> <br/> <version> 1.0-Snapshot </version> <br/> <Name> jettystudy </Name> <br/> <URL> http://maven.apache.org </URL> <br/> <build> <br/> <plugins> <br/> <plugin> <br/> <artifactid> Maven-compiler-plugin </artifactid> <br/> <inherited> true </inherited> <br/> <configuration> <br/> <source> 1.6 </source> <br/> <target> 1.6 </Target> <br/> <debug> true </debug> <br/> </configuration> <br/> </plugin> <br/> </plugins> <br/> </build> <br/> <dependencies> <br /> <dependency> <br/> <groupid> Org. eclipse. jetty. aggregate </groupid> <br/> <artifactid> jetty-all </artifactid> <br/> <version> 8.0.4.v20111024 </version> <br/> <type> jar </type> <br/> <scope> provided </scope> <br/> </dependency> <br/> <groupid> JUnit </ groupid> <br/> <artifactid> JUnit </artifactid> <br/> <version> 3.8.1 </version> <br/> <scope> test </scope> <br /> </dependency> <br/> </dependencies> <br/> </Project>3. Create a server class, start jetty server, and use hellohandler to process the requests sent by the browser;

Package COM. google. code. garbagecan. jettystudy. sample1; </P> <p> Import Org. eclipse. jetty. server. server; </P> <p> public class myserver {<br/> Public static void main (string [] ARGs) throws exception {<br/> server Server = new server (8080); <br/> server. sethandler (New hellohandler (); </P> <p> server. start (); <br/> server. join (); <br/>}< br/>}4. Create a handler class and process all client requests

Package COM. google. code. garbagecan. jettystudy. sample1; </P> <p> Import Java. io. ioexception; </P> <p> Import javax. servlet. servletexception; <br/> Import javax. servlet. HTTP. httpservletrequest; <br/> Import javax. servlet. HTTP. httpservletresponse; </P> <p> Import Org. eclipse. jetty. server. request; <br/> Import Org. eclipse. jetty. server. handler. abstracthandler; </P> <p> public class hellohandler extends acthandler {<br/> Public void handle (string target, request baserequest, httpservletrequest request, httpservletresponse response) <br/> throws ioexception, servletexception {<br/> response. setcontenttype ("text/html; charset = UTF-8"); <br/> response. setstatus (httpservletresponse. SC _ OK); <br/> baserequest. sethandled (true); <br/> response. getwriter (). println ("<p> Hello World </p>"); <br/> response. getwriter (). println ("request URL:" + target); <br/>}< br/>}5. Run the myserver class and access http: // localhost: 8080/in a browser. You can see "Hello World !" And the requested URL.

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.