aspose file tools*
The moose likes Servlets and the fly likes Two requests in one doPost method, can this happen? Please I need help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Two requests in one doPost method, can this happen? Please I need help" Watch "Two requests in one doPost method, can this happen? Please I need help" New topic
Author

Two requests in one doPost method, can this happen? Please I need help

Mouza Ali
Ranch Hand

Joined: Oct 28, 2008
Posts: 39
Hi,

Please I need someone to clarify this point for me as I googled for long time and didn't find any satisfactory answer.

I have a client that sends requests to bank servlet that should process the xml file and send it back to the client. Then I need that client again to send another XML file to the same bank servlet where it will process it again and send it back to the client. Is that possible?

I mean can the servlet recieves two different requests in one doPost() method? if so, how it will be able to distinguish b/w them so it will respond appropriately!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Mouza Ali:
I mean can the servlet recieves two different requests in one doPost()


possible


Originally posted by Mouza Ali:
if so, how it will be able to distinguish b/w them so it will respond appropriately!


you can do it in many ways like, hiddenfield,querystring ..etc
Mouza Ali
Ranch Hand

Joined: Oct 28, 2008
Posts: 39
Thanks for your reply,

You mean that I pass a string to the servlet, like for example "Request1" or "Request2" and check over there if it is == "Request1" go to some method; otherwise, go to the other one?

Is this effecient?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

yes. you need to check that which purpose your are sending the request to the servlet
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
I'm not sure what seetharaman venkatasamy means by "possible", but with HTTP 1 request means 1 response. You can't somehow deal with two successive requests at the same time in the doPost method.

What is the problem you are trying to solve? Why would it be problematic to handle two successive requests?


Ping & DNS - my free Android networking tools app
Mouza Ali
Ranch Hand

Joined: Oct 28, 2008
Posts: 39
Hi Ulf,

I mean that the servlet can handle two types of requests. I don't mean two requests that are the same and at the same time. For example, The servlet can recieve a request that asks to do addition and another request that asks to do subtraction but not at the same time.

For more explanation, when the first request is sent to the servlet, the first part of teh doPost() will do the addition and send the result back to the client, then the client take the result,process it and send it back to the servlet to do the subtraction. Finally, the servlet send the result back again to the client.

Hope you got what I mean, so do you have any suggestions, or using a string query is the only solution?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
I see. In that case, don't use a query string; use a hidden input field in your HTML form.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Ulf Dittmer:
I'm not sure what seetharaman venkatasamy means by "possible", handle two


hi, i mean instead of using multiple servlet for different functionality ...still you can use one servlet.


something like, in servlet

Mouza Ali
Ranch Hand

Joined: Oct 28, 2008
Posts: 39
Hi,

This will be done on the servlet side, but how to control this on the client side? I mean what to insert into the code to let the paramter be specified to be either for the first request or for the second request?

For more clarification, you said that in the servlet side, I should use the getParamter method, but how to set it in the first place on the client side?
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
You set it up as "Request1" by default, then during first elaboration you let your servlet set it as "Request2" and set it back in the form.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Mouza Ali:
For more clarification, you said that in the servlet side, I should use the getParamter method, but how to set it in the first place on the client side?


As Ulf mentioned above use hidden field .



ofcourse you need to do some logic that prevent the null pointer exception in jsp
Mouza Ali
Ranch Hand

Joined: Oct 28, 2008
Posts: 39
Hi again,

I'm using neither jsp nor html form. I'm using the applet as client embadded as a tag in the html form. Is there anything similar but with Applet?

I'll appreciate your help!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Ah, you never mentioned that the client was not a browser but an applet. With applets it would just be an extra HTTP parameter. How is the applet sending the data now? It must have some mechanism of assembling the various HTTP parameters already, no?
Mouza Ali
Ranch Hand

Joined: Oct 28, 2008
Posts: 39
Hello,

Sorry. I thought I did, but it appears that I mentioned that in another post, oops!

The applet is using HTTP doPOST() method, when the client generates XML file, it will open a URL connection with the servlet, then it will open a stream to send the first XML file. Then, when the servlet receives it, it will process it again to send it back to the client by opening another stream.

When the client receives this XML file, it will generate another XML file to be sent again to the servlet, and so on..

Several xml files to be sent.

So can you give me more clarification about the paramters you mentioned? I googled for a very long time, and sometimes, I found that a service() method should be used, but not much explanation and documentation about that!

Can you help?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Here's an example of how to POST parameters in Java: http://www.exampledepot.com/egs/java.net/Post.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Two requests in one doPost method, can this happen? Please I need help