When I bring up the URL http://localhost:8080/Beer-v1/form.html my page displays correctly but when I click on the submit button the servlet doesn't work. I suspect it can't find it but I can't figure out what I'm doing wrong.
The context path is just the /Beer-v1 part. You should not have the rest of the absolute URL in there.
The context path can be obtained programmatically from the request and should not be hard-coded.
Joined: Aug 08, 2011
I still can't get this to work. I understand the context path shouldn't be hard coded but I'm new to servlets and trying to do an example in the Head First Servlets book. I changed my form.html to the following and I still can't get this to work:
What happens? Just saying "it doesn't work" isn't helpful. What's the full text of the error message?
Anything in the logs?
Joined: Aug 08, 2011
There's no reference to my BeerSelect servlet in any of the log files. The funny thing is the servlet in the first chapter works fine. The only difference in this one is that I'm using a package and doing a form action which I think it can't find. The code compiled fine but I think it just can't find the servlet.
Paul Clapham wrote:The "action" of the form is treated as relative to the URL of the document in which the form is located
That is only true if the URL is page-relative. Starting the URL with the context path makes the action URL non-relative to the page and independent of the current URL.
Sorry to butt in to the thread, but now I'm confused. Given the code
How does the browser know whether "Beer-v1" is a context path or not? Or if that's the wrong question, how does the browser know what host to send the request to if the action is independent of the page's URL?
Paul Clapham wrote:How does the browser know whether "Beer-v1" is a context path or not? Or if that's the wrong question, how does the browser know what host to send the request to if the action is independent of the page's URL?
The leading slash tells the browser that its a server-relative URL.
I would say add doGet(..) in you servlet and put some SOPs/log messages and acess the servlet directly "http://localhost:8080/Beer-v1/SelectBeeer.do" from the IE's address bar and verify if that hits the servlet.
When you submit the html see the url in the addressbar ,i think that will tell you the servlet location which the html is trying to hit.