我有一个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 继续发送电子邮件?
简单的答案:使用专用Thread
或Executor
异步发送电子邮件。