如何在每次迭代后发送成功消息

时间:2017-05-31 10:26:26

标签: java ajax jsp servlets

我正在循环发送电子邮件并再次迭代该循环。目前,在使用

发送所有电子邮件后,将显示成功消息
request.getSession().setAttribute("message", resultMessage);
response.sendRedirect("index.jsp");

我想在jsp页面上每次成功迭代后显示成功消息。

servlet类中的迭代代码:

try {               
    for(int j=0;j<noOfEmailToSend;j++) {
        Iterator<String> sendersInputIterate = list.iterator();
        Iterator<String> subject1 = sub.iterator();

        while (sendersInputIterate.hasNext() && subject1.hasNext()) {   
            EmailFunction.sendEmail(ExchangeIP, port, sendersInputIterate.next(), toAddress, subject1.next(), content, uploadedFiles);  
            resultMessage = "The B-mail has been sent successfully :";
            Thread.sleep(sleeptime);
        }

        Thread.sleep(iterationInterval);
    }                                           
} catch (Exception ex) {
    ex.printStackTrace();               
    resultMessage = "There were an error: " + ex.getMessage();
} finally {
    request.getSession().setAttribute("message", resultMessage);
    response.sendRedirect("index.jsp");
}

请在每次迭代后建议如何显示此消息。

1 个答案:

答案 0 :(得分:1)

有一个快速的解决方案,它是:而不是在servlet中创建循环, 在客户端进行循环以执行ajax请求,将您的电子邮件放在javascript数组中然后通过循环迭代数组然后将它们逐个发送到servlet,如果电子邮件成功发送或出现故障,您可以检测天气,servlet每个请求可以发送1封电子邮件 并且输出流可以按请求打印一次

这是一个例子:

var emails=new Array();
emails.push("foo@foo.com");
emails.push("bar@bar.com");
emails.push("foo@bar.com");
$(document).ready(function(){
// first param is the url which will you send the param
//second param the email which will you send and in this case you have to pull it by request.getParameter("email");
third param the response which you got from servlet weather success or fail
for(i=0;i<emails.size();i++){
$.post("SendEmailServlet",{email: emails[i]},function(data){
 // third param the response from servlet
})
})
}