Im going through the Head First Servlets & JSP book and so far so good. I just need this explained. If you have the book its Chapter 3 for reference. I have a simple html form where a user can make a selection and then when he hits submit he gets a couple of answers based on his selection come up on another page. I use a servlet to call a Java class which gives back the right answers based on the users choice and the servlet prints it back to the screen. The code is below for all 4 files (deployment des., servlet, html, java class). My question is that I am trying to understand how the servlet maps everything together. The form action below is BeerSelect and the servlets url mapping is /BeerSelect. Is this not the link between the two?? If I change both (as a test to see how it works) to BeerSelec and /BeerSelec respectively I get a http error saying "The requested resource (/BeerV1/BeerSelect) is not available." How is this though when I have changed the vales to BeerSelec and restarted the server? Im using Tomcat. /BeerSelection from the http error doesn't exist in my code so how is it still popping up and not recognising /BeerSelec instead? Sorry if I didn't explain it great but thanks!
EDIT: the code below for the mapping in the html and servlet has 'BeerSelec' as the value name. I originally had it as 'BeerSelect'. My bad.
Louis Bros wrote:What you have there should work. Try a cache refresh on your browser.
No, if I change the Serlvet name and html action from BeerSelect to BeerSelec (or anything else for that matter) a html error 404 "The requested resource (/BeerV1/BeerSelect) is not available" error. i don't understand how its reporting an error with /BeerSelect in it when I have changed the code to something else.
Bear Bibeault wrote:The servlet name is immaterial except within the deployment descriptor for reference purposes. What matters is the mapping URL.
Yeah I know that. I don't touch the Servlet name or its class name. I just change the mapping url and the post action on the html form. Still doesn't work.
If you changed the HTML page to use BeerSelec (without the t) as the action and it still tries to go to BeerSelect (with t), then there either is a cache problem (as Louis suggested) or the HTML page you're accessing is not the one you modified.
Normally restart the web/app server will resolve the problem.(when you do web.xml changes) Since the servlet container may pointing to the old servlet mapping and thrown error due to mismatch.
further on, there is a very pretty diagram that shows how all the bits link together, It is a really good and helpful diagram, which is why I remember it 1 year later, but the book is at home so can't offer anymore help
Louis Bros wrote:What you have there should work. Try a cache refresh on your browser.
No, if I change the Serlvet name and html action from BeerSelect to BeerSelec (or anything else for that matter) a html error 404 "The requested resource (/BeerV1/BeerSelect) is not available" error. i don't understand how its reporting an error with /BeerSelect in it when I have changed the code to something else.
OK, it looks like your app was not deployed properly. Other approach might work, however I suggest you work like that:
- pack the app in a war not exploded
- go in server.xml and search for unpackWARs=true and change it to false
- move autodeploy from whatever it is to true
Now when you copy your war file to the webapps folder will deploy automatically (you should monitor the console and you will see it deploys a couple of seconds after you copy your new file)
- you don't delete existent file and copy new one as you will not be able to do so. You just copy the new one on the top of the old one.
You can copy on top of existent one and autodeploy 1000 times without restarting tomcat.
BTW, you know how to pack a war, right:
/WEB-INF/classes you copy the classes, in /WEB-INF you put web.xml, then the content directly in the / (root) folder of the war.
Try and advise please.
Daniel
Post by:autobot
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards