简体   繁体   English

关闭servlet中的http连接后执行作业

[英]Performing a job after closing the http-connection in a servlet

I want a user to make a post, the data in the post is handled and the request is forwarded to a jsp-page. 我希望用户发表文章,处理文章中的数据并将请求转发到jsp页。 The jsp-page is sent back to the user but then the server should do a job. 将jsp页发送回用户,但服务器应执行一项工作。 This job takes a lot of time so the user shouldn't wait for it. 这项工作需要很多时间,因此用户不应该等待它。

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
  //... get posted data ...
  request.getRequestDispatcher("/page.jsp").forward(request, response);
  //do-someting
}

I want the "do-something" to be done after the connection is closed and the user gets the page.jsp. 我希望在关闭连接并获得page.jsp之后执行“做某事”。 Of course the "do-something" is completely serverside like updating a database etc. So the server keeps working even after the user leaves the page. 当然,“做某事”完全是在服务器端进行的,例如更新数据库等。因此,即使用户离开页面,服务器也可以继续工作。 I tried to flush and close the outputstream in the servlet after the forward to the jsp-page but this results in an exception. 在尝试转到jsp页面之后,我尝试刷新并关闭servlet中的输出流,但这会导致异常。 Also flushing and closing in the jsp-page results in an exception. 同样,刷新和关闭jsp页也会导致异常。 How can I do this? 我怎样才能做到这一点?

If your job (do-something) is completely independent of what should be displayed in the jsp, then you can use one of the following: 如果您的工作(做某事)完全独立于jsp中应显示的内容,则可以使用以下方法之一:

  • A timer 计时器
  • A thread (in a thread pool) 线程(在线程池中)
  • A more powerful library like Quartz which will give you powerful scheduling capabilities 一个更强大的类库像石英 ,这将给你强大的调度功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM