Servlet URL pattern to match a URL that ends with a slash ("/")

11,507

Solution 1

It's quite possible that you can't do this by mapping in web.xml.

What you can do is to map servlet to /mypath/* and then check part after /mypath/ via request.getPathInto(). If it is "/", run your code. If it isn't, return 404 error.

Solution 2

In NetBeans, if I go to the Servlets tab on the web.xml file, the IDE would complain with, "Error: URL patterns cannot end with slash (/)". From the URL spec, it reads,

httpurl        = "http://" hostport [ "/" hpath [ "?" search ]]
hpath          = hsegment *[ "/" hsegment ]

So yes, an URI with an ending slash is invalid.

Share:
11,507
Jon Cram
Author by

Jon Cram

I'm Jon Cram, a software developer and interface designer living with my wife and cat on a boat near London (UK) in a house in Cardiff. I work for Box UK Lime Green Tangerine myself on a new startup. I work mostly with web apps, developing in PHP for quick-and-easy bits and Java for heavy-duty bits. I recently discovered that Grails is the best thing ever. I own and run Hosting Reborn, the UK's first pay-as-you-go web hosting service.

Updated on June 04, 2022

Comments

  • Jon Cram
    Jon Cram about 2 years

    I'd like to specify a Servlet URL pattern to match a URL that ends with a slash ("/") and only a slash.

    I understand that the pattern

        /example/path/*

    will match a URL of

        http://example.com/example/path/

    and that this appears to work. However, that same pattern would also match URLs of

        http://example.com/example/path/a/
        http://example.com/example/path/b/
        http://example.com/example/path/c/

    I'm merely looking for a URL pattern that will match http://example.com/example/path/ only without also matching http://example.com/example/path/a/ and so on.

    Clarification: a URL pattern ending with a slash is not allowed.