File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFS&J Question on page 118 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "HFS&J Question on page 118" Watch "HFS&J Question on page 118" New topic
Author

HFS&J Question on page 118

Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Hi all!
There is the question on the page 118;
Q: What if I want to support both GET and POST from a single servlet?
A: Developers who want to support both methods, ussually put logic in doPost(), then delegate to a doGet() if this request doesn't need to do post things:
public void doPost(...) throws ...{
doGet(reques, response);
}

I am not sure, but I think there is an error here. May be they had in mind:
... put all the logic in doPost() method then call this method from doGet():
public void doPost(...) throws ... {
// some logic here ...
}

public void doGet(...) throws ... {
doPost(request, response);
}

Thanks

[ November 11, 2004: Message edited by: Serghei Jelauc ]
[ November 11, 2004: Message edited by: Serghei Jelauc ]

SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
Sami Sayeed
Ranch Hand

Joined: Oct 05, 2004
Posts: 39
Hi,
i dont think so anythings wrong in that

its like you have the logic in the
doGet()
{
logic
}
doPost()
{
doGet(req,res);
}

cheers
sami
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Yes. But in book in answer they put logic in doPost() and call empty doGet() inside the doPost
[ November 12, 2004: Message edited by: Serghei Jelauc ]
Shiang Wang
Ranch Hand

Joined: Jun 20, 2003
Posts: 96
I think it works the same both ways, don't you think?

Shiang


SCBCD, SCWCD, SCJP
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
I agree it works the same both ways, but what the reason to call empty doGet() inside doPost()?
Let say you don't know what method will be in a request HTTP GET or HTTP POST. In this case you have to implement both doGet and doPost. If you implement for example doGet() you just will call it from doPost(), and this make sense.
But I cant find the logic in case, if you inplement doGet() and will call from that doGet() empty doPost(). What will happen if request will contain the HTTP POST method?
1. If you have overriden doPost() but it is empty - you servlet will do nothing.
2. If you do not have overriden doPost() the exception will be thrown.

Am I right?
Romy Huang
Ranch Hand

Joined: Sep 30, 2003
Posts: 35
if you want to support same function in both doPost() and doGet(), you can do both way.
1) in doGet(), call doPost() which has function logic.
or
2) in doPost(), call doGet() which has function logic.
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Yes it doesn't matter whether call doPost with logic from doGet or vise versa, but in book they said that logic in doPost and they call just empty method doGet from doPost.
[ November 14, 2004: Message edited by: Serghei Jelauc ]
 
GeeCON Prague 2014
 
subject: HFS&J Question on page 118