0

i am working on a Struts2 web application and currently have a task of sending automatic mail in CSV, EXCEL, PDF format. I started up with Timer api to send automatic mail (which can schedule tasks for future execution).

All formats are ready, But i'm getting error while sending mails as the HttpServletRequest object gives me null value when inside run () method.

HttpServletRequest request = ServletActionContext.getRequest();

I need to get the request object to get the language and session while sending mails. Here is my class (i am using google guice for injecting service)

public class MailTimerTask extends TimerTask{

private final Utility mails;
private final MailService mailsrv;

private MailTimerTaskBean taskbean = new MailTimerTaskBean();

@Inject
MailTimerTask(Utility mails, MailService mailsrv) {
  this.mails= mails;
  this.mailsrv= mailsrv;
}

public void run() {
   System.out.println("Mails-- Start");
   mailList();
   System.out.println("Mails-- Finished");
}

   public void mailList() {
ByteArrayOutputStream outputStream = null;

    HttpServletRequest request = ServletActionContext.getRequest(); // ERROR request object

  for (Customer customer : taskbean.getPdfBean().getListCustomers()) {
   outputStream = crt.generateOutputStream(taskbean, customer.getIdCustomer());
    if (outputStream != null) {
    MailBean mailBean = setMailBean(request, customer, outputStream, taskbean.getSite());
            mailsrv.sentMail(mailBean);
         }
     }
 }

  public MailTimerTaskBean getMailtimertaskbean() {
   return taskbean;
   }

 public void setMailtimertaskbean(MailTimerTaskBean mailtimertaskbean) {
this.taskbean = mailtimertaskbean;
  }

I have also tried with pass request object in a Bean (in this case taskbean), by doing that i get the request object but the session is null

Do not know why the request object is getting null ? using ServletActionContext

Also it would be helpful if anyone could suggest me other api's for sending automatic mails ?

thank you.

techGaurdian
  • 732
  • 1
  • 14
  • 35
  • Read this : http://stackoverflow.com/questions/4922015/servletactioncontext-getrequest-returns-null – Juned Ahsan Jun 18 '14 at 05:23
  • yes. means i cannot use ServletActionContext here ?. now i have implemented ServletRequestAware and setting the request from previous page. but i'm getting session null. sorry but i'm not getting it. – techGaurdian Jun 18 '14 at 07:17
  • If this is automatic task about which request and session are you talking? ;) – Aleksandr M Jun 18 '14 at 08:47
  • actually i have used free marker temple for the body part of mails in two different languages etc English and Germany and the template folder is in web dir. so every time while sending before it sends mail it checks the language through request and finds the Folder path (that contains Free marker template) through session using request.getSession().getServletContext().getRealPath(MAIL). (i created a generic class used for sending mails with template) – techGaurdian Jun 18 '14 at 09:26

0 Answers0