?
Solved

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

Posted on 2002-07-14
3
Medium Priority
?
2,477 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 …
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month9 days, 17 hours left to enroll

762 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