Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 82
Problem to Alipay 10 46
Eclipse for Java EE development 2 28
What browser will run Java? 7 68
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

828 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