--------
<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/Protocol
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(HttpServletReq
// 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.
difference between doGet() and doPost()
# | doGet() | doPost() |
---|---|---|
1 | In doGet() the parameters are appended to the URL and sent along with header information. | In doPost(), on the other hand will (typically) send the information through a socket back to the webserver and it won't show up in the URL bar. |
2 | The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters. | You can send much more information to the server this way - and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects! |
3 | doGet() is a request for information; it does not (or should not) change anything on the server. (doGet() should be idempotent) | doPost() provides information (such as placing an order for merchandise) that the server is expected to remember |
4 | Parameters are not encrypted | Parameters are encrypted |
5 | doGet() is faster if we set the response content length since the same connection is used. Thus increasing the performance | doPost() is generally used to update or post some information to the server.doPost is slower compared to doGet since doPost does not write the content length |
6 | doGet() should be idempotent. i.e. doget should be able to be repeated safely many times | This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable. |
7 | doGet() should be safe without any side effects for which user is held responsible | This method does not need to be either safe |
8 | It allows bookmarks. | It disallows bookmarks. |
When to use doGet() and when doPost()?
Always prefer to use GET (As because GET is faster than POST), except mentioned in the following reason:
If data is sensitive
Data is greater than 1024 characters
If your application don't need bookmarks.
How do I support both GET and POST from the same Servlet?
The easy way is, just support POST, then have your doGet method call your doPost method:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doPost(request, response);
}
Should I override the service() method?
We never override the service method, since the HTTP Servlets have already taken care of it . The default service function invokes the doXXX() method corresponding to the method of the HTTP request.For example, if the HTTP request method is GET, doGet() method is called by default. A servlet should override the doXXX() method for the HTTP methods that servlet supports. Because HTTP service method check the request method and calls the appropriate handler method, it is not necessary to override the service method itself. Only override the appropriate doXXX() method.
It is very Helpful for exams, Thanks
ReplyDelete"Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
ReplyDelete"