Catch-all (wildcard) servlet url-pattern overrides file extension patterns - servlets

I would like to achieve the following:
/webapp-context/Page-1 -> Handled by my custom "ContentServlet"
/webapp-context/Another-Page -> Handled by my custom "ContentServlet"
/webapp-context/Page-with-long-title -> Handled by my custom "ContentServlet"
/webapp-context/_cms/<something>.zul -> Handled by ZK framework
My latest attempt looks like this (web.xml extract):
<servlet-mapping>
<servlet-name>zkLoader</servlet-name>
<url-pattern>*.zul</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myContentServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Unfortunately now my content servlet handles all requests (I thought the more specific pattern takes precedence?).
No conflict exists if i map my content servlet to the pattern "/webapp-context/content/*", but that's not what I want.
Thanks for your time.

I just found a solution through this question: Difference between / and /* in servlet mapping url pattern
Using '/' instead of '/*' did the trick for me.
<servlet-mapping>
<servlet-name>myContentServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

Related

Tomcat URL Servlet Mapping

I have a sevelet with mapping like
<servlet-mapping>
<servlet-name>Inventory</servlet-name>
<url-pattern>/inventory</url-pattern>
</servlet-mapping>
I would like to create a url mapping to /invlist that goes to /inventory?q=list
<servlet-mapping> only maps a Servlet to a URL but cannot map a URL to URL.
You need to create another Servlet for /invlist and in this Servlet , redirect to /inventory?q=list using HttpServletResponse#sendRedirect
<servlet-mapping>
<servlet-name>Inventory</servlet-name>
<url-pattern>/inventory</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>InventoryList</servlet-name>
<url-pattern>/invlist</url-pattern>
</servlet-mapping>
Then in the InventoryList Servlet :
httpServletResponse.sendRedirect("inventory?q=list")

How does the `getServletMapping()` affects the URL in Spring WebMVC? [duplicate]

I have a web.xml file with content:
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.mycompany.test1</servlet-class>
</servlet>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>org.mycompany.test2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/path/test</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/path/test/*</url-pattern>
</servlet-mapping>
I tried requests
.../path/test/abc
.../path/test
Both requests are processed by Servlet2. Why?
UPDATE
Thank you guys for your help.
I realised that behaviour depends on order of servlet-mapping declaration.
I tried this web.xml
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.mycompany.test1</servlet-class>
</servlet>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>org.mycompany.test2</servlet-class>
</servlet>
<servlet>
<servlet-name>servlet3</servlet-name>
<servlet-class>org.mycompany.test3</servlet-class>
</servlet>
<servlet>
<servlet-name>servlet4</servlet-name>
<servlet-class>org.mycompany.test4</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/path/test</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/path/test/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet3</servlet-name>
<url-pattern>/path/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet4</servlet-name>
<url-pattern>/path</url-pattern>
</servlet-mapping>
results:
.../path/test/abc - servlet2
.../path/test/ - servlet2
.../path/test - servlet2
.../path/abc - servlet3
.../path/ - servlet4
.../path - servlet4
From Servlet 3.0 specification, this is how the web container must locate the servlet after receiving a request (emphasis mine):
The path used for mapping to a servlet is the request URL from the
request object minus the context path and the path parameters. The
URL path mapping rules below are used in order. The first successful
match is used with no further matches attempted:
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. Many containers provide an implicit
default servlet for serving content.
The container must use case-sensitive string comparisons for matching.
You should also look the specification of mappings (given below):
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/<contextroot>/. 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
Let us look at examples now. Consider the following set of mappings:
Path Pattern Servlet
/foo/bar/* servlet1
/baz/* servlet2
/catalog servlet3
*.bop servlet4
The following behavior would result:
Incoming Path Servlet Handling Request
/foo/bar/index.html servlet1
/foo/bar/index.bop servlet1
/baz servlet2
/baz/index.html servlet2
/catalog servlet3
/catalog/index.html “default” servlet
/catalog/racecar.bop servlet4
/index.bop servlet4
Note that in the case of /catalog/index.html and /catalog/racecar.bop, the
servlet mapped to “/catalog” is not used because the match is not exact.
Now coming to your problem :)
/path/test belongs to the 5th point of specification of mappings. Which means only the paths ending in /path/test will target servlet1.
However /path/test/* qualifies for the first point of the same specification. This means that:
.../path/test will be handled by servlet1 and
.../path/test/abc will be handled by servlet2
Which was verified by me in a test application.
Your paths conflict.
Both of your paths mean the same, the '/*' doesn't make any difference.
Apparently then when you try your path, the last match (servlet2) is executed.
You usually put in a path with the Servlet name, like for example:
/path/test/servlet1
/path/test/servlet2

How to change the path of swagger-ui in spring-mvc

How can I change the path of swagger-ui so that it is not loaded on / but at /docs/
The simple solution recommended in some blogs would be to unpack swagger-spring-mvc-ui sources in webapps dir. But I search for a elegant solution where the webjar swagger-spring-mvc-ui is a dependency like all others.
Is there a reason why something like this doesn't work?
<!-- swagger api declaration -->
<servlet>
<servlet-name>ApiDeclarationServlet</servlet-name>
<servlet-class>com.wordnik.swagger.servlet.listing.ApiDeclarationServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ApiDeclarationServlet</servlet-name>
<url-pattern>/api-docs/*</url-pattern>
</servlet-mapping>

why should we include <servlet-name> in deployment descriptor file

i have tried without giving servlet-name tag in my web.XML but it did not worked for me.
we are adding servlet-class & url tags in deployment descriptor file here what is the exact use of adding servlet-name tag in deployment descriptor file (web.XML).
Cant we run without servlet-name tag in our web.xml
The <servlet-name> is a unique identifier for a single Servlet. Some deployment descriptor elements some times need to refer to a specific servlet. They do that through the value of the <servlet-name>. For example, you might have multiple <servlet-mapping> elements, one for each extension. You might also have a Filter which you want applied only to a specific servlet.
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.pack.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.pack.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<servlet-name>myServlet</servlet-name>
</filter-mapping>

Configuring web.xml for webservices and servlet

I am new to Restlets. Trying to configure the web.xml (on JBoss). I have 2 entries, one for a servlet (got nothing to do with webservices) other for webservices, using Restlet. Here are the entries..
<servlet>
<servlet-name>AuthenticationServlet</servlet-name>
<servlet-class>com.safeid.web.server.api.servlet.AuthenticationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AuthenticationServlet</servlet-name>
<url-pattern>/authenticate/*</url-pattern>
</servlet-mapping>
<!-- Start of Entries for the REST Web Services. -->
<context-param>
<param-name>org.restlet.application</param-name>
<param-value>com.safeid.web.server.SafeIDRouterApplication</param-value>
</context-param>
<servlet>
<servlet-name>RestletServlet</servlet-name>
<servlet-class>com.noelios.restlet.ext.servlet.ServerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<!-- END of Entries for the REST Web Services.-->
Both don't work together. In the above setup the Restlet works. However when I change the
RestletServlet
/*
to something like
<servlet-mapping>
<servlet-name>RestletServlet</servlet-name>
<url-pattern>/credential/*</url-pattern>
</servlet-mapping>
the Restlet stop working and the AuthenticationServlet works fine. What am I missing here?
I had a similar frustration. Perhaps what I found out may help.
I had Router entries in my Application class like this:
router.attach("/users", UsersResource.class);
And things worked fine when my servlet mapping was like this:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
When I changed it to something like this:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/users/*</url-pattern>
</servlet-mapping>
it stopped working.
The problem is that the servlet container "consumes" or removes the part of the URL that it matched. In this case, it removes "/users". So if you were using a url like this:
http://www.mywebsite.com/users
you would have to change it to be:
http://www.mywebsite.com/users/users
Of course, you can make the url-pattern be whatever you want:
<servlet-mapping>
<servlet-name>Sandbox</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
and then you'd access it like this:
http://www.mywebsite.com/rest/users
The url-pattern gets stripped off, and you get whatever is leftover in your Application class for your own routing purposes.
HTH
Looks like you're missing the init-params as in the example below.
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
<init-param>
<param-name>org.restlet.application</param-name>
<param-value>my.class.that.extends.Application.MyApplication</param-value>
</init-param>
</servlet>
You need a class that extends org.restlet.Application (at least in Restlet 2.0 anyway).

Resources