假设我使用以下代码运行嵌入式jetty。我想将一些参数传递给我的过滤器。当我查看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() {}
}
}
答案 0 :(得分:11)
假设您正在使用此版本(或更高版本)的Jetty ServletHandler
,addFilterWithMapping(..)
方法返回FilterHolder
,它提供setInitParameter(..)
方法来设置您的init参数
答案 1 :(得分:1)
总结一下 - 它看起来像这样:
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));
}
}
}
}