Jetty
  1. Jetty
  2. JETTY-1223

DefaultServlet: NPE when setting relativeResourceBase and resourceBase is not set.

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.1.25
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      1

      Description

      Code to reproduce:

      ServletHolder servletHolder = new ServletHolder(new BotServlet());
      root.addServlet( servletHolder, "/bot/*" );

      ServletHolder defaultSH = new ServletHolder(new org.mortbay.jetty.servlet.DefaultServlet());
      defaultSH.setInitParameter("relativeResourceBase", "./src/main/java/org/myapp/web/files");
      root.addServlet(defaultSH, "/");

      DefaultServlet.java @ 181 (in Jetty 6.1.24):
      _resourceBase = _context.getContextHandler().getResource(URIUtil.SLASH).addPath(rrb);
      // getResource() returns null, so Jetty logs:

      03:46:17.751 INFO [main] / Unavailable javax.servlet.UnavailableException: java.lang.NullPointerException

      I suggest to handle this case and throw an exceptio with an explaination of what's the cause.

        Activity

        Hide
        Michael Gorovoy added a comment -

        Confirmed this issue with current trunk of Jetty 6. It is caused by the code that embeds Jetty failing to set the the resourceBase of Context that the servlet is being added to, that is an error in configuration of the context and not DefaultServlet. This issue does not apply Jetty 7.

        Below is the proper way of configuring a context in order to use the relativeResourceBase init parameter for the DefaultServlet.

        Context context = new Context(server,"/test",Context.SESSIONS);
        context.setResourceBase(new File("./src/test").getAbsolutePath());
        server.setHandler(context);

        ServletHolder defaultSH = new ServletHolder(new DefaultServlet());
        defaultSH.setInitParameter("relativeResourceBase", "./resources");
        context.addServlet(defaultSH, "/");

        The way to fix this issue is to deconstruct that statement to first check the result of ContextHandler.getResource() method call and if it is equal to null put an explicit error message into the log. This should take about 1h to fix.

        Show
        Michael Gorovoy added a comment - Confirmed this issue with current trunk of Jetty 6. It is caused by the code that embeds Jetty failing to set the the resourceBase of Context that the servlet is being added to, that is an error in configuration of the context and not DefaultServlet. This issue does not apply Jetty 7. Below is the proper way of configuring a context in order to use the relativeResourceBase init parameter for the DefaultServlet. Context context = new Context(server,"/test",Context.SESSIONS); context.setResourceBase(new File("./src/test").getAbsolutePath()); server.setHandler(context); ServletHolder defaultSH = new ServletHolder(new DefaultServlet()); defaultSH.setInitParameter("relativeResourceBase", "./resources"); context.addServlet(defaultSH, "/"); The way to fix this issue is to deconstruct that statement to first check the result of ContextHandler.getResource() method call and if it is equal to null put an explicit error message into the log. This should take about 1h to fix.
        Hide
        Michael Gorovoy added a comment -

        Patch for this issue is attached

        Show
        Michael Gorovoy added a comment - Patch for this issue is attached
        Hide
        Greg Wilkins added a comment -

        Applied

        Show
        Greg Wilkins added a comment - Applied
        Hide
        Jesse McConnell added a comment -

        closing resolved issues

        Show
        Jesse McConnell added a comment - closing resolved issues

          People

          • Assignee:
            Michael Gorovoy
            Reporter:
            Ondrej Zizka
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: