1.Jetty嵌入到Spring项目
try {
Server server = new Server(8080);
WebAppContext context = new WebAppContext();
context.setContextPath("/test");
context.setDescriptor(filePath + "/conf/web.xml"); // 指定web.xml配置文件
context.setResourceBase(filePath + "/conf/");// 指定webapp目录
context.setParentLoaderPriority(true);
server.setHandler(context);
server.start();
server.join();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
2.Jetty添加Filter的方式有两种
1.类似Tomcat,在web.xml中指定过滤器,但需要确定配置文件WEB-INF/web.xml 存在于webapp根目录
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<filter>
<filter-name>filterName</filter-name>
<filter-class>com.x.y.z.FilterClass</filter-class>
</filter>
<filter-mapping>
<url-pattern>/test</url-pattern>
<filter-name>filterName</filter-name>
</filter-mapping>
</web-app>
2.直接用addFilter API
主要用到 ServletContextHandler、
FilterMapping、
FilterHolder几个类
import java.io.IOException;
import java.util.EnumSet;
import java.util.Enumeration;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
public class TestEmbeddedFilter {
public static void main(String[] args) {
Server server = new Server(8082);
ServletContextHandler context = new ServletContextHandler(
ServletContextHandler.SESSIONS);
context.setContextPath("/");
ServletHandler handler = new ServletHandler();
ServletHolder sh = new ServletHolder(new MyServlet());
context.addServlet(sh, "/*");
FilterHolder fh = handler.addFilterWithMapping(MyFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
fh.setInitParameter("initParamKey", "InitParamValue");
context.addFilter(fh, "/*", EnumSet.of(DispatcherType.REQUEST));
context.setHandler(handler);
server.setHandler(context);
try {
server.start();
// server.join();
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("serial")
public static class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello MyServlet</h1>");
}
}
public static class MyFilter implements Filter {
public void destroy() {
System.out.println("Stopping filter");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("doFilter called with: " + request);
chain.doFilter(request, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
Enumeration<String> enums = filterConfig.getInitParameterNames();
while (enums.hasMoreElements()) {
String param = (String) enums.nextElement();
System.out.println(param + ":" + filterConfig.getInitParameter(param));
}
}
}
}
可参考:
addFilter使用方法
http://blog.csdn.net/sidyhe/article/details/52084558
http://stackoverflow.com/questions/14390577/how-to-add-servlet-filter-with-embedded-jetty
官方实例
http://alvinalexander.com/java/jwarehouse/jetty-6.1.9/modules/util/src/main/java/org/mortbay/servlet/GzipFilter.java.shtml
如果让某些请求绕过Filter
http://blog.csdn.net/moonsheep_liu/article/details/6862921