Reputation: 18884
The following index.html calls the (below) servet’s doPost to see if a url is available on auction. Everything runs great. However, I am looking to execute two additional methods from other classes after the page redirect. My problem is the other two additional methods take a long time to finish so I can’t put them directly after the response.sendRedirect as they hold up the initial response.
How can I send the redirect immediately (exactly as below)while then calling the other two methods which need the same String data variable found in doPost to operate?
index.html
<html>
<head>
<title>URL Auction Search Page</title>
</head>
<body>
<CENTER>
<FORM ACTION="/ResultServlet/Results" METHOD=GET>
<INPUT TYPE=TEXT NAME="st">
<INPUT TYPE=SUBMIT VALUE=Submit>
</FORM>
</CENTER>
</body>
</html>
Servlet
@WebServlet("/Results")
public class Results extends HttpServlet {
private static final long serialVersionUID = 1L;
public static String str="";
private String businessLogic(String q){
try {
str = new compute.URL.GetAvailURI( "https://www.registerdomains.com/auctionAPI/Key:a05u3***1F2r6Z&urlSearch="+q);
/*more boring number crunching */
return str;
}
/*
protected void doGet(HttpServletRequest request, HttpServletResponse response)
}
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Results r = new Results();
String st = request.getParameter("st");
String data = r.businessLogic(st);
response.sendRedirect("results/resultActionURL.html?st="+data);
//IDEALLY I WOULD LIKE TO CALL outsideMethod1(data) and outsideMethodTWO(data) HERE
//BUT IT TAKES TOO LONG. HOW CAN I RUN THEM W/O SLOWING DOWN THE RESPONSE
//(LIKE A ProcessBuilder call to a shell for example where there's almost a handoff)
}
}
Upvotes: 1
Views: 781
Reputation: 5514
Execute those methods in a separate thread.
public class Results extends HttpServlet{
...
private Thread t;
private volatile String myResult;
...
protected void doPost(...){
...
t = new Thread(){
public void run(){
myResult = outsideMethod1(data);
}
}
t.start();
}
}
Then in the method where you want to get the result back:
t.join();
String result = myResult;
...
and similarly for outsideMethodTWO
Upvotes: 2