hotohoto
hotohoto

Reputation: 487

How can I get JSP page as e-mail contents in Servlet code

I want to send an e-mail via Google or one of some other email Services in my Servlet code. Since the e-mail content is kind of dynamic - receiver's name or some other parts of it varies case by case, I want to use JSP page and JSTL features in it to generate the e-mail content.

How can I get JSP generated page content internally in my Servlet codes.

If possible I don't want to make any local HTTP connection to the e-mail content page. My web server environment would be Tomcat 6 and Servelt 2.5.

Upvotes: 0

Views: 176

Answers (2)

C.Champagne
C.Champagne

Reputation: 5489

I see several solutions:

1) Create your JSP normally, make a request to it from a java class and put the response content in your mail. (Found in SO here)

 URL urlPage = new URL(url);
 HttpURLConnection conn = (HttpURLConnection)urlPage.openConnection();
 conn.connect();
 BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));

 //then loop through lines of webpage with br.readLine();
 //and add it to your mail to send

2) You could inject a custom implementation of JspWriter redirectiong all the output to a file (or a Reader...). An easy (but a bit dirty) implementation of this could be: In your JSP:

<%@page import="foo.bar.JspFileWriter"%>
<% 
   String fileName = "JspStartContent" + System.currentTimeMillis() + ".html";
   out = new JspFileWriter(new File("c:\\Mobile", fileName), out); 

%>

Having the following implementation of JspWriter

public class JspFileWriter extends JspWriter {

BufferedWriter out;
JspWriter originalOut;
/**
 * @param bufferSize
 * @param autoFlush
 */
public JspFileWriter(File file, JspWriter originalOut) {
    super(originalOut.getBufferSize(), originalOut.isAutoFlush());
    try {
        this.originalOut = originalOut;
        FileWriter fw = new FileWriter(file);
        out = new BufferedWriter(fw); 
    } catch (IOException ex) {
        ;
    }


/**
 * @see javax.servlet.jsp.JspWriter#clear()
 */
@Override
public void clear() throws IOException {
    originalOut.clear();

}

/**
 * @see javax.servlet.jsp.JspWriter#clearBuffer()
 */
@Override
public void clearBuffer() throws IOException {
    originalOut.clearBuffer();

}

/**
 * @see javax.servlet.jsp.JspWriter#close()
 */
@Override
public void close() throws IOException {
    originalOut.close();
    out.close();

}
    ...

3) This is not an answer to your question but you could consider XSLT to generate your HTML.

Upvotes: 1

Christopher Schultz
Christopher Schultz

Reputation: 20862

Using JSP is often very awkward for this kind of thing. Whenever I have to dynamically-generate content for anything other than the web, I use a different framework entirely. I'm kind of partial to Apache Velocity but there are a few others like FreeMarker. I'm sure there are others.

I find a separate framework more portable, reliable, and easier to work with than JSP.

Upvotes: 1

Related Questions