lovespring
lovespring

Reputation: 19559

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

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.

Upvotes: 1

Views: 226

Answers (1)

Braj
Braj

Reputation: 46841

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.

Upvotes: 2

Related Questions