File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Can we Have Write Service,doGet,doPost method implementation in a single servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can we Have Write Service,doGet,doPost method implementation in a single servlet" Watch "Can we Have Write Service,doGet,doPost method implementation in a single servlet" New topic
Author

Can we Have Write Service,doGet,doPost method implementation in a single servlet

Sanjeev Rao
Greenhorn

Joined: Oct 17, 2008
Posts: 1
I have a class Called LoginServlet
I Implemented
--doGet()
--doPost()
--Service()
Methods ,So Which method will called when i request for it?


-----SANJAY@TECHY-----
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
Which method will called when i request for it?


Service will never be called. GET or POST requests will go to doGet or doPost.

Pehaps you were thinking about overriding the service( request, response) method but Service will not do that since Java is case sensitive.

IF you had overriden service() correctly, it would get all requests since the default service that routes requests to doGet etc..

Bill
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

Never override service(). (Unless you have a really good reason to, which you probably do not.)


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
asdf lkjh
Greenhorn

Joined: Oct 17, 2008
Posts: 12
doGet() or doPost() methods will be called based on what method you define in the jsp/html i.e. if you define method="post", doPost() will be called, and if you define method="get", doGet() will be called.
And again there is easiest way to find out what is getting called when, by using either debugger or logs
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61601
    
  67

"Shuki", please check your private messages for an important administrative matter.
Swarna Kota
Greenhorn

Joined: Jun 02, 2010
Posts: 1
service() method will be called first.
If you make an explicit call to doget() method from the service method then goGet() method runs.

For Example:





Output:

Servlet TestServlet at /Servlets------GET

If we modify the service method



Output:

doGet runs
doPost runs
Servlet TestServlet at /Servlets------GET
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3849

Swarna Kota wrote:Output:
Servlet TestServlet at /Servlets------GET

Why do you think you got that output? Just read the replies above to get an idea. You should not override the "service()" method. This post is almost two years old anyway. If you have any doubt you may start a new thread on your own.
And Welcome to JavaRanch


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3206
    
  49

Swarna, please UseCodeTags when you post a source code in future. I added code tags for you.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we Have Write Service,doGet,doPost method implementation in a single servlet