Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:41:20+00:00 2026-06-17T12:41:20+00:00

I am embedding jetty into my app, and trying to work out how to

  • 0

I am embedding jetty into my app, and trying to work out how to add servlet filters (for cookie handling). The wiki and the javadoc’s dont make it very clear, what am I missing:

Server server = new Server(port);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
FilterHolder f = new FilterHolder(new AuthorisationFilter());
context.addFilter(... f ...); // ?????
context.addServlet(new ServletHolder(new TestServlet()), "/");

The only info I have found on this is a forum post suggesting the documentation on this needs to be improved.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:41:21+00:00Added an answer on June 17, 2026 at 12:41 pm

    Update: For Jetty version 9.2.2:

        Server server = new Server();
    
        // Note: if you don't want control over type of connector, etc. you can simply 
        // call new Server(<port>);
        ServerConnector connector = new ServerConnector(server);
        connector.setHost("0.0.0.0");
        connector.setPort(8085);
        // Setting the name allows you to serve different app contexts from different connectors.
        connector.setName("main");
        server.addConnector(connector);
    
        WebAppContext context = new WebAppContext();
        context.setContextPath("/");
        // For development within an IDE like Eclipse, you can directly point to the web.xml
        context.setWar("src/main/webapp");
        context.addFilter(MyFilter.class, "/", 1);
    
        HandlerCollection collection = new HandlerCollection();
        RequestLogHandler rlh = new RequestLogHandler();
        // Slf4j - who uses anything else?
        Slf4jRequestLog requestLog = new Slf4jRequestLog();
        requestLog.setExtended(false);
        rlh.setRequestLog(requestLog);
        collection.setHandlers(new Handler[] { context, rlh });
        server.setHandler(collection);
    
        try {
            server.start();
            server.join();
        } catch (Exception e) {
            // Google guava way
            throw Throwables.propagate(e);
        }
    

    Original answer ===

    If you don’t want to use web.xml then use this:

    SocketConnector socketConnector = new SocketConnector();
    socketConnector.setPort(7000); // Change to port you want
    Server server.setConnectors(new Connector[] { socketConnector });
    
    WebAppContext webapp = new WebAppContext();
    
    webapp.setContextPath("/"); // For root
    webapp.setWar("/"); // Appropriate file system path.
    
    // Now you can use the various webapp.addFilter() methods
    webapp.addFilter(MyFilter.class, "/test", 1); // Will serve request to /test.
    // There are 3 different addFilter() variants.
    
    // Bonus ... request logs.
    RequestLogHandler logHandler = new RequestLogHandler();
    NCSARequestLog requestLog = new NCSARequestLog("/tmp/jetty-yyyy_mm_dd.request.log");
    requestLog.setRetainDays(90);
    requestLog.setAppend(true);
    requestLog.setExtended(false);
    requestLog.setLogTimeZone("GMT");
    logHandler.setRequestLog(requestLog);
    
    logHandler.setHandler(webapp);
    
    HandlerList handlerList = new HandlerList();
    handlerList.addHandler(logHandler);
    
    server.setHandler(handlerList);
    
    server.start();
    

    If you do want to use web.xml, instead of the addFilter() methods, just make sure you have a WEB-INF/web.xml in your webapp root path with the following xml:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <!DOCTYPE web-app
       PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
        <filter>
            <filter-name>filterName</filter-name>
            <filter-class>com.x.y.z.FilterClass</filter-class>
        </filter>
        <filter-mapping>
            <url-pattern>/test</url-pattern>
            <filter-name>filterName</filter-name>
        </filter-mapping>
    </web-app>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am embedding a report into my ASP.NET app using the report viewer control.
Embedding Jetty webserver into a Java application is supposedly easy; examples abound. No examples
I'm embedding Jetty (version 7.4.5.v20110725) into a java application. I'm serving JSP pages in
im trying to develop an web server by embedding jetty. So with jetty 7.3
I'm embedding Jetty in a similar manner as described here . When the RequestLogHandler
What are the benefits of embedding jetty vs deploying your webapp(s) in jetty? If
I'm embedding a flash swf into an html page and setting wmode=transparent. I need
I'm embedding Jetty in a Spring based application. I configure my Jetty server in
I am interested in embedding a VLC player into a WPF application. It is
The problem: I am embedding a CSS file into a custom control library with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.