[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2002-07-14
3
Medium Priority
?
2,479 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

656 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