如何将用户重定向到页面,然后 servlet 执行方法



我有一个JSP页面,用户在其中填写表单。

servlet 获取用户输入,进行一些计算,发送一些电子邮件,然后将用户重定向到另一个页面。

问题是"发送电子邮件"部分持续几秒钟,因此用户从提交表单的那一刻起要等待很多时间,直到显示新页面。

在 servlet 中,顺序是:

//...
//servlet gets the user input and store all the info to an object `MyObject` filesToAttach
response.sendRedirect(destination); // show the new page to the user..
sendEmail(filesToAttach); //method that sends the emails
//...

尽管sendRedirect是按顺序排列的,但在sendEmail方法完成之前,新页面不会向用户显示......

我可以使用一个ServletContextListener,例如每 10 分钟运行一次,并检查是否有要发送电子邮件。

但是,有没有办法向用户显示新页面,然后 servlet 继续发送电子邮件?

简单的答案:使用专用ThreadExecutor异步发送电子邮件。

相关内容

最新更新