如何防止HTTP请求超时,JSP?

时间:2011-04-02 05:53:32

标签: java jsp

您好我该如何防止http请求超时?

非常感谢

2 个答案:

答案 0 :(得分:1)

因此,您希望防止Web浏览器超时运行长时间的HTTP请求吗?

您需要将某些内容写入响应,即使它只是对进度的简单可视化。或者在后台线程中执行长时间运行的任务,而不是HTTP请求线程。或者优化长时间运行的任务,它不会长时间运行。


更新:根据评论,您可以在ExecutorService的帮助下创建一个后台线程,并将长时间运行的任务提交给它。

public class LongRunningTaskServlet extends HttpServlet {

    private ExecutorService executor;

    @Override
    public void init() {
        executor = Executors.newSingleThreadExecutor();
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        // First gather necessary parameters for the background task (you shouldn't pass request/response to it!).
        String param1 = request.getParameter("param1");
        String param2 = request.getParameter("param2");

        // Then create the background task and submit it to the executor.
        executors.submit(new LongRunningTask(param1, param2));
        // It returns a Future, you can if necessary store it in session so that you can later check if it is finished and/or has returned a result.

        // Immediately display result.
        request.getRequestDispatcher("/WEB-INF/result.jsp").forward(request, response);
    }

    @Override
    public void destroy() {
        executor.shutdownNow(); // Important! Pevent thread leaking.
    }

}

LongRunningTask课程必须实施Callable<T>Runnable。请注意线程,不要删除上面示例中的destroy()

另见:

答案 1 :(得分:0)

问题不是很清楚。虽然你的意思是,但会议不会超时。然后在web.xml中将session-timeout设置为0或更小。

<session-config>
    <session-timeout>-1</session-timeout>
</session-config>