Sample Application: ReportGeneration

 

This is an example of a full working JSP application using JSP and Servlet. Generating reports of data in the web page is a requirement facing by developers very often. The purpose of this example is to demonstrate how to make reports (excel and pdf) on data from a JSP page (which may be coming from underlying database in most cases).The task is fairly simple requiring only one servlet and the JSP files of which the report is to be made. The reports will be stored in "sample.xls" or "sample.pdf" in your local system. The table put here is just a sample and it can be extended according to your requirement.

You can download the source of this Sample Application here

This is illustrated based on the assumption that you are you're already familiar with JSP, Servlet technologies. and equipped with a Servlet-capable Web server.

We will be generating a report on the content of the web page which may be an excel or a PDF based on your requirement. There is no administration interface. We will be covering a few programming topics that involve JSP and Servlet such as setting the content type and header of the web page of which the report is to be made.

Apache Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and JavaServer Pages technologies. You can download the latest version of Apache Tomcat from here

/webapps/

/webapps/ReportGeneration/

/webapps/ReportGeneration/web/

/webapps/ReportGeneration/web/index.jsp

/webapps/ReportGeneration/web/ExcelReport.jsp

/webapps/ReportGeneration/web/PDFReport.jsp

/webapps/ReportGeneration/web/META-INF/context.xml

/webapps/ReportGeneration/web/WEB-INF/web.xml

/webapps/ReportGeneration/src/

/webapps/ReportGeneration/src/java/

/webapps/ReportGeneration/src/java/ReportGenerationServlet.java

 

Lets go over each one:

/webapps/

 

The /webapps/ directory is our web server document root.

/webapps/ReportGeneration/

 

/ReportGeneration/ is the subdirectory where our application is accessed by the browser.

/webapps/ReportGeneration/web

 

/web/ is the subdirectory where our JSP files and xml files have been kept.

/webapps/ReportGeneration/web/index.jsp

 

index.jsp will be the entry point of our application. The web browser will be accessing this via http://yourip/localhost:8080/ReportGeneration.

/webapps/ReportGeneration/web/ExcelReport.jsp

 

ExcelReport.jsp is the jsp file whose content is generated to excel file. The content type for this jsp should be "application/vnd.ms-excel".

/webapps/ReportGeneration/web/PDFReport.jsp

 

PDFReport.jsp is the jsp file whose content is generated to excel file. The content type for this jsp should be "application/pdf".

/webapps/ReportGeneration/web/META-INF/context.xml

 

context.xml will specify the context path to our project. In our case context path is "/ReportGeneration"

/webapps/ReportGeneration/web/WEB-INF/web.xml

 

web.xml acts as the deployment descriptor for our application. It is held in the application's WEB-INF directory. It defines a number of parameters that are used when the web application is deployed into the Tomcat Servlet/JSP container.

/webapps/ReportGeneration/src/

 

This folder holds all the source of our web application.

/webapps/ReportGeneration/src/java/

 

/java/This folder holds all the java files necessary for our application. Servlets are also stored in this directory. You can also store the files in a package structure.

/webapps/ReportGeneration/src/java/ReportGenerationServlet.java

 

/ReportGenerationServlet.java is the servlet for generating reports. This servlet sets the content type and header for jsp files which is necessary for generating reports on the particular pages.

 
Contact Us