dcsimg
writing a text file to server?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Rahul_Rohatgi
Posted On:   Thursday, October 3, 2002 06:02 AM

How do I get a servlet to write a text file to a particular directory on the server? Plus how do I get at the server path or directory structure?

Re: writing a text file to server?

Posted By:   John_Mendes  
Posted On:   Thursday, October 10, 2002 09:15 AM

Here's a very simple example of writing to file in a servlet inside the doPost method:


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String filepath = "C:/somepath/that/you/want/to/write/to.txt";
File filetowriteto = new File(filepath);

//open up a stream; using printwriter because it easy
PrintWriter outtofile = new PrintWriter(new FileOutputStream(filetowriteto));

//write something
outtofile.println("Hello, World!");

//be nice a put your toys away when you are done playing with them
outtofile.close();

//write out something to the user
.
.
.
}



-John

Re: writing a text file to server?

Posted By:   rajeev_kumar  
Posted On:   Thursday, October 3, 2002 06:08 AM

There are variour methods on the http request object such as getPort getServerName etc.. u can also get the ServletContext and using the File Object create the File on the Server
About | Sitemap | Contact