Search code examples
javajspemailstruts2ognl

Sending value from action to the jsp which is not redirected from the action


I am using JSP page to send as email template from my action class

that is aaa.jsp accept email_id from user then redirect to Mail action for sending hashcode to user then redirected to yyy.jsp to enter the hashcode sent to emil_id provided by user. I use xxx.jsp as email template.

my xxx.jsp is

<html>
  <head>
 </head>
 <body>
  <s:property value="hashcode"/>
 </body>
</html>

I have an action class that sends an email to the user with an authentication code for the registration purpose.

checkAndMail is used to check whether this mail is registered already and convert the xxx.jsp page to string

public String checkAndMail() throws Exception{
this.hashcode="9048075352";//getters and setters are used for hashxode and is of type String
/***********
   code for searching whether the email is registered already
************/

    HttpServletResponse response = ServletActionContext.getResponse();
    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response) {
    private final StringWriter sw = new StringWriter();

    @Override
    public PrintWriter getWriter() throws IOException {
        return new PrintWriter(sw);
    }

    @Override
    public String toString() {
        return sw.toString();
    }
    };
    setHashcode(hashcode);
       request.getRequestDispatcher("aa/bb/xxx.jsp").include(request,
        responseWrapper);
    String result1 = responseWrapper.toString();
    sendMailNew("[email protected]", result1,"AssignMail" );
   TARGET = "success";

} 

I call sendMailNew from checkAndMail for sending mail. My sendMailNew method which is used for sending mail is

public synchronized void sendMailNew(String mailTo,String Content,String type) throws Exception 
{
    String TEXT="";  
    Properties props = new Properties();
    props.put("mail.smtp.host", HOST);
    props.put("mail.smtp.user", USER);
    props.put("mail.smtp.auth", AUTH);
    props.put("mail.smtp.starttls.enable", STARTTLS);
    props.put("mail.smtp.debug", DEBUG);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.socketFactory.fallback", "false");

    try {
            Session session = Session.getDefaultInstance(props, null);
            session.setDebug(true);
            MimeMessage message = new MimeMessage(session);

            //message.setText(TEXT);
      if(type.equals("AssignMail"))
    {
            message.setContent(Content, "text/html");

    }
            message.setSubject(SUBJECT);
            message.setFrom(new InternetAddress(FROM));
            message.addRecipients(RecipientType.TO, mailTo);
            message.saveChanges();
            Transport transport = session.getTransport("smtp");
            transport.connect(HOST, USER, PASSWORD);
            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
    } catch (Exception e) {
        throw new Exception("Exception in Mail.sendMail" + e.getMessage());
    }
}

My question is how to pass a value (here parameter is hashcode) from checkAdnMail to xxx.jsp

this action class is redirected to yyy.jsp for entering the hashcode that should be sent to the user's email address.

Reference: Struts2 - How can I get the result of a JSP page as a string in an action class (for emails)


Solution

  • You can pass a value using a request attribute. For example

    request.setAttribute("hashcode", hashcode);
    

    then in JSP you have to reference a request object in the struts tag or just use JSP EL

    <s:property value="#request.hashcode"/> 
    

    or

    ${hashcode}
    

    The last value is not escaped, be cautious if you have wrong characters in the code.