Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sanjeev Rao
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class Called LoginServlet
I Implemented
--doGet()
--doPost()
--Service()
Methods ,So Which method will called when i request for it?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65113
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never override service(). (Unless you have a really good reason to, which you probably do not.)
 
asdf lkjh
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65113
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Shuki", please check your private messages for an important administrative matter.
 
Swarna Kota
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4588
302
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swarna, please UseCodeTags when you post a source code in future. I added code tags for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic