Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How To Get POST Data W/ Servlet?

Status
Not open for further replies.

beefeater267

Programmer
Apr 6, 2005
79
Hi,

I need to create a JAVA Servlet which just needs to capture HTTP POST data coming from another web application.

How do you retrieve HTTP POST data w/ a Java Servlet? For example, support I have a form w/ a text box named 'job'. When the data is posted via HTTP to the servlet, how can i get the value of 'job'?

thanks
 
Write a class that extends HttpServlet, and implement the method "doPost()".

Then within that method, write :

String job = request.getParameter("job");

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
What's the difference between the implementations of

doPost() and doGet()
 
Well, maybe its not as obvious as it seems ...

doPost() handles the HTTP POST instruction.

and doGet() handles the HTTP GET instruction.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
So, does: "doPost()" and "doGet()" always get executed? It seems confused b/c my servlet is not the application doing the posting. It is simply retreiving information that was posted to it.

I would have thought it would need to implement 'doGet()' because we are 'getting' the values that were posted to it.

So, anyways, if a browser was pointed to:

then, would i have to implement 'doGet()'?
 
You can just call doPost from doGet, that'd do the job.

Cheers,
Dian
 
Well lets get a couple of things straight.
First, your post asked how to handle an HTTP POST instruction. This is handled by the doPost() method, and GET is handled by doGet(), as I said before.

There are two methods, because some applications may only want to handle one or the other.
However, most applications put the actual logic in one method, and redirect to the other.

Eg :

Code:
doGet(HttpServletRequest request, HttpServletResponse response) {
  doPost(request, response);
}


doPost(HttpServletRequest request, HttpServletResponse response) {
  String job = request.getParameter("job");

}

This is probably how you should do it aswell, aslong as you don't care whether the client is sending a GET or POST instruction.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top