Hi all. I have tomcat 5.0 the context of my web application is : /toddstore consider the following url : http://localhost:8080/toddstore/login.html login.html contains a form to perform a log in operation. <form action="/loginservlet"> invoking login.html by the browser, I got that the resource is not found. changing the action to : action="/toddstore/loginservlet"> it works ! why ??? AFAIK, the first approach should work, coz the url is relative to the context path. any ideas ??
Incorrect. URLs referenced from the HTML pages such as form actions, images, style sheets, script files and so on are server relative. Otherwise, how would the container know which web application to route the request to? The context path must be used in such situations.
The TestServlet is looking from context root on. I don't have to include the context path in my action. If I put a "/" in front of TestServlet then it will look from the server port number, such as localhost:8080/TestServlet, but if I leave off "/", it will look at localhost:8080/context_root/TestServlet. The server handles it.
Yes, when you leave off the "/" you get a relative reference. But that reference is relative to the current URL not to the context path. So it could easily break if you move the page to a new URL which doesn't have the same relative base. Not recommended. I always specify a full absolute URL for form actions and other references so that there are no surprises down the line.