tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <ch...@christopherschultz.net>
Subject Re: Tomcat 6.0.26
Date Fri, 02 Jul 2010 01:47:49 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John,

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:

src/my/package/MyClass.java

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:

build/my/package/MyClass.class

When you create your webapp, this file should go into
WEB-INF/classes/my/package/MyClass.class

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

<web-app>
  <servlet-name>UkJava1900</servlet-name>
  <servlet-class>formprocessors.UkJava1900</servlet-class>
</web-app>

<servlet-mapping>
  <servlet-name>UkJava1900</servlet-name>
  <url-pattern>/UkJava1900</url-pattern>
</servlet-mapping>

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

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:

<form
  method="post"
  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
context).

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwtRUUACgkQ9CaO5/Lv0PDdGgCgjlRpxj6X+J+gU2/r8TRJzwoA
2F4An3hHCVfug56MHlC5y93i1UKvJgVP
=HiOG
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Mime
View raw message