This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Servlets and the fly likes Overriding the doPost method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Overriding the doPost method" Watch "Overriding the doPost method" New topic

Overriding the doPost method

B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39
Hi Guys,

Im am going over my old webdev tutorials and i have got stuck. Hopefully i might get some help here

this is the code:

What i am trying to do is override the doPost method with a doGet method. The code above will compile but i get the HTTP error 405.

Does anyone see where i am going wrong?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32819

You haven't defined doGet. So you are calling the superclass' implementation. I vaguely recall that implementation returning an error code so by default it doesn't do anything. You might try removing the doGet call and seeing if it helps.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2846

If you want to support both the doGet() and doPost() methods, a better alternative is write a different method say processRequest() and write all your request handling code in that method.Then simply call that method from doGet() and doPost() as well.

Hope this helps

|Asking Good Questions|
B Crosthwaite
Ranch Hand

Joined: Oct 22, 2008
Posts: 39

Thanks for you replies. I read some post on the interweb and i modified my code a little bit and came up with this:

This does the job with both get and post from the HTML page.

I was just wondering if this was the best way of going about this certain task? or is there a more efficient way?

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Calling doPost from doGet or vice versa is very common.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Overriding the doPost method
It's not a secret anymore!