Re: File system resource for static content
Hi. Not a direct answer, but did you look at the webdav app ? At least for ideas. Robert Drescher wrote: [...] I want a servlet to perform file uploads and to store the files in the local filesystem. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
File system resource for static content
Hello users, I'm trying to find the best practice way for implementing the following: I want a servlet to perform file uploads and to store the files in the local filesystem. This part is not that hard to do as i'm currently writing into the javax.servlet.context.tempdir. The problem is that i need to store the files in a directory, that will be accessible from the web. My tutor at university gave me the hint that this is best done with a resource which points to a local directory and that's mapped to the webapp. So I imagine that http://localhost:8080/WebTest/Upload is my servlet mapping and that http://localhost:8080/WebTest/files/ points to this resource. But all my research in the documentation did not bring any success. I know that i can specify resources in the context.xml, but not how I specify the path on the local system or how to do the mapping... If anyone already did this and can provide me with configuration examples, I'd very much appreciate it. I am using Tomcat 5.5 on Linux (not the pre-packaged), the application is deployed as war, my context.xml is located in META-INF ?xml version=1.0 encoding=UTF-8? Context privileged=true antiResourceLocking=false antiJARLocking=false path=WebTest / - My web.xml is the following: - ?xml version=1.0 encoding=UTF-8? web-app version=2.4 xmlns=http://java.sun.com/xml/ns/j2ee; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance; xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd; descriptionTest Internetapplikationen/description servlet descriptionController Servlet/description display-nameController Servlet/display-name servlet-nameController/servlet-name servlet-classorg.agility.webtest.control.Controller/servlet-class /servlet servlet-mapping servlet-nameController/servlet-name url-pattern/Controller/url-pattern /servlet-mapping welcome-file-list welcome-fileLogin.jsp/welcome-file /welcome-file-list /web-app -- Thank you very much for your help Robert
Re: File system resource for static content
The problem with this approach is that when you upgrade the war file the files will be deleted. I believe It is better to save the files outside the web app and deliver them either with a symbolic link from within your war file or using a reader servlet. Yuval Perlov www.r-u-on.com On Dec 14, 2008, at 7:17 PM, Steve Ochani wrote: Send reply to: Tomcat Users List users@tomcat.apache.org Date sent: Sun, 14 Dec 2008 12:56:17 +0100 From: Robert Drescher robert.dresc...@gmail.com To: users@tomcat.apache.org Subject:File system resource for static content Hello users, I'm trying to find the best practice way for implementing the following: I want a servlet to perform file uploads and to store the files in the local filesystem. This part is not that hard to do as i'm currently writing into the javax.servlet.context.tempdir. The problem is that i need to store the files in a directory, that will be accessible from the web. My tutor at university gave me the hint that this is best done with a resource which points to a local directory and that's mapped to the webapp. So I imagine that http://localhost:8080/WebTest/Upload is my servlet mapping and that http://localhost:8080/WebTest/files/ points to this resource. I do something very similar using apache commons upload project. I have a separate files directory in my webapp directory and I use the following code to initialize a path to save my uploaded files: ServletContext sc = getServletContext(); String path = sc.getRealPath(/files); I can then access a file in the that files directory via http://server:8080/appname/files/filename -Steve O. But all my research in the documentation did not bring any success. I know that i can specify resources in the context.xml, but not how I specify the path on the local system or how to do the mapping... If anyone already did this and can provide me with configuration examples, I'd very much appreciate it. I am using Tomcat 5.5 on Linux (not the pre-packaged), the application is deployed as war, my context.xml is located in META-INF ?xml version=1.0 encoding=UTF-8? Context privileged=true antiResourceLocking=false antiJARLocking=false path=WebTest / - My web.xml is the following: - ?xml version=1.0 encoding=UTF-8? web-app version=2.4 xmlns=http://java.sun.com/xml/ns/j2ee; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance; xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd; descriptionTest Internetapplikationen/description servlet descriptionController Servlet/description display-nameController Servlet/display-name servlet-nameController/servlet-name servlet-classorg.agility.webtest.control.Controller/servlet-cla ss /servlet servlet-mapping servlet-nameController/servlet-name url-pattern/Controller/url-pattern /servlet-mapping welcome-file-list welcome-fileLogin.jsp/welcome-file /welcome-file-list /web-app -- Thank you very much for your help Robert - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
Exactly. Since we are supposed to write an application that's running without extracting the war, Steves approach was my first try, but it's not working that way :( Also, symlinks are a good way in posix systems, but then the app is not platform independent anymore. The approach of a reader servlet sounds good, but how can I implement this to include the files into a jsp then? In other words, if /App/Reader is my reader servlet, can I include an image into jsp with img src=/App/Reader?file=image.jpg /? 2008/12/14 Yuval Perlov yu...@r-u-on.com The problem with this approach is that when you upgrade the war file the files will be deleted. I believe It is better to save the files outside the web app and deliver them either with a symbolic link from within your war file or using a reader servlet. Yuval Perlov www.r-u-on.com On Dec 14, 2008, at 7:17 PM, Steve Ochani wrote: Send reply to: Tomcat Users List users@tomcat.apache.org Date sent: Sun, 14 Dec 2008 12:56:17 +0100 From: Robert Drescher robert.dresc...@gmail.com To: users@tomcat.apache.org Subject:File system resource for static content Hello users, I'm trying to find the best practice way for implementing the following: I want a servlet to perform file uploads and to store the files in the local filesystem. This part is not that hard to do as i'm currently writing into the javax.servlet.context.tempdir. The problem is that i need to store the files in a directory, that will be accessible from the web. My tutor at university gave me the hint that this is best done with a resource which points to a local directory and that's mapped to the webapp. So I imagine that http://localhost:8080/WebTest/Upload is my servlet mapping and that http://localhost:8080/WebTest/files/ points to this resource. I do something very similar using apache commons upload project. I have a separate files directory in my webapp directory and I use the following code to initialize a path to save my uploaded files: ServletContext sc = getServletContext(); String path = sc.getRealPath(/files); I can then access a file in the that files directory via http://server:8080/appname/files/filename -Steve O. But all my research in the documentation did not bring any success. I know that i can specify resources in the context.xml, but not how I specify the path on the local system or how to do the mapping... If anyone already did this and can provide me with configuration examples, I'd very much appreciate it. I am using Tomcat 5.5 on Linux (not the pre-packaged), the application is deployed as war, my context.xml is located in META-INF ?xml version=1.0 encoding=UTF-8? Context privileged=true antiResourceLocking=false antiJARLocking=false path=WebTest / - My web.xml is the following: - ?xml version=1.0 encoding=UTF-8? web-app version=2.4 xmlns=http://java.sun.com/xml/ns/j2ee; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance; xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd; descriptionTest Internetapplikationen/description servlet descriptionController Servlet/description display-nameController Servlet/display-name servlet-nameController/servlet-name servlet-classorg.agility.webtest.control.Controller/servlet-cla ss /servlet servlet-mapping servlet-nameController/servlet-name url-pattern/Controller/url-pattern /servlet-mapping welcome-file-list welcome-fileLogin.jsp/welcome-file /welcome-file-list /web-app -- Thank you very much for your help Robert - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
On Sun, Dec 14, 2008 at 11:29 AM, Robert Drescher robert.dresc...@gmail.com wrote: In other words, if /App/Reader is my reader servlet, can I include an image into jsp with img src=/App/Reader?file=image.jpg /? Of course, it's just a URL. Better, just make it img src=/App/Reader/image.jpg / (or /App/images/image.jpg, or whatever). HTH, -- Hassan Schroeder hassan.schroe...@gmail.com - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
The image would work because it is a separate hit (not embedded). For general info, if you did want to embed you would use jsp:include You can also have the servlet map to something like /files/* and parse the path to find out which file is being requested - this makes the file url feel more native than /reader?file=fff One more thing, since you are serving the file and not the web container, you need to handle the Content-Type tag on your own. I am not aware of anyway to access tomcat's internal table but creating something of your own should not be a big issue (see this table http://www.iangraham.org/books/html4ed/appb/mimetype.html) . For an exercise this isn't a must but some browsers will take issue with the server not reporting content-type correctly. Yuval Perlov www.r-u-on.com On Dec 14, 2008, at 9:29 PM, Robert Drescher wrote: Exactly. Since we are supposed to write an application that's running without extracting the war, Steves approach was my first try, but it's not working that way :( Also, symlinks are a good way in posix systems, but then the app is not platform independent anymore. The approach of a reader servlet sounds good, but how can I implement this to include the files into a jsp then? In other words, if /App/Reader is my reader servlet, can I include an image into jsp with img src=/App/Reader?file=image.jpg /? 2008/12/14 Yuval Perlov yu...@r-u-on.com The problem with this approach is that when you upgrade the war file the files will be deleted. I believe It is better to save the files outside the web app and deliver them either with a symbolic link from within your war file or using a reader servlet. Yuval Perlov www.r-u-on.com On Dec 14, 2008, at 7:17 PM, Steve Ochani wrote: Send reply to: Tomcat Users List users@tomcat.apache.org Date sent: Sun, 14 Dec 2008 12:56:17 +0100 From: Robert Drescher robert.dresc...@gmail.com To: users@tomcat.apache.org Subject:File system resource for static content Hello users, I'm trying to find the best practice way for implementing the following: I want a servlet to perform file uploads and to store the files in the local filesystem. This part is not that hard to do as i'm currently writing into the javax.servlet.context.tempdir. The problem is that i need to store the files in a directory, that will be accessible from the web. My tutor at university gave me the hint that this is best done with a resource which points to a local directory and that's mapped to the webapp. So I imagine that http://localhost:8080/WebTest/Upload is my servlet mapping and that http://localhost:8080/WebTest/files/ points to this resource. I do something very similar using apache commons upload project. I have a separate files directory in my webapp directory and I use the following code to initialize a path to save my uploaded files: ServletContext sc = getServletContext(); String path = sc.getRealPath(/files); I can then access a file in the that files directory via http://server:8080/appname/files/filename -Steve O. But all my research in the documentation did not bring any success. I know that i can specify resources in the context.xml, but not how I specify the path on the local system or how to do the mapping... If anyone already did this and can provide me with configuration examples, I'd very much appreciate it. I am using Tomcat 5.5 on Linux (not the pre-packaged), the application is deployed as war, my context.xml is located in META-INF ?xml version=1.0 encoding=UTF-8? Context privileged=true antiResourceLocking=false antiJARLocking=false path=WebTest / - My web.xml is the following: - ?xml version=1.0 encoding=UTF-8? web-app version=2.4 xmlns=http://java.sun.com/xml/ns/j2ee; xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance; xsi:schemaLocation=http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd; descriptionTest Internetapplikationen/description servlet descriptionController Servlet/description display-nameController Servlet/display-name servlet-nameController/servlet-name servlet-classorg.agility.webtest.control.Controller/servlet-cla ss /servlet servlet-mapping servlet-nameController/servlet-name url-pattern/Controller/url-pattern /servlet-mapping welcome-file-list welcome-fileLogin.jsp/welcome-file /welcome-file-list /web-app -- Thank you very much for your help Robert - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
Hi. What if user-a uploads a file called abc.jpg and then user-b uploads a file called abc.jpeg ? Who wins ? Worse, they do it at the same time.. ;-) - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
André Warnier wrote: Hi. What if user-a uploads a file called abc.jpg and then user-b uploads a file called abc.jpeg ? Who wins ? Worse, they do it at the same time.. ;-) Ooops, I meant abc.jpg both times of course. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
On Sun, Dec 14, 2008 at 12:45 PM, André Warnier a...@ice-sa.com wrote: What if user-a uploads a file called abc.jpg and then user-b uploads a file called abc.jpg ? Who wins ? Hopefully, everyone wins, since the Klever Koder has checked first to be sure an existing file isn't being overwritten :-) -- Hassan Schroeder hassan.schroe...@gmail.com - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
That seems like the usual race condition problem to me. Hopefully one of them will get an error, at worst, and it won't silently overwrite the file with the one from whoever won the race. André Warnier wrote: André Warnier wrote: Hi. What if user-a uploads a file called abc.jpg and then user-b uploads a file called abc.jpeg ? Who wins ? Worse, they do it at the same time.. ;-) Ooops, I meant abc.jpg both times of course. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
I had to do something like that myself. Here's a little method that creates a folderpath for each unique session. public File getTempDir(HttpSession hsess) { String path = hsess.getServletContext().getRealPath(/); // / context root of the application return new File(path, hsess.getId()); } Hope this gets you started. - Peter -- View this message in context: http://www.nabble.com/File-system-resource-for-static-content-tp20999639p21004662.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: File system resource for static content
Ok, thanks to all, I finished my Reader Servlet. I created a work dir which has to be included into the webapps.policy so that webapps can read and write to it. My reader servlet accepts a param file so far, since i haven't figured out how to do the mapping and parsing to do the /WebApp/files/filename.jpg so far. But that should be a quick fix. Anyhow, the path is stored in a context-param so all I need to do to port the application is set the path in the web.xml and set the policies to allow read/write access. And yes, i create subdirectories for each user and each picture will get a random file name. Since I maintain user objects which organize all the pictures each user uploads, it's no problem to find it again. Since this is only a test project for University, this stays in-memory, but apparenty, for a real deployment, the whole thing will be done with JDBC. But again, thanks for your help, recommendations and advices Robert
Re: File system resource for static content
On Sun, Dec 14, 2008 at 1:47 PM, Robert Drescher robert.dresc...@gmail.com wrote: My reader servlet accepts a param file so far, since i haven't figured out how to do the mapping and parsing to do the /WebApp/files/filename.jpg so far. But that should be a quick fix. see HttpServletRequest.getPathInfo() -- Hassan Schroeder hassan.schroe...@gmail.com - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org