2

Currently my Servlet receives data from a form on a web page via a POST request.

I need to perform a variety of actions with the data received but the client doesn't need to wait for this processing to finish. I am not sending anything back.

The way it works now is the client continually waits until an HTTP response is sent but that doesn't happen until doPost() has completed. But I use doPost() to store the data & trigger actions on that data.

I could simply store the data with doPost() but then how do I trigger an action on that data outside of doPost()?

Kyle Anderson
  • 6,801
  • 1
  • 29
  • 41
  • do you want to send client to next page and let processing occur in background? As right your client may get stucked as your thread is waiting in post to complete processing. – Abhijeet Panwar Jul 09 '14 at 17:15
  • I'm using jQuery to make the call and in this case I don't need to redirect anywhere. Just remain on the same page. As I build this web app out I may have other cases where I will redirect to another page. – Kyle Anderson Jul 09 '14 at 17:27
  • When your form submits data to servlet, processing occurs at background in dopost, So what's problem in that if you want to keep client on same page , he will be on same page untill processing in dopost completes. I am not getting that what do you exactly want? – Abhijeet Panwar Jul 09 '14 at 17:33
  • Depending on the client, it could timeout. For instance a Java client application makes a POST request to a servlet at a URL. If a response is not sent within a certain threshold an error message is sent due to a timeout. – Kyle Anderson Jul 09 '14 at 17:35
  • I want to save the parameter values in variables, send a response and then perform processing with those variables afterwards. I need to make a variety of API calls and pass those values as parameters, etc. – Kyle Anderson Jul 09 '14 at 17:36
  • So you can save those variables in session, and later you can fetch them from session when you need to do process on them. – Abhijeet Panwar Jul 09 '14 at 17:38
  • How does that work exactly. I save the values as variables. How do I trigger the fetch afterwards? I don't want to do it on a schedule. It should happen right away. – Kyle Anderson Jul 09 '14 at 17:41
  • Explaining it in answer :) – Abhijeet Panwar Jul 09 '14 at 17:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57044/discussion-between-abhijeet-and-punkrocker27ka). – Abhijeet Panwar Jul 09 '14 at 18:11

4 Answers4

1

A simple answer would be Threads. This post is helpful.

Save the parameter values as variables within the doPost() method. Then put all of the long running operations in a new Thread. The doPost() method will complete and send an HTTP response while the second Thread takes however long it needs to finish.

doPost():

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String x = request.getParameter("x");
    String y = request.getParameter("y");

    PostProcessing pp = new PostProcessing(x, y);
    Thread t = new Thread(pp);
    t.start();

}

New Thread:

public class PostProcessing implements Runnable{

private String x;
private String y;

public PostProcessing(String x, String y) {
    this.x = x;
    this.y = y;
}

@Override
public void run() {

    x.doSomething();
    y.doSomethingelse();

    /*LONG RUNNING OPERATIONS GO HERE*/
}

}
Community
  • 1
  • 1
Kyle Anderson
  • 6,801
  • 1
  • 29
  • 41
0

You can use

request.getRequestDispatcher("./xy.jsp/html").forward(request, response);

The caller will depart and you are free for long running doPost()

edit: I'm sorry, that's right, i forgot that even the requestDispatcher will only fired after completition. Than the only other method would be to start a new task/callable/runable/thread and delegate the work so that the servelet can return.

0

As far as I have understood by discussion in comments, you want to save values in variables and want to process them later. So for the same purpose we use session variables.SO what you can do is when your servlet recives those parameters.Save then in session using :

HttpSession session = request.getSession(). //gives you a session object

String uname=request.getparameter("uname); //uname  is value you want to save for processing later.

session.setAttribute("username",name); //So sets this variable to session 

Later whenver you want to use this variable You can get it by getting session and then fetch it my passing it's key to session object.

HttpSession session = request.getSession().
String username = (String)session.getAttribute("uname"); //your name variable was saved with key name uname, session.getattribute will return object, so We are converting it back to String 

Now whatever process you want to do on your uname variable,you can fetch that using your session and can do process on it.

For furthur clarification about session and how to use it , you can follow this simple article: Using Session

Abhijeet Panwar
  • 1,837
  • 3
  • 26
  • 48
  • But how am i triggering the processing after doPost()? I don't need a session necessarily. I just need to trigger a method after doPost() so that the response is sent back to the client in a timely manner & my processing begins after the response is sent. – Kyle Anderson Jul 09 '14 at 18:03
0

Do you need to try and do everything from the Servlet?

You could publish the incoming data on a JMS Queue and return control to the caller once that is completed.

Then you could have a different class read the data from the JMS Queue and process it asynchronously.

Mike Barlotta
  • 715
  • 4
  • 16