This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Service method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Service method" Watch "Service method" New topic
Author

Service method

Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Hi,
Can any one explain the diffrence between service method and doGet and doPost method.
Thanks
Arathi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Basically, HttpServlet has an implementation of service() which parses the request, bundles up the necessary information into a HttpServletRequest object, and invokes doGet() or doPost() depending on whether the request was an HTTP GET or POST. It's much easier, then, for you to override doGet() or doPost(), because you're letting HttpServlet do all the dirty work of interpreting the headers and request data.
In theory, there could be other kinds of Servlets which are not HttpServlets. These other Servlets would implement service() in some other way. I'm not aware of anyone really taking advantage of this design capability.


[Jess in Action][AskingGoodQuestions]
Venkatesh C.R.
Greenhorn

Joined: Jul 28, 2003
Posts: 14
hi,
Generally You call or utilize a servlet from a html or any such content files.They generally allow the get and post methods.So in order to reply to these requests we need to use the get and post methods.But if we call one servlet from an another servlet using RequestDispatcher() method the service() method will be invoked.
hope this satisfies your question.
BYE... C.R.Venkatesh
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Hi,
I have one more doubt. When a developer writes a servlet program, heusually uses doGEt or doPost method. Will this doGet or DoPost method will call service() or what.Or is it vice versa
Please clear my idea.
Thanks
Arathi
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

It is the service mthod which calls the doGet and doPost. Do not override the service method in you servlet class.
Hope this helps.


Groovy
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
Hi,
All servlets must implement the Servlet interface. The init() , service(),
destroy() methods are defined in this interface.
There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed below).
doGet, if the servlet supports HTTP GET requests
doPost, for HTTP POST requests
doPut, for HTTP PUT requests
doDelete, for HTTP DELETE requests

Thus in service we use these doXXX() .
Cheers,
Gaya3
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Since doGet and doPost oftentimes perform the same task many programmers create a common method and have doPost and doGet call that method. Or you can have doPost call doGet and place your handler in doGet (or vise versa).
Sometimes it's advantageous to have different handlers for each method type. If your are writing a servlet to be an HTML form handler, make your form use method="post". Then, if you do not want your servlet called directly, you can have doGet return an error page. Place your normal handler code in doPost.
This works because when typing a URL into a browsers address line, the browser issues a GET command for the requested URL.
For the most part, doGet and doPost perform identical operations - therefore you only need to code once for both of them. They differ only in how they recieve data from the page. doGet receives it's information encoded in the URL string, doPost gets it from the HTTP header. This is where the service method comes in. It determines, based on the HTTP method requested, which of doPost or doGet methods to call.
As mentioned earlier, you really don't want to mess with the service method. Think of it as the traffic director for the HTTPServlet class. You only need to concern yourself with where that traffic goes - doPost, doGet, doPut, and doDelete.
Hope this helps.
[ July 31, 2003: Message edited by: Tom Blough ]

Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Brett Bell
Greenhorn

Joined: Jul 03, 2003
Posts: 7
Originally posted by Ernest Friedman-Hill:
In theory, there could be other kinds of Servlets which are not HttpServlets. These other Servlets would implement service() in some other way. I'm not aware of anyone really taking advantage of this design capability.

I have worked on digital tv channels which require the this lower level access because they don't quite adopt the full HTTP standard.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I have worked on digital tv channels which require the this lower level access because they don't quite adopt the full HTTP standard.

Huh, interesting. Can you elaborate a little -- where do the servlets run, and where do the clients run (in the set-top box?)
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Hi,
Could any explain the situation when we use service method and when we use do get and dopost method.
Thanks
Dave Cronin
Ranch Hand

Joined: Apr 25, 2003
Posts: 114
If you override the service method, it means that the servlet responds to both HTTP GET and HTTP POST methods, and it cannot tell them apart. Also, you lose default server implementations such as OPTIONS and TRACE. So it is better to use doGet and doPost for servlets, and recommended by the spec too. To get the same functionality for both GET and POST you can just call doGet from doPost, or if you just want to respond to POST for example, just override doPost.
I am not sure why the TV channel application would need to override service(), unless perhaps it is not an HttpServlet but a GenericServlet. There aren't any doGet or doPost methods at all in GenericServlet.


Dave Cronin
SCJP, SCWCD, SCBCD
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Hi,
Can you elaborate more on this "To get the same functionality for both GET and POST you can just call doGet from doPost, or if you just want to respond to POST for example, just override doPost."
What is the same functionality you mean. Any example
Also example for generic servlets
Thanks
[ August 01, 2003: Message edited by: Arathi Raj ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Arathi
I have myself started studying servlets, so I am not exactly sure whether I am correct or not. I guess that providing the same functionality means that you call one method from the other.
Example

Now generally both the methods may be interchangably used so you may call one from the other.
[ August 02, 2003: Message edited by: Anupam Sinha ]
Dave Cronin
Ranch Hand

Joined: Apr 25, 2003
Posts: 114
Anupam's code example is exactly what I meant. However, if you're designing a user input form for a web page, you may want to use only the POST method. That means you should only override doPost, and not doGet.
The GET method was only intended to "get" or retrieve information, even though it is often used for update methods as well. The problem with using GET for update is that the length of the URL is limited, which limits the amount of data you can send. Also all the updates will be recorded in the browser's history list, which can be a security problem. Imagine using passwords with GET.
To quote from the API, "GenericServlet defines a generic, protocol-independent servlet." I don't have any examples for GenericServlet, but an HttpServlet is a subclass of GenericServlet. The HttpServlet has its own specialised way of handling the service method, which is useful because it detects the various HTTP request types for you, such as GET and POST. That actually makes it easier to write servlets for web pages since you only need to override doGet or doPost to respond to a GET or POST request.
If you had you own protocol, you could subclass GenericServlet and write your own protocol-handling servlet, like MyProtocolServlet. But HttpServlet and HTTP are so useful that it's unlikely you'd want to do that.
Arathi Raj
Ranch Hand

Joined: Nov 22, 2002
Posts: 90
Hi,
In what circumstances we may require to call doget from dopost and vice versa.
Thanks
Mike Southgate
Ranch Hand

Joined: Jul 18, 2003
Posts: 183
I'm still pretty new at this but...
the html form tag has a method parameter which can be set to either "post" or "get".
If you use post in the tag then you use doPost, if you use get in the tag then you use doGet. To decide which one to use when:
1) get is faster
2) get is limited to 4KB of data or less
3) get's parameter's are displayed in the URL - security issue for passwords etc
4) get allows users to bookmark pages including parameters
So, most of the time I'd expect you'd use get. But on signon screens of when large amounts of data are involved, you'd use post.
Often, the servlet doesn't care if the html said get or post, either one is ok from the servlets perspective. In that case, one would often code either the doget or the dopost to handle the data and then just have the other one call it. That way the code is only written once, yet both methods of html are handled.
ms


ms<br />SCJP, SCJD
 
Don't get me started about those stupid light bulbs.
 
subject: Service method