File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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: 15093

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 8 API documentation
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 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: 63866

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

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: use doGet & doPost Method
It's not a secret anymore!