Solved

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

Posted on 2002-07-14
3
2,469 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 50 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now