Configuring web.xml for webservices and servlet - servlets

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

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.

web app web.xml error [duplicate]

This question already has answers here:
Getting error: The content of element type "web-app" must match,
(3 answers)
Closed 5 years ago.
I am getting an error in my GWT application being developed in Eclipse. It's in the web.xml file. Here's the error:
The content of element type "web-app" must match "(icon?,display- name?,description?,distributable?,context-
param*,filter*,filter-mapping*,listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-
file-list?,error-page*,taglib*,resource-env-ref*,resource-ref*,security-constraint*,login-config?,security-
role*,env-entry*,ejb-ref*,ejb-local-ref*)".
I have seen numerous posts about this and the problem is the order of the elements of the file, but that fix doesn't work for me (I have also tried putting all the <servlet-mapping> tags right after the corresponding <servlet>, it did not work either)
My web.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>com.yachtcloser.server.DispatchServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>com.yachtcloser.server.UploadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>download</servlet-name>
<servlet-class>com.yachtcloser.server.DownloadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.yachtcloser.server.LoginServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/dispatch.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/upload.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.do</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>Yc.html</welcome-file>
</welcome-file-list>
</web-app>
Are there any other ways of tracking this error; other files that are linked to this?
well, as per new format of DTD web-app tag might contains following tags.
<!ELEMENT web-app (icon?, display-name?, description?, distributable?,
context-param*, filter*, filter-mapping*, listener*, servlet*,
servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?,
error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,
login-config?, security-role*, env-entry*, ejb-ref*, ejb-local-ref*)>
above mentioned icon, display-name, description, distributable....etc are in the same order as they have mentioned in the DTD file.
e.g. if you put description tag before display-name it gives error.
I deleted the file and pasted the text from the old one into a new file with the same name and now there's no errors.
Just for a reference: A SelectAll->Cut->Save->Paste->Save also fixes the problem. Probably there is a line ending character issue.
I followed the suggestion for "copy all" - "cut" - "paste" - "save" and this seemed to clear up the message. I found that in the "pasted" version all tabs had been converted to spaces.
So it seems that the web.xml validator in Eclipse does not like tabs.
The error itself gives you the clue. The order of the elements in your web.xml should follow the order specified in the error.
<displayname>
</displayname>
<description>
</description>
....... like this the elements should be in order as it says in the error.

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>

Resources