0

I want to achieve this:

http:\\localhost:8080\mysite\search\cotton\search.html
http:\\localhost:8080\mysite\search\bean\search.html
http:\\localhost:8080\mysite\search\cosmetic\search.html
http:\\localhost:8080\mysite\search\shoe\search.html


  <servlet-mapping>
    <servlet-name>abcSearch</servlet-name>
    <url-pattern>/search/*/search.html</url-pattern>
  </servlet-mapping>

mean one pattern for the above all urls

can any one help out for me???

muhammadanish
  • 437
  • 2
  • 6
  • 20

2 Answers2

1

The rules for mappings are as follows

In the Web application deployment descriptor, the following syntax is used to define mappings:

  • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.

  • A string beginning with a ‘*.’ prefix is used as an extension mapping.

  • The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<context-root>/. In this case the path info is ’/’ and the servlet path and context path is empty string (““).

  • A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.

All other strings are used for exact matches only.

So this

/search/*/search.html

would match exactly

http://host/context/search/*/search.html

You can't get path matching at the middle of the path with Servlet's url-patterns.

If you only have the 4 paths, I recommend you put 4 <servlet-mapping> element with each exact path match.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • you mean http:\\localhost:8080\mysite\search\shoe\*.html, but in this way i cannot handle the above scenario – muhammadanish Jan 17 '14 at 16:47
  • @Danish Forget that suggestion. I was wrong. I recommend you use a `Filter` that matches all paths and dispatches accordingly. – Sotirios Delimanolis Jan 17 '14 at 16:49
  • @Danish Can't really type that up right now, but see [here](http://stackoverflow.com/questions/2725102/how-to-use-a-servlet-filter-in-java-to-change-an-incoming-servlet-request-url). The `Filter` will read the path it's handling, determine the appropriate servlet and use it. – Sotirios Delimanolis Jan 17 '14 at 16:55
-1

Try to change URLs like:

http:\\localhost:8080/mysite/search/cotton
http:\\localhost:8080/mysite/search/bean
http:\\localhost:8080/mysite/search/cosmetic
http:\\localhost:8080/mysite/search/shoe

or like that:

http:\\localhost:8080/mysite/cotton/search.html
http:\\localhost:8080/mysite/bean/search.html

For the first case pattern will be <url-pattern>/search/*</url-pattern> and for the second one <url-pattern>*/search.html</url-pattern>

ADS
  • 708
  • 3
  • 14