a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 85|回复: 0

[其他] JAVA技巧:嵌入式Jetty集成Spring运行解析

[复制链接]
发表于 2012-8-4 12:28:23 | 显示全部楼层 |阅读模式
1. 首先修改pom.xml文件,添加spring的依赖项[html] view plaincopy  4.0.0 com.google.code.garbagecan.jettystudy jettystudy jar 1.0-SNAPSHOT jettystudy http://maven.apache.org    maven-compiler-plugin true  1.6 1.6 true       org.eclipse.jetty.aggregate jetty-all 8.0.4.v20111024 jar provided     org.springframework spring 2.5.6 jar provided
) `6 }% I! Y; \/ z. a# Y' x   junit junit 3.8.1 test    2. 创建一个Server类,用来通过spring来启动Jetty server [java] view plaincopy package com.google.code.garbagecan.jettystudy.sample4;
. T1 O2 S# Q" Z! e+ Z) v$ @0 a1 o  import org.springframework.context.support.ClassPathXmlApplicationContext;/ b5 H, ]3 p: W: G0 \  Y6 W% |8 `. l  B
  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 com.google.code.garbagecan.jettystudy.sample4;( }" `* g# V. u& E1 s) I. |
  import java.io.IOException;1 I) J6 n7 W& [/ n7 u6 ~
  import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
3 ?# J0 Y- A4 l* V9 V( \% [  import org.eclipse.jetty.server.Request;import org.eclipse.jetty.server.handler.AbstractHandler;
1 ?* e# B$ o" d2 J9 j( q  {  public class MyHandler extends AbstractHandler { public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
' E% F9 J2 d5 M3 L6 r  throws IOException, ServletException { response.setContentType("text/html;charset=utf-8");response.setStatus(HttpServletResponse.SC_OK);baseRequest.setHandled(true);response.getWriter()。println("Hello World
; x" f6 T, a7 n6 w
0 S- t0 f$ T# O4 G");response.getWriter()。println("Request url: " + target + "$ [4 R' x! }  W5 e; |
");response.getWriter()。println("Server port: " + request.getServerPort() + ": T* v4 z+ \+ g/ p# c. @5 K9 K
");} 4. 创建一个spring配置文件,并放在com/google/code/garbagecan/jettystudy/sample4/spring.xml位置,内容如下。2 y. `$ |- d: Y8 A
  [java] view plaincopy  , K/ S+ X( b8 Y, N. l8 v' h0 c( n( J
         9 T8 ]2 @+ g: x. y& N: I; a
             其中定义了Jetty Server的配置,包括Connector和Handler等等。$ \, o0 S9 u3 S* f' O  n
  5. 运行MyServer类,然后通过http://localhost:8080/来访问。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-21 21:06 , Processed in 0.219679 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表