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

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.

Related

Passing two forward slashes as path param in Rest API

#Path("/{code:.+}")
public Response getTemplateForCode(#PathParam("code") String code)
I want to pass URI in path parameter eg(ABC://HELLO). But iam getting only single slash ABC:/HELLO only. Can anyone tell how to get double slashes with full uri ABC://HELLO. Is there any REGEX to get double slashes?
I don't want to pass in query params
In Java regex we can use a backslash to escape the special meaning of a slash.
The pattern
\w+:\/\/\w+
matches
ABC://HELLO
See https://regex101.com/r/5vRHHN/1

UriTemplate matching with optional trailing slash

I'm using the Spring Web UriTemplate for some URL matching. In the following example, the second assert fails because of the mismatched slash at the end of the URL:
UriTemplate uriTemplate = new UriTemplate("/myservice/{version}/stuff");
String inputUriA = "/myservice/v1/stuff"; // Without slash
assertTrue(uriTemplate.matches(inputUriA)); // Matches
String inputUriB = "/myservice/v1/stuff/"; // With slash
assertTrue(uriTemplate.matches(inputUriB)); // Does not match
I'm aware that the two cases (with slash and without) technically resolve to two separate REST resources. However, in common use, these URLs are generally considered equivalent. Is there a way to configure the uriTemplate to match both inputUriA and inputUriB?
I'm using Spring Web 4.0.8.RELEASE.

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

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>

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.

Categories