Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] How to prevent ServletHandler$Default404Servlet from being added

Am 08.05.2015 um 01:23 schrieb Jan Bartel:

> The thing is, you haven't really provided enough information for
> anyone to determine exactly what is going wrong for you.

sorry. I thought the question Joakim asked me was enough as
clarification.

> Maybe this will help you. I've modified one of the standard embedded
> examples (original here:
> https://github.com/eclipse/jetty.project/blob/master/examples/embedded/src/main/java/org/eclipse/jetty/embedded/OneServletContext.java)
> to show what happens to the configuration of the ServletHandler at
> various stages:
> 
>         Server server = new Server(8080);
> 
[...]

> As you can see, if there is no servlet mapped to "/", then the
> ServletHandler will instantiate a ServletHandler.Default404Servlet.
> If, however, you provide a mapping yourself, that is used instead.
> Maybe you can use the above to verify against your setup.

Thanks for that. The difference to my application is that the
main server configuration comes from the config-XML-file and
the e.g. AS2Server is accessing the server after its startup
and tries to add its own servlet. So the code looks something
like this:

            Server hs = null;
            hs = Server.getServer(_serverName);
            if (hs == null){
                logSystemError("HTTP-Server with name '" + _serverName + "' not found");
                return;
            }
            Handler handler = hs.getChildHandlerByClass(ContextHandlerCollection.class);
            if (handler == null){
                logSystemError("Unable to install servlet. The server contains no ContextHandlerCollection");
                return;
            }
            ContextHandlerCollection coll = (ContextHandlerCollection) handler;
            Handler[] schs = hs.getChildHandlersByClass(ServletContextHandler.class);
            if (schs == null){
                schs = new Handler[0];
            }
            ServletContextHandler hc = null;
            for (int i = 0; i < schs.length; i++){
                hc = (ServletContextHandler) schs[i];
                if (hc.getContextPath().equals(_contextName)){
                    break;
                }
            }
            if (hc == null){
                hc = new ServletContextHandler(ServletContextHandler.SESSIONS);
                hc.setContextPath(_contextName);
                coll.addHandler(hc);
            }
            logSystemMessage("Adding servlet to context");
            ServletHolder holder = new ServletHolder();
            holder.setClassName(AS2MessageReceiveServlet.class.getName());
            holder.setName("AS2ReceiveServlet");
            hc.addServlet(holder, _pathSpec);
            try{
                Server.removeDefault404Servlet(hc.getServletHandler());
            }
            catch(ServletException se){
                logSystemError("unable to remove default servlet", se);
            }

Your example needs a stopped server where in my case the server
is already started. In that case the Default404Servlet is already
added before I'm able to add my own. Before I ended up with the
code above (especially the call of removeDefault404Servlet at
the end) I tried a couple of things but always ended up in a
dead end.


Cheers, Lothar


Back to the top