Forums Register Login

New to servlets, can someone explain this for me.

+Pie Number of slices to send: Send
Hi,

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.

web.xml


Servlet


Java Class to get answer



HTML form

+Pie Number of slices to send: Send
What you have there should work. Try a cache refresh on your browser.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
The servlet name is immaterial except within the deployment descriptor for reference purposes. What matters is the mapping URL.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I always recommend to add context path before your action value of form


I guess your jsp/html page is not directly under your root directory
+Pie Number of slices to send: Send
Except that this is 2012 and if you are still using scriptlets in your JSP you are doing it wrong.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Hi Wendy,

Which diagram is that and which book are you referring to ?
+Pie Number of slices to send: Send
 

Kumar Raja wrote:Hi Wendy,

Which diagram is that and which book are you referring to ?


the book Alan was asking about "Head First Servlets & JSP".
+Pie Number of slices to send: Send
 

Alan Smith wrote:

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.

- if war is called whatever.war, then the servlet url will be http://localhost:8080/whatever/yourhtmlhere.html and you take it from there.

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
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
https://gardener-gift.com


reply
reply
This thread has been viewed 1674 times.
Similar Threads
Head First BeerSelect problems
servlet can't import the model
help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:17:50.