how can i use wild card to map url in java servlet - java

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???

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.

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>

Related

How to map an Java Servlet only to paths that doesn't have any extension?

I have an Java Servlet that I want to execute only if the path requested DOESN'T have any extension, if the path ends with any extension(css, jsp, js...) it must continue normally, without calling the servlet.
How can I do this?
No you can't have such a regex in servlet mapping.
According to servlet specs:
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.
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.
However you can have a default filter (/) to handle all such cases.

Double asterisk in a request mapping

What does it mean when double asterisk is present in a request mapping?
For instance
#RequestMapping(value = { "/", "/welcome**" }, method =
RequestMethod.GET) public ModelAndView welcomePage() { ...
Universally speaking asterisks (in wildcard role) mean
/welcome* : anything in THIS folder or URL section, that starts with "/welcome" and ends before next "/" like /welcomePage.
/welcome** : any URL, that starts with "/welcome" including sub-folders and sub-sections of URL pattern like /welcome/section2/section3/ or /welcomePage/index.
/welcome/* : any file, folder or section inside welcome (before next "/") like /welcome/index.
/welcome/** : any files, folders, sections, sub-folders or sub-sections inside welcome.
In other words one asterisk * ends before next "/", two asterisks ** have no limits.
Ant paths
URL mapping ordering. From Spring Docs:
When a URL matches multiple patterns, a sort is used to find the most
specific match.
A pattern with a lower count of URI variables and wild cards is
considered more specific. For example /hotels/{hotel}/* has 1 URI
variable and 1 wild card and is considered more specific than
/hotels/{hotel}/** which as 1 URI variable and 2 wild cards
...
There are also some additional special rules:
The default mapping pattern /** is less specific than any other pattern. For example /api/{a}/{b}/{c} is more specific.
A prefix pattern such as /public/** is less specific than any other pattern that doesn’t contain double wildcards. For example
/public/path3/{a}/{b}/{c} is more specific.

Dispatcher servlet spring and url pattern

I'm new to spring framework I today I ran into dispatcher servlet configuration in web.xml file and i came up with a question concerning url pattern like this syntax /. So what does actually the "/" symbol apply in case I deploy web application in tomcat server as following: host:port/ or host:port/myWeb/
The pattern / will make your servlet the default servlet for the app, meaning it will pick up every pattern that doesn't have another exact match.
URL pattern mapping :
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.
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.
Rules for path mapping :
The container will try to find an exact match of the path of the request to the path of the servlet. A successful match selects the servlet.
The container will recursively try to match the longest path-prefix. This is done by stepping down the path tree a directory at a time, using the / character as a path separator. The longest match determines the servlet selected.
If the last segment in the URL path contains an extension (e.g. .jsp), the servlet container will try to match a servlet that handles requests for the extension. An extension is defined as the part of the last segment after the last . character.
If neither of the previous three rules result in a servlet match, the container will attempt to serve content appropriate for the resource requested. If a default servlet is defined for the application, it will be used.

GlassFish 3.1 and the tag <url-pattern> of web.xml file

I read that into the tag <url-pattern> I can write a prefix or a suffix pattern url.
But if I try to write something like:
<url-pattern>*sde</url-pattern>
or
<url-pattern>/sde*</url-pattern>
and try to get the url as polsde alsde or sdepp sdelop I have a 404 error and a deploy failed
into server log.
What's wrong?
URL-pattern is wrong.
The pattern you have supplied is invalid
The url-pattern specification:
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.
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.
Please refer Java Servlet Specifications
Interesting question! From reading the 3.0 servlet spec, it doesn't look like the wildcard in servlet mappings works the same way as a regex wildcard; there are boundaries to the mapping.
*.sde would be a valid mapping.
So would "/sde/*", but I don't see any mention of embedded wildcards as you're using them. For the Servlet 3.0 specification, see section 12.2.

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

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.
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.
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.

Categories