1

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

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

索引.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>

小服务程序

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

}
4

1 回答 1

2

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

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;
...

同样对于外部方法二

于 2013-01-21T22:03:33.140 回答