Link to home
Start Free TrialLog in
Avatar of ricjava
ricjava

asked on

Writing codes in service() method

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
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ricjava
ricjava

ASKER

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:)
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(...)
Avatar of Mick Barry
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.
Avatar of ricjava

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ricjava

ASKER

Thanks everybody..
you are welcome :-)