File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes export data from JSP to text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "export data from JSP to text file" Watch "export data from JSP to text file" New topic

export data from JSP to text file

rohinit thacker

Joined: Jul 18, 2004
Posts: 2
Hi All,

I am trying to export data from jsp file to txt file on client side
I used following code

response.setHeader("Content-Disposition", "inline;filename=print.txt");%>

<%!String custName;%>


It is showing me save as dialog box and I could open the txt file also.
but the problem is there are lots of white spaces.

how do I get rid of this white spaces?

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

Firstly, there is a huge problem with your JSP. By writing:

You have caused the custName variable to be declared at class level, and therefore shared by all instances of the JSP. That means that if two or more people hit your JSP at the same time, they will interfere with each other, each trying to set the variable.

Very bad.

Secondly, the extra whitespace you are seeing is just the nature of JSPs. All whitespace in a JSP, even that surrounding JSP declarations is preserved. To avoid it, don't use a JSP at all, but code this as a servlet where you have complete control over the output.
[ July 19, 2004: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
or put as many JSP coding on a single line which will then get all turned into a single line ending. Hard to read of course but sometimes the only option.

What I've at one time done is create a servlet which uses XSLT to generate output in any format using XML input delivered by another servlet together with the name of the XSL file to use for the generation.
If you cache the precompiled XSL performance is excellent.

michael yue
Ranch Hand

Joined: Nov 20, 2003
Posts: 204
I think I have experience the problem Bear described but in a Servlet. I declared a variable in the class of the servlet. If i put the variable in the doPost method will it solve my problem?
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

Yes, you will run into the same types of problems in a servlet.
I agree. Here's the link:
subject: export data from JSP to text file
jQuery in Action, 3rd edition