Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

use doGet & doPost Method

 
Abhijit mohan
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i use doGet & doPost Method in same class? please send me example programe.
thank you
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15276
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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" ...
 
shekar march chandra
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all right thanks, can you tell me what are the differences between doGet, doPost and Actionforward?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64830
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ActionForward is not part of servlets but of a proprietary framework (Struts, I believe).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic