嵌入式Jetty集成Spring运行
嵌入式Jetty集成Spring运行
1. 首先修改pom.xml文件,添加spring的依赖项[html] view plaincopy
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("
Hello World
");response.getWriter()。println("
[java] view plaincopy <?xml version="1.0" encoding="UTF-8"?>
5. 运行MyServer类,然后通过localhost:8080/来访问
銆€
銆€