?
Solved

Writing codes in service() method

Posted on 2005-04-26
10
Medium Priority
?
209 Views
Last Modified: 2010-03-31
hi experts,

Often I seen programmer writing codes in the service method of servlet. By right, I think we should write only in doGet or doPost. Why want to write in service method? Is it a bad practise or somewhat any reason?

Thanks
0
Comment
Question by:ricjava
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 15

Accepted Solution

by:
aozarov earned 140 total points
ID: 13873129
The service method does nothing else then detecting the request type (GET,POST,PUT,DELETE) and delegate to the right doXXX
method. J2ee recommendation is to override the needed method[s] instead of using the service directly and I quote from
the Javadoc: "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 above). "
One case where you might consider putting code in the service method (though you can easilly get around without it)
is to have some factorized code there before calling super.service(...) for further dispatching.
0
 

Author Comment

by:ricjava
ID: 13873397
Hi,
>One case where you might consider putting code in the service method (though you can easilly >get around without it)
>is to have some factorized code there before calling super.service(...) for further dispatching.
What is factorized code?
And why do u want to call the super.service?
Can you explain a bit:)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13873424
Factorized code means code that you want to apply regardless if it is get/post/...
e.g you might want to set the response buffer size for any kind of request.
That is relevant only if the same servlet supports/overrides more then one request type [get/post/...])
After you applied to common/factorized code you want to delegate the call to the right method (doPost,doGet,...)
and that can be achived by calling super.service(...)
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 92

Expert Comment

by:objects
ID: 13873930
You can put code in service() if you want but then you need to handle lots of things yourself, which are handled by the service() method of HttpServlet (which yoiu would be overriding).

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServlet.html#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

Basically it does all the standard things for you, and leaves you to do the stuff specific to your servlet.
0
 

Author Comment

by:ricjava
ID: 13874077
I don't like to put codes in service method but saw others doing it.

so objects, u don't recommend anyone to put codes in service method?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 80 total points
ID: 13874203
> u don't recommend anyone to put codes in service method?

thats correct, unless they have a specific reason for it. ie. the default service() implementation somwehow doesn't meet their needs.
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 80 total points
ID: 13876289
Well, if you write a Servlet which extends GenericServlet, then you might want to do so, but if you're extending HttpServlet - I've never written code in the service () method.
0
 

Author Comment

by:ricjava
ID: 13883141
Thanks everybody..
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13883362
you are welcome :-)
0
 
LVL 92

Expert Comment

by:objects
ID: 13883369
(:
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

621 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