Empty String ("") for URL mapping is not working in web.xml

257 views Asked by At

I'm working on a requirement where I need to write the servlet to the context path of the application, according to servlet specification document empty string i.e. "" mapping should work for application root context. I have tried different combinations like

    <servlet>
        <servlet-name>Redirect</servlet-name>
        <servlet-class>RedirectServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Redirect</servlet-name>
        <url-pattern></url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Redirect</servlet-name>
        <servlet-class>RedirectServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Redirect</servlet-name>
        <url-pattern>""</url-pattern>
    </servlet-mapping>

these are not working. Is there any other way to write the servlet for the context path for path "/" there is a servlet already. Requirement is to write the servlet for context path

1

There are 1 answers

0
Joakim Erdfelt On

Empty string is for application root context.

It's a legacy / backwards compat feature.

The recommended declaration for root context is still /, and the returned string for application root context will always be "" (empty string).

That being said, this little bit of trivia about application root context isn't relevant to your issue at hand.

You are dealing with url-pattern, a different concept.

The url-pattern rules are applied after the request has matched an application root context.

A url-pattern that has quotes is invalid.

A url-pattern that is empty is pointless, as it will never match an incoming request and you'll never have a dispatched request sent to it.