jetty 8.0 add filter example

时间:2021-06-06 06:47:52

http://zyn010101.iteye.com/blog/1679798

package com.cicc.gaf.sso.server;
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 org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.cicc.gaf.sso.server.SSOSyncFilter.HelloPrintingFilter;

public class OneServletContext {

/**
* @param args
* Administrator
* 2012-9-12 下午1:12:10
*/
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
context.addServlet(new ServletHolder(new HelloServlet()), "/*");
context.addServlet(new ServletHolder(new HelloServlet("Buongiorno Mondo")), "/it/*");
context.addServlet(new ServletHolder(new HelloServlet("Bonjour le monde")), "/fr/*");

context.addFilter(HelloPrintingFilter.class, "/*",EnumSet.of(DispatcherType.REQUEST));

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

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");
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig arg0) throws ServletException {

}

@Override
public void destroy() {}
}
}