Tomcat URL Servlet Mapping - servlets

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")

Related

web.xml: how to map domain/xx/* to a servlet?

I have these URLs
abc.com/aa/url1
abc.com/ab/url2
abc.com/ac/url3
etc.
How to setup web.xml to map this pattern to a servlet?
<servlet>
<servlet-name>NAME</servlet-name>
<servlet-class>MyClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NAME</servlet-name>
<url-pattern>/xx/*</url-pattern>
</servlet-mapping>
Where xx is a two-letter string. What do I put in the place of xx so that all links above will go to MyClass?
You can have more than one url-pattern entries:
<servlet>
<servlet-name>NAME</servlet-name>
<servlet-class>MyClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NAME</servlet-name>
<url-pattern>/aa/*</url-pattern>
<url-pattern>/ab/*</url-pattern>
<url-pattern>/ac/*</url-pattern>
</servlet-mapping>
Unfortunately, the servlet mapping cannot take a regular expression so you can't do a variation of /a*/* for the 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>

Java web app - Deployment descriptor - URL pattern mapping

What is the difference between the two URL mappings : /* and / ?
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>DefaultServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
What I understood after reading the specs is that :
The pattern /* will force everything through MyServlet.
The pattern / will make DefaultServlet as the default servlet in the app .
Both almost means the same to me . Please let me know if there is any specific difference ?
Thanks for the links , going through them I have compiled this answer . Let us see a sample web.xml :
Case 1:
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
`
In this case all requests /context/ , /context/anything and /context/sample.do will invoke servlet2 .
Case 2:
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
`
In this case requests like /context/, /context/anything invokes servlet2 and /context/sample.do will invoke servlet1.

Filtering static content Jersey

I'm trying to serve static content (a HTML form that calls a Jersey REST resource) from the same webapp as the servlet that handles the requests to the resource. As I understand I can filter requests to static content away from the Jersey servlet. My web.xml is as follows, but at the moment I am unable to access the static content nor the resource...both were working separately.
<filter>
<filter-name>my-filter</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<param-name>com.sun.jersey.config.property.WebPageContentRegex</param-name>
<param-value>/*.html</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>my-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>my-service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mydomain.ws.myservice</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>my-service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
FWIW your original problem was probably because the param-value for the WebContentRegex was not a regular expression. Ok techincally it was, but it is not matching what you probably want. You should try something like /.*.html instead.
I setup my services such that the rest service are under their own subpath separate from the static content:
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-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