Jetty practice-Integration of embedded jetty With Spring

Source: Internet
Author: User

Link: http://blog.csdn.net/kongxx/article/details/7227107

1. First modify the Pom. xml file and add spring dependencies.

<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> </P> <p> <dependency> <br/> <groupid> org. springframework </groupid> <br/> <artifactid> spring </artifactid> <br/> <version> 2.5.6 </version> <br/> <type> jar </Type> <br/> <scope> provided </scope> <br/> </dependency> </P> <p> <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>2. Create a server class to start jetty server through spring

Package COM. google. code. garbagecan. jettystudy. sample4; </P> <p> Import Org. springframework. context. support. classpathxmlapplicationcontext; </P> <p> public class myserver {<br/> Public static void main (string [] ARGs) throws exception {<br/> New classpathxmlapplicationcontext ("/COM/Google/code/garbagecan/jettystudy/sample4/spring. XML "); <br/>}< br/>}3. Create a handler class and use it to process HTTP requests.

Package COM. google. code. garbagecan. jettystudy. sample4; </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 myhandler 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 ("<li> request URL:" + target + "</LI>"); <br/> response. getwriter (). println ("<li> server port:" + request. getserverport () + "</LI>"); <br/>}< br/>}4. Create a spring configuration file and place it in the COM/Google/code/garbagecan/jettystudy/sample4/spring. xml location. The content is as follows.
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <beans xmlns = "http://www.springframework.org/schema/beans" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> </P> <p> <bean id = "server" class = "org. eclipse. jetty. server. server "init-method =" start "Destroy-method =" stop "> <br/> <property name =" connectors "> <br/> <list> <br/> <bean id = "connector" class = "org. eclipse. jetty. server. NIO. selectchannelconnector "> <br/> <property name =" Port "value =" 8080 "/> <br/> </bean> <br/> </List> <br/> </property> </P> <p> <property name = "handler"> <br/> <bean id = "handlers" class = "org. eclipse. jetty. server. handler. handlerlist "> <br/> <property name =" handlers "> <br/> <list> <br/> <Bean class =" com. google. code. garbagecan. jettystudy. sample4.myhandler "/> <br/> <Bean class =" org. eclipse. jetty. server. handler. defaulthandler "/> <br/> </List> <br/> </property> <br/> </bean> <br/> </property> <br/> </bean> <br/> </beans>The jetty server configuration is defined, including ctor and handler.
5. Run the myserver class and access it through http: // localhost: 8080.

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.