Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Problem creating servlet as a subclass!

Posted on 2006-10-25
3
Medium Priority
?
380 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 1200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

885 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