• Post Reply Bookmark Topic Watch Topic
  • New Topic

Any File(URI uri) example?  RSS feed

 
Roshni Singhania
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to figure out how to use this constructor of File class which takes a URI as an argument. I tried searching in Google and tried a few ways of putting a uri in its constructor, but it's not working. What I want to do is create a text file on my computer, through web browser, using a Servlet. So I tried things like new File("http://localhost:8080/LOG.TXT"). I was expecting that this text file will be created in the same directory where index.html is placed on the web server, but it doesn't work. Has anyone ever tried this? I'd really appreciate it if anyone could give me some hints as to what exactly I should be doing here?

Thanks in advance!
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.io.File can only handle files on the local file system (and other file systems using Windows shares / SMB). You can't just create a File for any file on any system. Also, creating a File object does not automatically create a real file.

Fortunately for you, the Servlet is run on the server, and therefore you can access it's local file system. For this you need to know the path of the servlet:

See ServletContext for more information.

Now this will not return the file equivalent of http://localhost:8080/LOG.TXT, but of http://localhost:8080/WebApp/LOG.TXT, with WebApp being the name of your web application. "file" now points to "C:\Prorgam Files\Apache Software Foundation\Tomcat 5.5\webapps\WebApp\LOG.TXT" or something similar, depending on where your web application is located.
 
Roshni Singhania
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob Prime,

Thanks for the quick reply. I tried it and yes, it's successfully creating the log file on my machine. Thanks a lot.

Also, I was wondering if there was any way that we could write this log file on client's machine also, so that in whoever is accessing my Servlet will also get this log file. Is there any way to access client's temp folder or something, where I can place this log file? While testing this Servlet with my friend's help, I found that getRemoteAddr() method gives me client's (in this case, my friend's) IP address, but I don't know how to use this address to create a log file on his machine. Anyone ever tried this?

Thanks in advance!
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt it will be possible with Java code. Yes you can get his IP address, but that doesn't mean you can access his file system. And that's a good thing: imagine that every website you visit can write files to your hard drive!

Since JSP and Servlets are executed on the server and not the client, I don't see them writing a log file on the client. A Java applet (if signed!) can do it, and possibly JavaScript too (although I hope not for security reasons). Cookies can be an alternative - those will be written on the client system.

But why do you want to keep the same log on the client too? They shouldn't be bothered with all the internals of your website, just the results.
 
Roshni Singhania
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rob Prime:
I doubt it will be possible with Java code. Yes you can get his IP address, but that doesn't mean you can access his file system. And that's a good thing: imagine that every website you visit can write files to your hard drive!


Yes, that is exactly what I thought too.. but since I'm just learning Servlets, I was curious about that and kind of wanted a confirmation of sort to know that I thought correctly.

Oh, and I wasn't trying to put server log on client's side.. just a text file of output/result generated by the Servlet.

Anyways, thanks for clearing it up for me. It helped a lot!

Have a nice day.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!