gpt4 book ai didi

java - 调用 doPost() 中的方法而不减慢结果页面的速度

转载 作者:行者123 更新时间:2023-11-30 04:31:54 28 4
gpt4 key购买 nike

下面的index.html调用(下面的)servet的doPost来查看某个url是否可以用于拍卖。一切都运行得很好。但是,我希望在页面重定向之后从其他类执行两个附加方法。我的问题是其他两个附加方法需要很长时间才能完成,因此我不能将它们直接放在 response.sendRedirect 之后,因为它们会保留初始响应。

如何立即发送重定向(如下所示),然后调用需要在 doPost 中找到的相同字符串数据变量进行操作的其他两个方法?

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)
}

}

最佳答案

在单独的线程中执行这些方法。

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();
}
}

然后在你想要返回结果的方法中:

t.join();
String result = myResult;
...

对于 externalMethodTWO 也类似

关于java - 调用 doPost() 中的方法而不减慢结果页面的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14447845/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com