Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Using a Java Servlet to Generate Up-To-Date Microsoft Excel Spreadsheets  : Page 4

You don't have to automate Excel or use Jakarta to create simple Excel spreadsheets. Find out how to create them dynamically using a Java servlet.


advertisement
Seeing the Results in Excel
To see the SpreadSheetServlet servlet in action, all you have to do is access the servlet from a Web browser. Obviously, the machine that is accessing the servlet needs to have Microsoft Excel installed so that it can interpret the Excel content when the servlet responds.

The URL you use to access the servlet depends on the context root that you define for the project in your EAR application deployment descriptor (application.xml). Note the <context root> element in the code below:

<?xml version="1.0" encoding="UTF-8"?> <application id="Application_ID" version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"> <display-name> SpreadSheetEAR</display-name> <module id="WebModule_1145216557944"> <web> <web-uri>SpreadsheetWebProject.war</web-uri> <context-root> SpreadsheetWebProject</context-root> </web> </module> </application>

The URL also depends on the <servlet-mapping> tag you define in the Web deployment descriptor, web.xml, as specified by the <url-pattern> tag:

<servlet-mapping> <servlet-name>SpreadsheetServlet</servlet-name> <url-pattern>/SpreadsheetServlet</url-pattern> </servlet-mapping>

So using the example data above, you'd access the servlet using the address:



http://<yourhostname>:<yourappserverlisteningport> /SpreadSheetWebProject/SpreadSheetServlet

The servlet, displayed in Internet Explorer, is shown in Figure 3.

 
Figure 3. Sample Spreadsheet: The figure shows the Microsoft Excel spreadsheet produced by accessing the SpreadSheetServlet.
To make sure the Excel spreadsheets being produced by the servlet are truly up-to-date, you can issue an UPDATE statement using the DB2 command line processor to update the player's statistics.

In this article you've seen how to create simple spreadsheets in a Java servlet for consumption by a Web browser. Such a facility enables your Web site visitors to view current data from your database in a familiar format: a Microsoft Excel spreadsheet. You might use such dynamically-created spreadsheets to inform clients about current inventory, or to provide your accounting department with current database data.

If you've ever tried it, teaching a non-techie how to issue an SQL query can be quite a painful experience. Letting the bean counters in your enterprise do their bean counting using a familiar tool such as Microsoft Excel is a much more efficient approach.

This approach has its limitations, though. You do not have the fine-grained power to create spreadsheets like the ones you can produce using Apache Jakarta POI. If you need to create spreadsheets where you can manipulate fonts and specific fields, you'll be happier with an architecture that uses Apache POI on your application server. See the article "Learn to Read and Write Microsoft Excel Documents with Jakarta's POI" for more on the POI project.



Kulvir Singh Bhogal works as an IBM consultant, devising and implementing J2EE-centric solutions at customer sites across the nation.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap