Jetty添加Filter过滤器

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根目录






    
        filterName
        com.x.y.z.FilterClass
    
    
        /test
        filterName
    

  

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("

Hello MyServlet

"); } } 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 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

转载于:https://www.cnblogs.com/kxdblog/p/6368836.html

你可能感兴趣的:(Jetty添加Filter过滤器)