Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to create a new .txt file from JSP ?  RSS feed

 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
From my JSP page when I click a button I have to create a text file. This txt file has to be populated with the data in my current JSp/html page.
How can I create a .txt file from jsp ?
Thanks,
Maya
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I see it, the JSP should POST to a servlet that would assemble the POSTed data from the request parameters and then use standard Java (i.e. File, Writer classes) to create, write and close a file in the server (you did not mean creating a file on the client machine, did you?)
 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tony,
My aim is to create a .txt file with the output on my machine, or on the machine which runs the application.
I want to know how can I create a .txt file ?
Is there a weay that I can use response.setContentType ??
Please help
Thanks
Maya
 
mou haj
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a file writer class
FileWriter somename = new FileWriter(path+name where file has to be created);

then you may create the lines like this
HdrLine = "H"+base_Number_r.elementAt(i)+arrival_Date_r.elementAt(1)+"\n";
somename.write(HdrLine);
If it is a fixed length txt file then youll have to pad the values with necessary amount of spaces
Try this
all the best
 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mou/ Tony,
I tried creating a file and writing data into it from a vector like this.
File f1 = new File("data.txt");
FileWriter fw = new FileWriter(f1);
for(int i=0;i<carddata.size();i++){
String cardElements = (String)carddata.elementAt(i);
fw.write(cardElements);
}
But it is only creating an empty file.
Any idea
Maya
 
Shreya Menon
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved the prob.
Thanks for the help
Maya
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what was your solution dear menon?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!