Solved

Problem creating servlet as a subclass!

Posted on 2006-10-25
3
378 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
A solution for Fortify Path Manipulation.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

623 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