wood burning stoves 2.0*
The moose likes Servlets and the fly likes Multiple  response for a request Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Multiple  response for a request" Watch "Multiple  response for a request" New topic
Author

Multiple response for a request

mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
Hello Friends,
The application I developed works as follows.
1. It(Servlet) accepts a request from the client(JSP or HTML)
2. Passes the request to another component and gets the response from the component.
Here,their are two cases
a. The component may return the response to the servlet immediately, and the servlets sends the response to the client.
b. THe component may be busy processing another request, hence may ask the servlet to wait for some minutes, if so then the servlet should send an acknowledge to the client that " You are in queue and will be given a response after a few minutes"
and wait for the response from the component. After a few minutes when it gets the response from the component it should establish connection to the client and send the response.
Now the issues are
1. Since the number of requests are high it is not possible to make the client wait for the response(as the session may expire), hence I should the store the cleint details and establish a connenction with the client when the servlet is ready with the response. how is it possible.
2. How can I store the client details(infact, what are the details to be stored) and establish a connection from the server.
3. I have to pass multiple response for the client's request(1.Acknowledge 2.The actual response).And this should be done after a interval of time. Using servlets it is not possible to send multiple responses, so how can I achieve it.

Instead of using servlets is their any alternative throug which I can accomplish this.
Hoping a reply at the earliest
Mohamed Zafer
Mirko Froehlich
Ranch Hand

Joined: Aug 21, 2000
Posts: 114
I'll answer question number 3 first: There is no way to have more than one response for a single request, neither using Servlets nor using any other server-side technology. This is simply how HTTP works. There is also no way for the Servlet to initiate a connection to the client; the client has to make an HTTP request to your server that your server can respond to. What you can do is, as you already suggested, store the client information in the session (this answers question 2), and look for this data the next time a request comes in. Basically, this is how it would work:
1. Client makes an HTTP request to your Servlet
2. Servlet realizes it will need to do some time-intensive processing
2.a. Servlet stores a flag in the session, which indicates that the processing has been initiated but not finished
2.b. Servlet sends HTTP response back to client. The response mainly contains a message along the lines of "Still processing; please refresh this page in a few seconds".
2.c. Servlet creates a new thread to do the time-intensive processing
3. Client gets the response; after a few seconds the user refreshes the page, which sends a new HTTP request to the Servlet
4. The Servlet looks at the flag in the session and realizes that it has already started (and possibly finished) processing everything. It tries to join the processing thread. If this is successful, the processing has been finished and the Servlet can send the appropriate response to the client. Otherwise, it will send the "Still processing" response again and continue processing in the separate thread.
If you don't know how long the processing is going to take (i.e. sometimes it might take too long, but other times it will finish very quickly), you can define a timeout in the application (e.g. 20 seconds). In step 2, the Servlet can then immediately start the new processing thread and try to join it with the desired timeout. If this is successful, the processing time was short enough and the correct response can be directly sent back to the client. Otherwise, proceed as outlined above.
If you don't want the user to have to manually refresh the browser page, you can also use a META-REFRESH tag to automatically refresh the page every few seconds. A lot of e-commerce websites do this while the credit card information is being processed.
I hope this helps.
-Mirko

Originally posted by mohamed zafer:
Hello Friends,
The application I developed works as follows.
1. It(Servlet) accepts a request from the client(JSP or HTML)
2. Passes the request to another component and gets the response from the component.
Here,their are two cases
a. The component may return the response to the servlet immediately, and the servlets sends the response to the client.
b. THe component may be busy processing another request, hence may ask the servlet to wait for some minutes, if so then the servlet should send an acknowledge to the client that " You are in queue and will be given a response after a few minutes"
and wait for the response from the component. After a few minutes when it gets the response from the component it should establish connection to the client and send the response.
Now the issues are
1. Since the number of requests are high it is not possible to make the client wait for the response(as the session may expire), hence I should the store the cleint details and establish a connenction with the client when the servlet is ready with the response. how is it possible.
2. How can I store the client details(infact, what are the details to be stored) and establish a connection from the server.
3. I have to pass multiple response for the client's request(1.Acknowledge 2.The actual response).And this should be done after a interval of time. Using servlets it is not possible to send multiple responses, so how can I achieve it.

Instead of using servlets is their any alternative throug which I can accomplish this.
Hoping a reply at the earliest
Mohamed Zafer

 
Consider Paul's rocket mass heater.
 
subject: Multiple response for a request
 
Similar Threads
long post IBM.158
How to handle an asynchronized scenario?
Multiple URLConnections
RequestDispatcher's forward method does not work.
Database Lock