• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Delegating to doGet()

 
vinod balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have a doubt in writing servlet which needs to support both doGet() and doPost(). In hfsj pgno 118, it is given as

public void doPost() throws..
{
doGet(req,res);
}

I have doubt in the above code, if a form with get() is submitted to this servlet how the servlet service method able to locate the correct method as doPost is only visible.. Please clear my doubt..

Thanks.
 
Rohitt Vermaa
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If both the methods are to be supported then both doGet and doPost method needs to be implemented. This is clearly mentioned in HFSJ.

public void doGet(HttpServletRequest req,
HttpServletResponse res)
{
// Action to be performed
}

public void doPost(HttpServletRequest req,
HttpServletResponse res)
{
doGet(req,res);
}
 
Sreeraj G Harilal
Ranch Hand
Posts: 310
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If a form submitted to a servlet using GET method,that servlet overriding only doPost() method, an error page will send to the client.

To handle HTTP GET method you must override doGet() method of HttpServlet class.


it's only allow developers to avoid code repetition if both doGet() and doPost() method doing the same thing.

example

 
vinod balaji
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ranchers for your detailed explanation..
Thanks once Again..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic