a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 123|回复: 0

[专业语言] JAVA认证基础知识:嵌入式Jetty集成Spring运行

[复制链接]
发表于 2012-8-4 12:44:44 | 显示全部楼层 |阅读模式
 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 ) A, y3 b( o/ g' u
   org.springframework spring 2.5.6 jar provided . R4 P( V9 @1 m0 q4 e
   junit junit 3.8.1 test    2. 创建一个Server类,用来通过spring来启动Jetty server [java] view plaincopy package com.google.code.garbagecan.jettystudy.sample4;$ u6 b' c/ ]6 r8 E' v
  import org.springframework.context.support.ClassPathXmlApplicationContext;
& z' X1 Q9 |" i. e; E, m5 k+ [5 ]9 v  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;
% j7 l0 u8 X# I0 X  import java.io.IOException;( r" W, |! F$ t3 _6 Y' ?
  import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
0 f! u: z! R' m5 y: K  import org.eclipse.jetty.server.Request;import org.eclipse.jetty.server.handler.AbstractHandler;
6 b3 @7 G$ C" I2 N  K  public class MyHandler extends AbstractHandler { public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
' [! R7 P5 ~0 F6 K8 A* f  throws IOException, ServletException { response.setContentType("text/html;charset=utf- 8");response.setStatus(HttpServletResponse.SC_OK);baseRequest.setHandled(true);response.getWriter()。 println("Hello World2 [4 O# v7 @) L( @

  a& [8 p" O5 K. a, O  r");response.getWriter()。println("Request url: " + target + "& d) `$ A# c7 D  a$ M
");response.getWriter()。println("Server port: " + request.getServerPort() + ") d4 Y# K" ?1 R5 s
");} 4. 创建一个spring配置文件,并放在com/google/code/garbagecan/jettystudy/sample4 /spring.xml位置,内容如下。外语学习网! z2 C7 u. K" L" e+ p9 Y0 b
  [java] view plaincopy  % T1 |$ q, {9 f4 z6 I
        
" _6 L6 h4 C$ p! S) n             其中定义了Jetty Server的配置,包括Connector和Handler等等。0 C- [: h( k2 c3 x
  5. 运行MyServer类,然后通过http://localhost:8080/来访问。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-4 03:43 , Processed in 0.155723 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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