• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

export data from JSP to text file

 
rohinit thacker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65123
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you will run into the same types of problems in a servlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic