Solved

Problem creating servlet as a subclass!

Posted on 2006-10-25
3
373 Views
Last Modified: 2013-11-24
I just created a java class that creates an excel document.
It has a function called saveToFile which saves the excel document to a file.

Now I also want to be able to "download" the document just created, I know I could create another class (a servlet) setting the content type to "application/vnd.ms-excel" and then download the file... But! I would like to do this in a single file (not create another file for the servlet), so I thought about creating a servlet subclass. But then I don't know how I can call the subclass from the other class.    Any idea how I can achieve my goal (either by servlet subclass or anything else....) ??

Am I explaining my problem well enough?  
 
thanks
0
Comment
Question by:nediam1234
  • 2
3 Comments
 
LVL 9

Expert Comment

by:shinobun
ID: 17806982
Subclassing will also introduce a new class.  Why not just create a new Servlet class?  I think that would be more maintainable.
0
 

Author Comment

by:nediam1234
ID: 17807120
thanks for a quick reply,

When I save my excel sheet to a file I use something like:

ExcelSheet sheet = new ExcelSheet();
sheet.someFunctionToCreateTheDocument();
sheet.someAnotherFunctionToCreateMoreOfTheDocument();

Then, I can call
sheet.saveToFile("myFilename.xls");
to save the file.

It would be nice to be able to call something like this in the jsp page:
response.sendRedirect(sheet.downloadServlet);
to "download" the document. Without having to pass the whole document as a parameter to another servlet (the variable "sheet" in the above sample already has the whole excel document in an variable).

are you saying that I should call something like this?  :
ExcelSheet sheet = new ExcelSheet();
sheet.someFunctionToCreateTheDocument();
sheet.someAnotherFunctionToCreateMoreOfTheDocument();

location.href= 'newServletFromANewFile?theData=<%=sheet.getTheWholeDocumentAsString()%>';

then my question is... isn't it a problem calling a servlet if the excel sheet is be big?
0
 
LVL 9

Accepted Solution

by:
shinobun earned 400 total points
ID: 17808418
In order to download via a servlet, you must use the ServletResponse#getOutputStream() method [1] to get an OutputStream and write the file to the stream.  You only need to create a servlet that will:

1. Create the excel sheet.  (The code you posted.)
2. Get the output stream.
3. Dump the excel sheet as a byte stream to the output stream.

[1] http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletResponse.html#getOutputStream()
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues undeā€¦
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now