package examples.htmlkona;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import weblogic.db.jdbc.*;
import weblogic.html.*;
/**
* This servlet merges database data with an HTML formletter.
*
* @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved.
* @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved.
*/
public class DataMerge extends HttpServlet {
public synchronized void service(HttpServletRequest req, HttpServletResponse res)
throws IOException
{
Connection conn = null;
try {
res.setStatus(HttpServletResponse.SC_OK);
res.setContentType("text/html");
conn = defaults.login();
ServletPage hp = new ServletPage("DataMerge");
String file = defaults.fileloc() + "formletter.html";
QueryDataSet ds = new QueryDataSet(conn, "select ename, sal from emp");
ds.fetchRecords();
hp.getBodyElement()
.setAttribute(BodyElement.bgColor, HtmlColor.white);
hp.getBody()
.addElement(new HeadingElement("This database data:", 2))
.addElement(new LiteralElement(ds))
.addElement(new HeadingElement("Merged with this HTML document:", 2))
.addElement(new StringElement().setContentsWithFile(file))
.addElement(new HeadingElement("Produces these form letters:", 2));
for (int i = 0; i < ds.size(); i++) {
Record rec = ds.getRecord(i);
StringElement elem = new StringElement();
elem.setContentsWithFile(file)
.replace("$person", new BoldElement(rec.getValue("ename").asString()))
.replace("$amount", rec.getValue("sal").asString())
.replaceWithElement("Human Resources", "ItalicElement");
hp.getBody()
.addElement(elem)
.addElement(MarkupElement.HorizontalLine)
.addElement("Copyright 1996-2000 by BEA Systems, Inc. All Rights Reserved.");
}
hp.output(res.getOutputStream());
}
catch (Exception e) {
defaults.showException(e, res.getOutputStream());
}
// Always close the connection in a finally block.
finally {
try {conn.close();} catch (Exception e2) {;}
}
}
}