Solved

Problem creating servlet as a subclass!

Posted on 2006-10-25
3
374 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
advertisement module in core php 4 148
Java - Why doesn't this JFrame work 3 45
web services creation SOAP vs REST 5 21
type mismatch (Object[] to double[] 4 13
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

864 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

23 Experts available now in Live!

Get 1:1 Help Now