1

There are two servlets: A,B

A mapping to: /*

B mapping to: /sub_dir/*

if the url is in /sub_dir/* then it processed by B, else by A.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
lovespring
  • 19,051
  • 42
  • 103
  • 153
  • What do you mean _if the url is in `/sub_dir/*`_? – Sotirios Delimanolis Jul 02 '14 at 18:03
  • 1
    To do this you would want to bypass the container url mapping. So, you would setup a servlet to handle /* and then parse the url string yourself and direct traffic as you see fit using the RequestDispatcher. – Freddy Jul 02 '14 at 18:08
  • @SotiriosDelimanolis Yes! maybe I should say " if the url of the request matched by the /sub_dir/*" – lovespring Jul 02 '14 at 20:06

1 Answers1

2

It might help you to understand the url-pattern

Servlet Matching Procedure

A request may match more than one servlet-mapping in a given context. The servlet container uses a straightforward matching procedure to determine the best match.

The matching procedure has four simple rules.

  • First, the container prefers an exact path match over a wildcard path match.

  • Second, the container prefers to match the longest pattern.

  • Third, the container prefers path matches over filetype matches.

  • Finally, the pattern <url-pattern>/</url-pattern> always matches any request that no other pattern matches.


Have a look at my another post How does a servlets filter identify next destination is another filter or a servlet/jsp? for detailed description.

Can I do this complex url mapping in web container using servlet?

Yes, you can create a complex url mapping by just keeping the rules in the mind.

In your case most specific or longest url-pattern /sub_dir/* takes higher precedence over /* as per rule.

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76