tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Tomcat 6.0.26
Date Fri, 02 Jul 2010 01:47:49 GMT
Hash: SHA1


I think we might want to start again and work from the ground up. If
you've been working in the dark-ages with the invoker servlet and
non-packaged servlets, these things can pile up on you.

First of all, make it a point to put all your servlet classes into
packages. In fact, make it a point to put /all/ your classes into
packages. When you put a class into a package, the source (.java) file
should go into a directory that matches it, and when you compile it, the
.class file will end up in a similar directory.

For instance, if you have a class called "MyClass" in the package
"my.package" (classes are usually written in CamelCase while package
names are usually all lowercase), you should have your files laid out
like this:


The source code to MyClass should have a package declaration like this:

package my.package;

when you compile to a directory called "build", you'll have:


When you create your webapp, this file should go into

Next, you'll need to map this class to one or more URLs. These URLs are
relative to what's known as the "context path" of a web application. Web
applications can be deployed either as a "root" web application (that
is, they will be accessed via a URL like http://host/servlet) or as a
non-root application with a non-blank context path (where you'll access
the webapp via a URL like http://host/context/servlet).

In web.xml, to map your servlet to "/UkJava1900", you should have (as it
appears you do):



Now, when you hit http://host/context/UkJava1900, you'll execute the
code in the formprocessors.UkJava1900 class (which came from

Note that URL patterns are case-sensitive. Your latest error message
says "The requested resource (/ukjava1900) is not available.". Note the
discrepancy between the <url-mapping> and the error message: the
capitalization is inconsistent. There's nothing that says your URL must
be /UkJava1900... you are free to use /ukjava1900 if you choose.

Now, we come to the JSP. Here is the best way to write your <form> tag:

  action="<%= response.encode(request.getContextPath() + "/ukjava1900") %>">

Two things are happening, here:

1. request.getContextPath gets the current context path (like "/foo" if
your webapp is deployed to /foo, or "" if you are using a ROOT-deployed

2. response.encode will add a jsessionid parameter to the end of the URL
if the client doesn't support HTTP cookies.

The first thing is important because it will allow you to re-deploy your
webapp under arbitrary context paths without having to re-write all your
links. The second is important if you want to support cookie-less
clients, which is always nice to do.

For recently-written webapps, I would recommend using the JSTL tag
library which has a convenient tag for doing things like this:

<form method="post" action="<c:url value="/ukjava1900" />">

The <c:url> tag knows how to do all the above stuff with less
possibility of typing errors and stuff. Always nice.

Hope that helps,
- -chris
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message