Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can we override service() method in a servlet?

 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My code does not work when I extend HttpServlet class (I get error as "The POST/GET method is not supported by the URL") but works when I override service() method extending GenericServlet. Code is as follows



web.xml file is as follows:-



I don't want to override service() method but use doGet()/doPost(). How can I fix it? Please help!!

Thanks in advance!!
 
E Armitage
Rancher
Posts: 989
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure you want to extend GenericServlet instead of HttpServlet?

You should not be overriding the service method. Override doGet or doPost or both if you need to. If you have common code to execute for doGet and doPost then put it in a method and call that method from the doGet and doPost when you need to.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help!!

I changed the code to as follows and it worked but it was not working earlier. Thanks a lot!!



It's working now.
 
Tim Holloway
Bartender
Posts: 18419
60
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The default HTTPServlet service() method is what invokes doGet() and doPost() as well as the other HTTP-related command methods.
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it. Thank you!!
 
S Majumder
Ranch Hand
Posts: 349
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhimanyu,

GenericServlet is a generic, protocol-independent servlet.HttpServlet is a servlet tied specifically to the HTTP protocol.Http servlet serve mainly GET ,POST,PUT,DELETE request .
if you want to override the service() then you need to extends GenericServlet .

Thanks ,
Satya
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Appreciate the help and thank you for sharing the valuable knowledge.

Now I know when to override service() and other methods.
 
S Majumder
Ranch Hand
Posts: 349
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little :

The default service() method in an HTTP servlet routes the request to another method based on the HTTP transfer method (POST, GET, and so on). For example, HTTP POST requests are routed to the doPost() method, HTTP GET requests are routed to the doGet() method, and so on. This enables the servlet to perform different request data processing depending on the transfer method. Since the routing takes place in service(), there is no need to generally override service() in an HTTP servlet. Instead, override doGet(), doPost(), and so on, depending on the expected request type.

Source :webpage

Hope this help you ,

Thanks,
Satya
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it helped. Thank you for the documentation.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


All classes used in servlet/JSP should be in a package. If there is no package the JVM looks in the "current" directory, something you have no control over in the usual server environment. This comes up a lot with beginners.

Bill
 
Abhimanyu Jain
Ranch Hand
Posts: 151
1
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you!! I will make sure that all other servlets/JSP are in a package from now onwards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!