wood burning stoves*
The moose likes Servlets and the fly likes  use doGet & doPost Method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark " use doGet & doPost Method " Watch " use doGet & doPost Method " New topic

use doGet & doPost Method

Abhijit mohan

Joined: Aug 22, 2005
Posts: 1
how can i use doGet & doPost Method in same class? please send me example programe.
thank you
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

What do you mean?

If you're writing a servlet, you can just implement a doGet() as well as a doPost() method in it.

In an HTML form, you can specify what HTTP method should be used to send the form to the server by using the "method" attribute on the <form> element:

<form method="post" ...


<for method="get" ...

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
shekar march chandra

Joined: Jul 07, 2005
Posts: 25
Hi Abhijith,

Yes, It is possible to use doGet()as well as doPost() in a single class.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Test extends HttpServlet throws IOException, ServletException
public void doGet(HttpServletRequest req, HttpServletResponse res)
} /* end of doget()*/

public void doPost(HttpservletRequest req, HttpServletResponse res)
String name=req.getParameter("username");
PrintWriter out= res.getWriter();
} /*end of dopost*/

} /* end of class */

In your html file the declaration will be like this

form method="Get" action="dosomething".

By looking at get request, the servlet engine will call appropriate class as mapped in web.xml and invokes doget() method, which inturns calls dopost()method.

I think your doubt is clear. If not reply back .

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Originally posted by shekar march chandra:
Yes, It is possible to use doGet()as well as doPost() in a single class.

But don't be fooled by the code sample, it is possible and often even preferable for the doGet and doPost methods to do different things...
Ma Yao

Joined: Sep 13, 2011
Posts: 14
all right thanks, can you tell me what are the differences between doGet, doPost and Actionforward?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60804

ActionForward is not part of servlets but of a proprietary framework (Struts, I believe).

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
It is sorta covered in the JavaRanch Style Guide.
subject: use doGet & doPost Method
Similar Threads
is servlet doGet and doPost method are threadsafe?
Dont want the control to return to the servlet and execute after the forward
service() in HttpServlet
Trying to access a servlet on button click