jQuery in Action, 2nd edition*
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "export data from JSP to text file" Watch "export data from JSP to text file" New topic
Author

export data from JSP to text file

rohinit thacker
Greenhorn

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.setContentType("application/plain");
response.setHeader("Content-Disposition", "inline;filename=print.txt");%>

<%!String custName;%>
<%custName=(String)session.getAttribute("custName");%>

<%=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?

Regards
Rohini
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

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] [Bear's FrontMan] [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.


42
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
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Yes, you will run into the same types of problems in a servlet.
 
Consider Paul's rocket mass heater.
 
subject: export data from JSP to text file