Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URL rewriting with Tomcat/Servlets

Graham Spice
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everybody,
Me and a friend have hit a stumbling block. We are developing an online portal for school using JSPs, Servlets and Tomcat. We want to pass parameters to a servlet using URL rewriting.
The path we have to our JSPs is:
C:\tomcat\webapps\portal\jsp\<our jsp files.....>
The path we have to our servlets is:
C:\tomcat\webapps\portal\web-inf\classes\<our servlet files....>
When we use a button on a jsp and use ACTION="../servlet/<our servlet>
it works fine. The servlet picks up the parameter using the getParameter method. Tomcat finds the servlet directory properly.
When we use an href on a jsp and try to use URL rewriting it doesn't work.
When we use <href="../servlet/<our servlet>?ACTION=admin it tries to find the path exactly as written, including the "servlet" directory.
It looks for http://localhost:8080/portal/servlet/<our servlet>?ACTION=admin
This is close but when we use the buttons on a jsp Tomcat correctly finds the servlet directory. When we use URL rewriting it tries to find the exact path written.
Anybody run into this before, any ideas??? I thought it could be the web.xml file (currently we don't use one). I only thought this file was necessary for multiple servlets???
Thanks in advance,
Graham (As green as a greenhorn gets...)

Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should definately use the web.xml file.

using the word 'servlet' in your URL is the 'older school' way of doing it, and is sort of a 'lazy' thing to do anyways.

If someone didn't want to make a web.xml entry for their servlet, all they needed to do is place the class file in the classes directory, and put 'servlet' in the URL.

I admit that coding entries into web.xml is sometimes a pain, but it's fairly straightforward. Here, I've mapped the class file serAdmin.class (in WEB-INF/classes) to the name AdminServlet. This name then makes to the URL-pattern 'admin'. With this mapping in place, for *both* form submissions and URL's, all you need is:
<form action="admin?ACTION=admin" >
<a href="admin?ACTION=admin" >

Notice the lack of slashes and ..'s And also why it's generally a bad idea to have a parameter named 'action'. It gets confusing after a while. Myself, I use actionTarget

And one last thing about the choice of words 'URL Rewriting'. This is generally used to mean the process whereby the session id is placed into the URL. This is for users that don't have cookies enabled.

URL rewriting in JSP/Servlets is supported by two methods on the response object.

If a user has cookies enabled, then no problem the String you send into these methods is returned un-modified. If they have them turned off, you'll get links and URLS that look something like:
[This message has been edited by Mike Curwen (edited November 01, 2001).]
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic