a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 125|回复: 0

[专业语言] JAVA认证考试专业语言基础知识17

[复制链接]
发表于 2012-8-4 12:44:44 | 显示全部楼层 |阅读模式
首先修改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 3 ]( r% J0 J( x, L7 E2 j/ S+ i( z9 u
   junit junit 3.8.1 test    2. 创建一个Server类,用来通过spring来启动Jetty server [java] view plaincopy package com.google.code.garbagecan.jettystudy.sample4;$ g7 C2 O, F# K2 T: ~4 ]0 }
  import org.springframework.context.support.ClassPathXmlApplicationContext;; c/ y! X# ~+ \$ F+ ^3 X, U
  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;' ^" n5 a+ @1 G" v% S4 _
  import java.io.IOException;
9 a% z; G# o. w  import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
' p" F9 B; ?+ R" S! q8 C9 r& Q, ?  import org.eclipse.jetty.server.Request;import org.eclipse.jetty.server.handler.AbstractHandler;
0 Q. L1 ]$ V- O4 T  public class MyHandler extends AbstractHandler { public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)4 X. V5 \! R* b7 ^+ c
  throws IOException, ServletException { response.setContentType("text/html;charset=utf-8");response.setStatus(HttpServletResponse.SC_OK);baseRequest.setHandled(true);response.getWriter()。println("Hello World
) [/ _) p1 M  ~& l3 Q1 |% m8 Z( A/ ?
: e0 d1 z5 X# _5 v");response.getWriter()。println("Request url: " + target + "
5 l/ C7 d) v! z* ~; _0 J5 L8 h) F");response.getWriter()。println("Server port: " + request.getServerPort() + "0 D& D+ N7 |# `/ j& v6 m
");} 4. 创建一个spring配置文件,并放在com/google/code/garbagecan/jettystudy/sample4/spring.xml位置,内容如下。
' h0 f1 J9 R* ]% Y* Z' m  [java] view plaincopy  
. O3 |6 @- z4 e3 r) H* @         % I) Y. r- D; s  v" H
             其中定义了Jetty Server的配置,包括Connector和Handler等等。5 d5 h9 F7 r& J1 S* J6 Z
  5. 运行MyServer类,然后通过http://localhost:8080/来访问
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 23:35 , Processed in 0.250394 second(s), 21 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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