How do I support both GET and POST protocol from the same Servlet?

Alex Chaffee

The easy way is, just support POST, then have your doGet method call your doPost method:

public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
	doPost(req, res);   

Note that implementing the <mono>service()</mono> method is usually not what you want to do, since HttpServlet provides its own implementation of <mono>service()</mono> that turns around and calls <mono>doGet(), doPost(),</mono> etc.

Lee Crocker (LCrocker@INFORMANT.COM): "It's probably cleaner not to override <mono>service()</mono> when extending HttpServlet. The existing service method just calls <mono>doGet()</mono>, <mono>doPost()</mono>, etc. as appropriate, so you can certainly override it if you feel like it, but then you wind up not only treating GET and POST identically, but also all other HTTP commands, like HEAD, TRACE, and OPTIONS. If you want GET and POST to do the same thing, just have <mono>doGet()</mono> and <mono>doPost()</mono> call the same private method that does all the work."

See also: