Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

use doGet & doPost Method

 
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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" ...
 
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
 
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...
 
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?
 
Marshal
Posts: 67418
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic