视窗
loading...
您现在的位置:首页 > IT认证 > JAVA >

嵌入式Jetty集成Spring运行


 1. 首先修改pom.xml文件,添加spring的依赖项[html] view plaincopy <project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/POM/4.0.0 /maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>de.garbagecan.jettystudy</groupId> <artifactId>jettystudy</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>jettystudy</name> <url></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>

  <dependency> <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>3.8.1</version> <scope>test</scope> </dependency> </dependencies> </project> 2. 创建一个Server类,用来通过spring来启动Jetty server [java] view plaincopy package de.garbagecan.jettystudy.sample4;

  import ntext.support.ClassPathXmlApplicationContext;

  public class MyServer { public static void main(String[] args) throws Exception { new ClassPathXmlApplicationContext("/com/google/code/garbagecan/jettystudy/sample4/spring.xml");} 3. 创建一个Handler类,用了处理http请求[java] view plaincopy package de.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, HttpServletRequest request, HttpServletResponse response)

  throws IOException, ServletException { response.setContentType("text/html;charset=utf-8");response.setStatus(HttpServletResponse.SC_OK);baseRequest.setHandled(true);response.getWriter()。println("<h1>Hello World</h1>");response.getWriter()。println("<li>Request url: " + target + "</li>");response.getWriter()。println("<li>Server port: " + request.getServerPort() + "</li>");} 4. 创建一个spring配置文件,并放在com/google/code/garbagecan/jettystudy/sample4/spring.xml位置,内容如下。

  [java] view plaincopy <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="/schema/beans" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="/schema/beans /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"> <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="de.garbagecan.jettystudy.sample4.MyHandler" /> <bean class="org.eclipse.jetty.server.handler.DefaultHandler" /> </list> </property> </bean> </property> </bean> </beans>其中定义了Jetty Server的配置,包括Connector和Handler等等。

  5. 运行MyServer类,然后通过localhost:8080/来访问。

闁靛棌鍋�

闁靛棌鍋�

闁靛棙鍔曢崢銈囨嫻閿濆拑绱i柡鍕閹蜂即鎮ч崼鐔哥秬閻犲洤鐡ㄥΣ鎴﹀Υ閿燂拷
闁哄牜鍓涢悵顖炴焾閵娿儱鐎诲ǎ鍥e墲娴煎懘寮堕妷锔剧埍濞存粌绨肩花浼存嚂閺冨倻绉归柨娑虫嫹閻庣數顢婂ù鍡樻姜閻e本鐣卞ǎ鍥e墲娴煎懘骞嬮幋婊勭拨闁告梹绋掗惇浼村冀閸ャ劍顫栧ǎ鍥e墲娴煎懘鎯冮崟顐㈡瘔濠㈣泛瀚哥槐锟�闁瑰瓨鍨冲⿰鎴犱焊婵犲洤娅㈠ù锝嗙矎閳ь剙鎳愬▓鎴︽偋閸喐缍€闁戒焦銇炵紞鏃堝级閸愯法绀�閻庝絻顫夐崹婊勭椤掑啯绁弶鐐垫櫕濞堟垶绌遍埄鍐х礀濠碘€冲€瑰﹢浣割嚕閸屾繍鍞撮柟瀛樼墬濠€浣圭瑹閸偅缍€闁瑰瓨鐗炵换姘枖閺囨矮绻嗛柟顓у灥椤曨剟宕f繝鍐╊槯闁艰鲸姊婚柈鎾箣閹存粍绮﹂柨娑樻湰濠€鎵博濞嗗海绐楃紒鏂款儏瀹撳棝宕氶悩缁樼彑闁挎冻鎷�閻庣數鎳撶敮顐﹀礆濞戞ê鏁堕悗纭咁潐閸ㄦ粍绂掗浣稿Ы闁规澘绻戦弫顔界濮椻偓閳ь剙鍊哥紞瀣儍閸曨厐褰掓煀椤掑﹦绀�闁艰鲸姊婚柈鎾棘閻熸壆纭€闁挎稒宀搁崑鏍ㄧ閿燂拷:webmaster@jscj.com闁靛棌鍋撻柣顫祷閻︿粙鏁嶉敓锟�4008816886

相关文章

无相关信息
更新时间2024-01-03 11:10:56【至顶部↑】
江南财子®  联系我们 | 邮件: webmaster@jscj.com | 客服热线电话:4008816886 |  婵烇綀顕ф慨鐐碘偓骞垮灪濠€鍥ь嚗椤旇绻�

付款方式留言簿投诉中心网站纠错二维码手机版

电话:
付款方式   |   给我留言   |   我要纠错   |   联系我们