This article link: http://blog.csdn.net/kongxx/article/details/7227107
1. First modify the Pom.xml file to add spring dependencies
<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.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>com.google.code.garbagecan.jettystudy</groupId> < Artifactid>jettystudy</artifactid> <packaging>jar</packaging> <version>1.0-snapshot
</version> <name>jettystudy</name> <url>http://maven.apache.org</url> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <inherited>true
</inherited> <configuration> <source>1.6</source> <target>1.6</target> <debug>true</debug> </configuration> </plugin> </plugins> </build> < Dependencies> <dependency> <groupid>org.eclipse.jetTy.aggregate</groupid> <artifactId>jetty-all</artifactId> <version>8.0.4.v20111024</ version> <type>jar</type> <scope>provided</scope> </dependency> <dependen cy> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version
>2.5.6</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version& gt;3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project >
2. Create a server class to start Jetty server with spring
Package com.google.code.garbagecan.jettystudy.sample4;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyServer {public
static void Main (string[] args) throws Exception {
new Classpathxmlapplicationcont Ext ("/com/google/code/garbagecan/jettystudy/sample4/spring.xml");
}
3. Create a handler class that handles HTTP requests
package com.google.code.garbagecan.jettystudy.sample4;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.eclipse.jetty.server.Request;
Import Org.eclipse.jetty.server.handler.AbstractHandler; public class MyHandler extends Abstracthandler {public void handle (String target, Request baserequest, httpservletreques T request, httpservletresponse response) throws IOException, servletexception {response.setcontenttype ("Text/html;ch
Arset=utf-8 ");
Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);
Baserequest.sethandled (TRUE);
Response.getwriter (). println ("
4. Create a spring configuration file and place it in the Com/google/code/garbagecan/jettystudy/sample4/spring.xml location, as follows.
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd "> <bean id=" Server "class=" Org.eclipse.jetty.server.Server "init-method=" "Start" destroy-method= "Stop" > <property name= "Connectors" > & lt;list> <bean id= "Connector" class= "Org.eclipse.jetty.server.nio.SelectChannelConnector" > <property
Name= "Port" value= "8080"/> </bean> </list> </property> <property name= "Handler" >
<bean id= "Handlers" class= "Org.eclipse.jetty.server.handler.HandlerList" > <property name= "Handlers" > <list> <bean class= "Com.google.code.garbagecan.jettystudy.sample4.MyHandler"/> <bean class= " Org.eclipse.jetty.server.handler.DefaultHandler "/> </list> </property> </bean> </property> </bean> </beans>
This defines the configuration of the jetty server, including connector and handler, and so on.
5. Run the MyServer class and then access it through http://localhost:8080/.