import java.util.HashMap;
import java.util.Map;import org.apache.jasper.servlet.JspServlet;
import org.mortbay.jetty.Connector;import org.mortbay.jetty.Server;import org.mortbay.jetty.bio.SocketConnector;import org.mortbay.jetty.servlet.Context;import org.mortbay.jetty.servlet.DefaultServlet;import org.mortbay.jetty.servlet.FilterHolder;import org.mortbay.jetty.servlet.ServletHolder;import org.springframework.js.resource.ResourceServlet;import org.springframework.web.context.ContextLoaderListener;import org.springframework.web.filter.DelegatingFilterProxy;import org.springframework.web.servlet.DispatcherServlet;/**
* 在eclipse中启动 * ertuo */public class JettyWebStarter {
/** * 服务器启动。 */ public void start() { Server server = new Server(); Connector connector = new SocketConnector(); connector.setPort(8080); server.addConnector(connector);Context root = new Context(server, "/booking-mvc", Context.SESSIONS);
/**启动的时候就会加载这个listener 这里开始建立spring的application content*/ ContextLoaderListener listener = new ContextLoaderListener(); /**设置spring配置文件位置*/ Map<String, String> initParams = new HashMap<String, String>(); initParams.put("contextConfigLocation", "/WEB-INF/config/web-application-config.xml"); root.setInitParams(initParams); /**servlet context*/ root.setResourceBase("E:/sources/spring/spring-webflow-2.0.5.RELEASE/projects/spring-webflow-samples/booking-mvc/src/main/webapp"); root.addEventListener(listener); /**资源文件解析*/ ServletHolder holder=new ServletHolder(new ResourceServlet()); root.addServlet(holder, "/resources/*"); /**spring control*/ holder = new ServletHolder(new DispatcherServlet()); holder.setInitParameter("contextConfigLocation", "/WEB-INF/config/web-application-config.xml"); root.addServlet(holder, "/spring/*"); /**jsp 解析servlet*/ holder=new ServletHolder(new JspServlet()); root.addServlet(holder, "*.jsp"); /**html*/ holder=new ServletHolder(new DefaultServlet()); root.addServlet(holder, "*.html"); /**welcome file 这里没有生效 不知道为什么*/ root.setWelcomeFiles(new String[]{"index.html"}); postStart(root); try { server.start(); server.join(); } catch (Exception e) { throw new RuntimeException(e); } } private void postStart(Context root){ /**spring内部过滤器代理 里面包含了默认的11个过滤器 这里的初始化参数可以直接些spring的bean名称*/ FilterHolder filterHolder=new FilterHolder(DelegatingFilterProxy.class); filterHolder.setName("springSecurityFilterChain"); root.addFilter(filterHolder, "/*", org.mortbay.jetty.Handler.DEFAULT); }}http://nbaertuo.iteye.com/blog/366847