Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URL-PATTERN Confusion

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That explains it perfectly Jason. I thought I was missing something, but it really is as simple as it appears.
Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic