I have a web application that is functioning properly (servlets called) when I place the application folder in the {JETTY_HOME}/webapps directory.
If I place a copy of the web application in a sub folder of webapps, then all the static files are called when I browse to the site, but the servlets that I call via ajax are returning 404.
http://localhost/shlaa
- calls /shlaa/CommentController.do correctly with no error.
http://localhost/mapapp/shlaa
-
returns 404 for the ajax call /mapapp/shlaa/CommentController.do
web.xml in both paths /WEB_INF folders contains the following
<servlet> <servlet-name>CommentController</servlet-name> <servlet-class>web.CommentController</servlet-class> </servlet> <servlet-mapping> <servlet-name>CommentController</servlet-name> <url-pattern>/CommentController.do</url-pattern> </servlet-mapping>The jetty.xml folder in {JETTY_HOME}/etc contains the following:
<Call class="org.mortbay.jetty.webapp.WebAppContext" name="addWebApplications"> <Arg><Ref id="contexts"/></Arg> <Arg><SystemProperty name="jetty.home" default="."/>/webapps</Arg> <Arg><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Arg> <Arg type="boolean">True</Arg> <!-- extract --> <Arg type="boolean">True</Arg> <!-- parent priority class loading --> </Call>
In order to correctly deploy Java Web application to some Application Server or just simple Web Container (like in your case), the web app (also called web module) MUST have a specific structure. The top-level directory of a web module is the document root of the application.
Here is a Web Module Structure:
Now, what you define in
<url-pattern>in web.xml file it is just a logical / virtual path for some servlet. Again, it is NOT a real path to the physical location of the servlet class, but a logical path – you make it up as you wish.OK, now you must put your webapp with the right structure right in the webapps directory. For example, in the above picture Assembly Root symbolizes your webapp folder. So you take that folder either directly or you can make a WAR (Web application ARchive) and put it right under
webappsdirectory. It’s not that in webapps you have some directory and in that direcory there are several webapps. All web apps must be right under webapps directory.So in your case, if your webapp folder has a name shlaa, you must put that folder right under
webapp. Period.Now quote from the official Java EE docs:
In your case, the URL to your webapp will be
http://localhost:8080/shlaaNow qoute from the Jetty wiki docs (Jetty/Howto/SetContextPathto):
To make the long story short just place your web app right under webapps directory to make everything work.
As for the
<url-pattern>– feel free to define whatever patter you like.NOTE: actually the are several ways to configure Jetty, i.e. XML configuration is not the only one. See Configuring Jetty for the details.
Quote from The Java™ Servlet SpecificationVersion 3.0:
Example with your names.
Your webapp name is mapapp (it’s the application root). You want your CommentController.do to be accessed via
Then (if all other requiremants like directory structure etc are met) in your web.xml you put the following:
The first forward slash (
/) in the above url-pattern symbolizes context root / context path (mapapp in this case).This way it will work.
Here are some usefull links.
The Java EE 5 Tutorial:
Modules
Applications
The Java EE 6 Tutorial:
Applications
Jetty
Applications
Hope this will help you.
UPDATE
For the Jetty specific configuration see this link:
How to have a web app respond only on a specific port