职业教育新东方网 > 职业教育 > 计算机 > Java认证 > 备考辅导 > 公共基础知识 > 文章正文

嵌入式Jetty集成Spring运行

分享到:

2012-02-03 09:57  作者:  来源:互联网  字号:T|T

  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

   junit junit 3.8.1 test

2. 创建一个Server类,用来通过spring来启动Jetty server [java] view plaincopy 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 ClassPathXmlApplicationContext("/com/google/code/garbagecan/jettystudy/sample4/spring.xml");} 3. 创建一个Handler类,用了处理http请求[java] view plaincopy 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, 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("

Hello World

");response.getWriter()。println("

 

  [java] view plaincopy <?xml version="1.0" encoding="UTF-8"?>

  

其中定义了Jetty Server的配置,包括Connector和Handler等等。

 

 

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

 

《《《我要报班:  点击进入

(责任编辑:单月)

 

热点课讯

精彩问答

提问