• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

New to servlets, can someone explain this for me.

 
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 54
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have there should work. Try a cache refresh on your browser.
 
Alan Smith
Ranch Hand
Posts: 185
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 171
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Wendy,

Which diagram is that and which book are you referring to ?
 
Wendy L Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic