File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark " use doGet & doPost Method " Watch " use doGet & doPost Method " New topic
Author

use doGet & doPost Method

Abhijit mohan
Greenhorn

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: 14430
    
  23

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" ...

or

<for method="get" ...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
shekar march chandra
Greenhorn

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)
{
doPost(req,res);
} /* end of doget()*/

public void doPost(HttpservletRequest req, HttpServletResponse res)
{
String name=req.getParameter("username");
res.setContentType("text/html");
PrintWriter out= res.getWriter();
out.println(name):
} /*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 .

Thanks,
Chandra
David O'Meara
Rancher

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
Greenhorn

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
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

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