Java Reference
In-Depth Information
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class GenerateReportAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
Connection connection;
ServletOutputStream servletOutputStream =
response.getOutputStream();
InputStream reportStream = getServlet().getServletConfig()
.getServletContext().getResourceAsStream(
"/reports/DbReport.jasper");
/reports/DbReport.jasper");
response.setContentType("application/pdf");
"/reports/DbReport.jasper");
"application/pdf");
application/pdf");
Class.forName("com.mysql.jdbc.Driver");
"com.mysql.jdbc.Driver");
com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:
jdbc:mysql://localhost:
3306/flightstats?user=user&password=secret");
"jdbc:mysql://localhost:
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), connection);
connection.close();
servletOutputStream.flush();
servletOutputStream.close();
return mapping.getInputForward();
}
}
All action classes must extend the class org.apache.struts.action.Action .
Typically, the execute() method is overridden to implement custom logic for
servicing a request. As can be seen in the code above, the execute() method takes
an instance of HttpServletResponse as one of its parameters. This makes it easy to
write Action classes that generate reports.
The technique illustrated in the preceding example is not much different from what
we have seen in various earlier examples throughout the topic. In most examples, we
used standard Java Servlets to generate web reports, implementing the report
 
Search WWH ::




Custom Search