This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi As in HFJS book chapter 3, im trying set the <url-pattern> to something like 'SelectBeer.do" . But i am not able to set the url-pattern to anything except the actual servlet class name. When i run the web app(the same web app described in chapter 3, the beer advisor web app) after i click the submit button it goes to the url which ends with the servletclassname. i.e. http://localhost:8080/BeerV1/BeerSelect. i changed the action atttribute value in the form.html to 'selectbeer.do'.see my web.xml also.
Thanks for your response. I think you can help me out since you are SCWCD! so you might have tried the same example in chapter3. Hmm.. right now i can't as im from a different place now. but i can assure you that i copied the same form.html code from the book.(Chapter 3 HFSJ) is it dependant on the SDK version? i have netbeans 5.5.1 which takes jdk1.4, not 1.5. is this a reason? Ok, i remember it.. the html starting from <form>...
It looks like to me that the code written for the form.html is not complete. As it is pointed out you got to place the submit button too. If you have not placed that in the actual code that you used in testing please do so first and retry again.Assuming that you have a submit button at the right place in the code of form.html the request which would be generated by the browser has to point to the following url, http://yourserverip ort/approot/beerselect.do.
And the web container first look for an exact match , folder match and then extension match in the web.xml file. In this case it seems to me that there is an exact match and extension match too. exact match takes the precedence over extension match then. Hence your application must be mapped to /beerselect.do , which in turn maps it self to BeerSelect servlet.
Please do try the example again and keep posted the updates.
Hope this helps
Joined: May 01, 2008
Oh, i included the submit button in form.html, but i forgot to add that while posting. The submit button is placed correctly. the problem is when i click on the submit button it goes to the /BeerSelect servlet not the beerselect.do(which is again the servlet). i dont know why it checks for the servlet name instead of the url-pattern i gave. apart from web.xml and the form.html, are there any places where i can change this? i dont understand the exact mach and extension match concept. is it because i used the same name for the servlet and the url-pattern? is it case sensitive?
I'm a little confused myself - can you clarify what you are expecting to happen, and what is actually happening!
In your web.xml (I presume this is the one in the <tomcat-path>\webapps\Beer-v1\WEB-INF\) you have the following:
So anything posted to /selectbeer.do will be matched against the servlet-name BeerSelect which in turn is mapped to the servlet com.example.web.BeerSelect (i.e. <tomcat-path>\webapps\Beer-v1\WEB-INF\classes\com\example\web\BeerSelect.class).
So the other BeerSelect servlet you mention in your posting - how is this defined and where is it? Why have two servlets with the same name??
The other thing worth checking is caching issues - both the browser and the container may be caching previous versions of "faulty" code.
View the source of your form in the browser and make sure it's as you expect - if not try a Ctrl F5 (assuming IE). Try restarting your container to make sure it's picking up any changes you make to web.xml etc.
Hi Ranil, Actually my problem was with the second way. when i used
the url in the address bar was ../BeerSelect not the expected ../beerselect.do i doubt this is the expected result as you suggested(exact match and extension match) see that i used full lower case letters for the urlpattern. will that effect the result?
Yes , it effects. The url that you are trying to access must resolve to an available url mapping in web.xml. so that if you want to access /selectbeer.do , then the form must be submitted to "selectbeer.do" not "SelectBeer.do". You may be able to get a good understand on URL mapping rules by referring to HFSJ 2nd Edition , page 616 onwards.
hope that helps you
Joined: May 01, 2008
Hi Ranil, Now everything works fine. these are the scenarios i tried:
Everything works fine. as far as i know, the url-pattern in web.xml and the value of the action attribute in the form.html should be the same including letter casing.