Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New to servlets, can someone explain this for me.

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Louis Bros
Ranch Hand
Posts: 54
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you have there should work. Try a cache refresh on your browser.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The servlet name is immaterial except within the deployment descriptor for reference purposes. What matters is the mapping URL.
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Except that this is 2012 and if you are still using scriptlets in your JSP you are doing it wrong.
 
Kumaravadivel Subramani
Ranch Hand
Posts: 171
Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Kumar Raja
Ranch Hand
Posts: 547
2
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Wendy,

Which diagram is that and which book are you referring to ?
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Daniel Val
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic