File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes URL-PATTERN Confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "URL-PATTERN Confusion" Watch "URL-PATTERN Confusion" New topic
Author

URL-PATTERN Confusion

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

The Url-pattern in the web.xml file has always kind of confused me so I hope I can get some clarification.
I have a small little test web app. I have a JSP page which submits a single text field to a Servlet. My JSP page is in a pages directory.
Ok, so in my web.xml file the url-pattern for my Servlet is /actions/LoginServlet.
Now, as long as my webapp is the ROOT web app in Tomcat, I can submit to the servlet via action="/actions/LoginServlet".
Now, if I put my webapp in its own folder, and try and access my Servlet, it, of course, takes me back to the ROOT and can't find the servlet.
So my question is, how do I write the url-pattern so that my servlet is accessable relative to the location of my WEBAPP.


GenRocket - Experts at Building Test Data
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

I changed my action in my form to "../actions/LoginServlet". This seems to do the trick rather the web app is the ROOT web app or in it's own context.
Is this correct what I did?
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
The / goes to the root of your webapp, the display name that is defined in web.xml.
So if you have a webapp called "test", if you set up a servlet mapping with url-pattern /actions/LoginServlet, then the URL http://localhost/test/actions/LoginServlet will work.
However, I believe the FORM action interprets / as the root of the host, so what you end up with is http://localhost/actions/LoginServlet, which sends you to the ROOT webapp. The mapping you set up is in terms of the test webapp, though. They are independent of each other.
Doing ../actions/LoginServlet moves you back one from the pages directory, which puts you back at the test root. You could also do the following:
action="/test/actions/LoginServlet"
That's my preferred method, it ensures you are going to the right place, just in case your pages directory structure changes.
If the login.jsp page was directly under the test webapp (the "test" directory), then you could also do action="actions/LoginServlet".
Hope that helps some.
[ September 15, 2003: Message edited by: jason adam ]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

That explains it perfectly Jason. I thought I was missing something, but it really is as simple as it appears.
Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: URL-PATTERN Confusion
 
Similar Threads
about web.xml file.
welcome page not comming
Problem with Apache Tomcat 5.5.12 and calling Servlets
One War file with multiple webapps
servlet context problem tomcat7