File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes New to servlets, can someone explain this for me. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "New to servlets, can someone explain this for me." Watch "New to servlets, can someone explain this for me." New topic
Author

New to servlets, can someone explain this for me.

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

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

Joined: Jun 03, 2011
Posts: 54

What you have there should work. Try a cache refresh on your browser.


OCA7
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

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

Joined: Jan 10, 2002
Posts: 60046
    
  65

The servlet name is immaterial except within the deployment descriptor for reference purposes. What matters is the mapping URL.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

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

Joined: Oct 27, 2005
Posts: 19541
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 10, 2002
Posts: 60046
    
  65

Except that this is 2012 and if you are still using scriptlets in your JSP you are doing it wrong.
Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

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.

No pain, No gain.
OCJP 1.6
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

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

Joined: Mar 18, 2010
Posts: 516
    
    2

Hi Wendy,

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


Regards
KumarRaja

Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

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

Joined: Jan 09, 2012
Posts: 41
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New to servlets, can someone explain this for me.
 
Similar Threads
Why this EL code part is not working
Unable to compile class for JSP
error for first web application : The requested resource (/Beer-v1/SelectBeer.do) is not available.
Resource not available
help needed with tomcat/requestDispatcher - example form HeadFirst servlets and jsp