aspose file tools*
The moose likes Servlets and the fly likes how do i choose doget() or dopost() corrently? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how do i choose doget() or dopost() corrently?" Watch "how do i choose doget() or dopost() corrently?" New topic
Author

how do i choose doget() or dopost() corrently?

Hk mingham
Greenhorn

Joined: Oct 16, 2010
Posts: 4
i know the different between the post and get,but ,you know ,when request is maked by container,the container will automatically choose the right http method (post or get),and then it will past this http method (post or get) to servlet,and then ,the servlet will figure it out.


but the question is ,the servlet is designed by us,and how do we make the right choose between dopost() or doget()?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Hk mingham wrote:but the question is ,the servlet is designed by us,and how do we make the right choose between dopost() or doget()?

Case counts. Precision counts. It's doGet() and doPost().

What do you mean by "how we choose?"

Do you mean how do we choose which gets called? You don't. The container calls the one that matches the HTTP method of the request.

Do you mean how do we choose which to implement? You implement whichever one you want to support. doGet() for GETs. doPost() for POSTs. Both if you want to support both.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Hk mingham wrote:
the container will automatically choose the right http method

Yeah, that depends on the request.
The container calls the service() method. And then depending on the type of the request, service() method calls either doGet() or doPost().
You can override the service() method if you want, but it's not advisable.

Hk mingham wrote:
how do we make the right choose between dopost() or doget()?


In case you don't not the type of service and want to process both GET and POST requests in the same way, then you can write the functionality in one method and call that method from the other.

Example:


BUt it's better not to mix GET and POST - as they're made for different purposes.
GET is used to get data, it's not supposed to change anything on the server.
POST is not idempotent, data submitted in the body of POST is usually used to change something on server. For example, a transaction.


OCPJP6
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Trivikram Kamat wrote:BUt it's better not to mix GET and POST - as they're made for different purposes.

This is an important point that many people do not properly understand. It's more important in web services than in web applications, but it's still an important distinction.
Hk mingham
Greenhorn

Joined: Oct 16, 2010
Posts: 4
totally thank you Trivikram Kamat and Bear Bibeault,i have a clearier mind on it after i have finished the answer from you.yeah,thaks so much.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
You're welcome
 
Don't get me started about those stupid light bulbs.
 
subject: how do i choose doget() or dopost() corrently?