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 banghead situation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "banghead situation" Watch "banghead situation" New topic
Author

banghead situation

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

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 ??
Jason Milliron
Greenhorn

Joined: Jan 25, 2004
Posts: 22
Instead of <form action="/loginservlet"> put <form action="loginservlet">. The extra / before loginservlet is making it look form the port's root, not context root/loginservlet. Hope this helps.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

coz the url is relative to the context path.


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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jason Milliron
Greenhorn

Joined: Jan 25, 2004
Posts: 22
Otherwise, how would the container know which web application to route the request to? The context path must be used in such situations.


If you use this sample code:

<html>
<body>
<form name="testForm" action="TestServlet">
<input type=submit>
</form>

</body>

</html>

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.

Thanks,
Jason
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

If I put a "/" in front of TestServlet then it will look from the server port number


Actually, no it will not. It doesn't work (because the leading slash makes the URL server-relative), but it has nothing to do with the port number.
Jason Milliron
Greenhorn

Joined: Jan 25, 2004
Posts: 22
Actually, no it will not. It doesn't work (because the leading slash makes the URL server-relative), but it has nothing to do with the port number.


Your right, i meant what you said but I didn't know the correct wording. Up in the URL it is placed right after the port number, but nothing to do with the port number. Sorry for the confusion.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: banghead situation
 
Similar Threads
Tomcat with more than one Applications in WEBAPPS folder
Tomcat6.0+eclipse 3.3
web.xml url-pattern wildcards?
URLs and RequestDispatcher.forward()
Compiling Servlets how to..