wood burning stoves 2.0*
The moose likes JSP and the fly likes how to pass  text to file using Jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to pass  text to file using Jsp?" Watch "how to pass  text to file using Jsp?" New topic
Author

how to pass text to file using Jsp?

Neelima Paramsetty
Greenhorn

Joined: Jun 17, 2003
Posts: 16
Hi,
i want to send some data into the file.for that the code is
<html>
<body>Action completed
<%@ page import ="java.io.*" %>
<%! String str="neelima"; %>
<%
try
{

PrintWriter out=new PrintWriter(new FileOutputStream("\jspex.txt"));
out.println(str);
out.close();
}
catch(Exception e)
{
e.getMessage();
}
%>
</body>
</html>
jspex.txt file is inthe same path as this jsp page.how i have to set the path for this text file.
anybody can help me to solve this.
Thanks in advance,
Neelima
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Three problems here:
- You cannot declare a variable called "out", because the JSP environment already has an implicit variable by that name (which is a JspWriter)
- You have a \ character at the beginning of your filename, which must be removed
- You need to call the ServletContext's getRealPath() method to obtain a path to the file you want to write.
Here's a revised version:


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Other things to think about:
FileWriter is better than FileOutputStream if you are writing character data
If you want to append to the file instead of overwriting it each time, then construct your FileWriter object with FileWriter(realPath, true)
If you're going to write a lot of data, consider using a BufferedWriter
Neelima Paramsetty
Greenhorn

Joined: Jun 17, 2003
Posts: 16
hi newman,
thanks a lot.it is working nicely.
thanks,
neelima
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to pass text to file using Jsp?
 
Similar Threads
how to pass implicitobject(out) in the methods?
Cannot retrieve init parameter through JSP file
JSP EDITOR QUETION? NEED HELP
creating html file from jsp
Reading from .txt file