Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Using Filters and Servlets with Embedded Jetty

Header Image

Ahoy there! If ye be sailin’ the seas of web development with Embedded Jetty, ye might be wonderin’ how to use Filters and Servlets to navigate the treacherous waters of user input and data processing. Fear not, for I shall guide ye on this adventure! In this article, we’ll be exploring how to use Filters and Servlets with Embedded Jetty, including adding them to the server and configuring them. So hoist the anchor and let’s set sail!

Adding Filters and Servlets to Embedded Jetty

Before we dive into the specifics of Filters and Servlets, let’s take a moment to talk about how to add them to your Embedded Jetty server. First, you’ll need to create a new ServletContextHandler and set it as the server’s handler. This context will be where you add your Filters and Servlets. Here’s an example of how to do it:

Server server = new Server(8080);
ServletContextHandler contextHandler = new ServletContextHandler(server, "/");
server.setHandler(contextHandler);

Now that you’ve got your ServletContextHandler set up, you can add your Filters and Servlets to it. You can add Filters and Servlets individually, or you can add them using a ServletHandler. Here’s an example of how to add a Filter and a Servlet individually:

contextHandler.addFilter(MyFilter.class, "/myFilter/*", EnumSet.of(DispatcherType.REQUEST));
contextHandler.addServlet(MyServlet.class, "/myServlet");

And here’s an example of how to add them using a ServletHandler:

ServletHandler servletHandler = new ServletHandler();
servletHandler.addServletWithMapping(MyServlet.class, "/myServlet");
servletHandler.addFilterWithMapping(MyFilter.class, "/myFilter/*", EnumSet.of(DispatcherType.REQUEST));
contextHandler.setServletHandler(servletHandler);

Configuring Filters and Servlets

Now that you’ve got your Filters and Servlets added to your Embedded Jetty server, you’ll need to configure them. This can include setting up initialization parameters, specifying URL patterns, and more. Let’s take a look at how to configure Filters and Servlets in Embedded Jetty.

Configuring Filters

Filters are used to intercept requests before they reach the Servlet. This can be useful for things like authentication, logging, or modifying request parameters. To configure a Filter, you’ll need to implement the Filter interface and override the doFilter method. Here’s an example of a simple Filter that logs all incoming requests:

public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Initialization code here
    }
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        System.out.println("Incoming request: " + httpRequest.getMethod() + " " + httpRequest.getRequestURI());
        chain.doFilter(request, response);
    }
    
    @Override
    public void destroy() {
        // Cleanup code here
    }
}

Once you’ve got your Filter implemented, you can configure it by specifying the URL pattern and any initialization parameters. Here’s an example of how to configure the MyFilter we just created:

contextHandler.addFilter(MyFilter.class, "/myFilter/*", EnumSet.of(DispatcherType.REQUEST));

Configuring Servlets

Servlets are used to handle requests and generate responses. They’re the backbone of any Java web application. To configure a Servlet, you’ll need to implement the Servlet interface and overridethe service method. Here’s an example of a simple Servlet that responds with a “Hello, world!” message:

public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/plain");
        response.getWriter().write("Hello, world!");
    }
}

Once you’ve got your Servlet implemented, you can configure it by specifying the URL pattern. Here’s an example of how to configure the MyServlet we just created:

contextHandler.addServlet(MyServlet.class, "/myServlet");

Conclusion

And there you have it, matey! Ye now know how to use Filters and Servlets with Embedded Jetty. By adding Filters and Servlets to yer server, ye can handle requests, intercept data, and provide a customized experience for yer users. So set sail on yer next web development adventure with confidence, knowing that ye can navigate the waters of Embedded Jetty with ease. If ye be lookin’ for more information on Embedded Jetty, be sure to check out the official documentation and online communities. And remember, keep yer code shipshape and yer puns sharp!