?
Solved

Exact difference with doGet() and doPost() of HTTPServlet?

Posted on 2002-07-14
3
Medium Priority
?
2,481 Views
Last Modified: 2011-09-20
Hello all,

I would like to know what is the exact difference between doGet() and doPost()methods of HTTPServlet?

In my application, the former programmer used both of them with the exact same code inside, because he said that it is difficult to know which of them will be called by the environment.

What could be the reason for such practice?

Thanks,
Freesky
0
Comment
Question by:freesky
3 Comments
 
LVL 2

Accepted Solution

by:
xxMariusxx earned 200 total points
ID: 7153046
Let's say you have 2 HTML forms:
--------
<form method="get" action="MyServlet">
<input name="someData">
<input type="submit">
</form>
--------
<form method="post" action="MyServlet">
<input name="someData">
<input type="submit">
</form>
--------

Notice the "method" in each form is the only thing that differs.  GET is the default.  With a GET, all the data in the form is encoded in a QueryString and appended to the URL of the servlet.  This is typically used for small amounts of data.  POST is used for large amounts of data.  For more information regarding GET vs. POST, go to http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html to read the Method Definitions for HTTP.

HttpServlet has a service() method.  The purpose of service() is to analyze incoming requests and determine appropriate method calls.  If you make an HTTP request and the form method was GET, then service() will call doGet().  If it was POST, then the service() method will call doPost().

Of course, when you create your own servlets, you're extending HttpServlet...so service is inherited from the superclass.  You're free to override it.

Putting the same code in doGet() and doPost() is a waste of disk space.  If they're meant to have the same functionality, do something akin to the following:

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     performTask(req, resp);
}

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     performTask(req, resp);
}

private void performTask(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     // put your code in here
}

You probably don't want to override service() to achieve this result because doGet() and doPost() aren't the only methods service() deals with.

Marius
0
 

Author Comment

by:freesky
ID: 7170109
Thanks a lot Marius for your complete and easy to understand answer.

Have a nice day!

Freesky :)
0
 
LVL 10

Expert Comment

by:jaggernat
ID: 12397275
that was an excellent piece of explanation.

Jay.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question