嵌入式 Jetty 是不是能够设置过滤器的 init-params?

Posted

技术标签:

【中文标题】嵌入式 Jetty 是不是能够设置过滤器的 init-params?【英文标题】:Does embedded Jetty have the ability to set the init-params of a filter?嵌入式 Jetty 是否能够设置过滤器的 init-params? 【发布时间】:2014-04-06 22:51:01 【问题描述】:

假设我正在使用以下代码运行嵌入式码头。我想将一些参数传递给我的过滤器。当我查看 the Jetty API 添加过滤器时 - 没有设置 init-params 的选项。我的问题是嵌入式 Jetty 是否能够设置过滤器的 init-params?

import java.io.IOException;
import java.util.EnumSet;

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.ServletHandler;

public class JettyFilter 

  public static void main(final String[] args) throws Exception 
    Server server = new Server(8080);

    ServletHandler handler = new ServletHandler();
    server.setHandler(handler);

    handler.addServletWithMapping(HelloServlet.class, "/*");
    handler.addFilterWithMapping(HelloPrintingFilter.class, "/*",
        EnumSet.of(DispatcherType.REQUEST));

    server.start();
    server.join();
  

  public static class HelloServlet extends HttpServlet 
    private static final long serialVersionUID = 1L;

    @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 SimpleServlet</h1>");
    
  

  public static class HelloPrintingFilter implements Filter 
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException 
      System.out.print("hello from filter");
    

    @Override
    public void init(FilterConfig arg0) throws ServletException 

    

    @Override
    public void destroy() 
  

【问题讨论】:

【参考方案1】:

假设您使用的是 Jetty 的 ServletHandler 这个版本(或更高版本),addFilterWithMapping(..) 方法会返回一个 FilterHolder,它提供了一个 setInitParameter(..) 方法来设置您的 init 参数。

【讨论】:

【参考方案2】:

总结一下 - 它看起来像这样:

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

    


【讨论】:

以上是关于嵌入式 Jetty 是不是能够设置过滤器的 init-params?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用嵌入式码头添加 servlet 过滤器

Jetty添加Filter过滤器

嵌入 Jetty 并在 ServletContext 上设置默认属性

如何在 ODL 控制器中配置拒绝服务过滤器

Jetty vs. Tomcat

Jetty 中的 JSR-356 javax websockets(嵌入式和非嵌入式)