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 doPost() vs doService() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "doPost() vs doService()" Watch "doPost() vs doService()" New topic
Author

doPost() vs doService()

Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
If I've got a servlet that doesn't need to be passed any parameters, it just gets called from one html page and displays results in a new page. Do I use doService() or the usual doPost()? Something's not working at the moment, and I'm not quite sure how to figure it out. Is there a method that does not require any parameters but that will run automatically?
Thanks,
Annette
Chris Klu
Greenhorn

Joined: Jan 03, 2001
Posts: 18
If it doesn't take any parameters use doGet()
Danny Mui
Ranch Hand

Joined: Dec 14, 2000
Posts: 42
If you want to treat gets and posts the same, override the doService() method so that a post and get will result in the same output. On the other hand, if you need to be specific (ie ignore post methods) then override the doGet() and doPost() methods as needed.
of course all IMHO.
Annette L'Heureux
Ranch Hand

Joined: Dec 07, 2000
Posts: 135
Thanks all!
I got it to work. I overrided doGet().
Does anyone know how to call it from a link?
i.e. on a webpage I have something like:
<a href="http://some url/servlet/servlet_name"> click here </a>
I've tried that, but it doesn't work. The only way I can do it is to call it directly from the address bar in the browser. Can it be done with a link?
thanks,
Annette
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
Sure - there is nothing magic about a servlet, a GET is what a browser does for any link. Anywhere you would write href="someurl" the url can point to a servlet.
The main thing is that your server must be configured correctly to interpret that URL as a request to the servlet.
Bill

Java Resources at www.wbrogden.com
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Annette,
I just give an example which I used. Please take a look at this. In servlet side, I coded doGet(..) and doPost(..) calls doGet(..) and didn't do anything with doService(..). The html page is not a form. This is a link I used in my html page. When we click on Serup Accounts link it will invoke doGet(..) of SetupServlet.
By default all href links will be linked to doGet(..) of the servet to which they are linked to. If you could give the same address fron browser and invoke means, the same url in a html also should work since both use GET method. Try to check the spelling (case sensitive) of the url in your href link.

<A HREF="http://www.webappcabaret.com/maha/servlet/SetupServlet?RequestType=Setup Accounts">Setup Accounts</A>
regds
maha anna
B Wiley Snyder
Ranch Hand

Joined: Nov 26, 2003
Posts: 50
Originally posted by Annette L'Heureux:
Thanks all!
I got it to work. I overrided doGet().

Does anyone know how to call it from a link?
i.e. on a webpage I have something like:
<a href="http://some url/servlet/servlet_name"> click here </a>

I've tried that, but it doesn't work. The only way I can do it is to call it directly from the address bar in the browser. Can it be done with a link?

thanks,
Annette


How do you over ride a doGet(), I have to post info to the page but I'm doing it from a link??? Is that possible, if you post from a link you're screwed. How can I do a post from a link if I can't over ride get?



William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12682
    
    5
<a href="http://some url/servlet/servlet_name"> click here </a>

I've tried that, but it doesn't work.

The use of "/servlet/" indicates that you have encountered the dreaded "invoker" servlet problem. Read this ranch FAQ on the invoker.
Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doPost() vs doService()
 
Similar Threads
how to invoke both doGet() and doPost()
doGet() and doPost()
handling servlet exception gracefully
Coding in the doService() JSP page.
doGet() calling doPost() problem -- URL line visible in destination page!