Ok, I just started dipping into J2EE and of course have some questions. To make it short and sweet, I want to know if it is possible for a JSP page to gain any type of file access to the client's computer?
Printable View
Ok, I just started dipping into J2EE and of course have some questions. To make it short and sweet, I want to know if it is possible for a JSP page to gain any type of file access to the client's computer?
Not unless you count the form-based file upload or and applet or ActiveX control embedded in the page. On the browser it's just HTML. The fact it was created by a JSP is irrelevent.
Yes i realize "on the browser" it is just HTML. I guess i should have worded my question better. Can the J2EE server somehow access the client's file system.. But it seems from your response this is not possible as i suspected. I thought of an applet like you mention. Correct me if I'm wrong, but can't a signed applet accepted by the client gain limited access to the client's file system? Either way, I'm looking for some way to write a simple text file to the client's computer. I would prefer to do it with some type of server-side technology rather than client-side because I don't want to have to rely on a client having a specific plugin. I guess I'm barking up the wrong tree because this if this was possible it would allow websites to place mallicious files on a client's computer. What other options do I have? Maybe have an automated email system that sends an email to the clieent with the file as an attachment?
EDIT: Nevermind, I just thought of a solution. I could generate the file and save it on the server, and then use JSP to dynamically create a page with a link to the file. I'm guessing I have unlimited acccess to the file system on the server-side? Correct?
yes, this is correct.
You could also use a server-side redirect to redirect the client browser to the text file.
you do this by setting the following header:
response.setHeader("Location", "<text file location>");
by setting this header, you tell the browser to go to that location. This redirect is done without the users knowledge, so this will be the most user friendly method.
Another way to do it off course is to set the content type of the page to "text/plain" and to stream the text straight from the jsp page (which in the end might be easier)
I actually meant to write that i'm creating an XML file, not a simple text file (i'm a little out of it right now). So I cannot have any HTML or other stuff mixed in with it, therefore i need it to be a file. Anyways, it seems creating the file and then genrating a link for the client would be the best way. So I guess the last question I have is how do I go about dynamically creating the file on the server? Since J2EE is built on top of J2SE does this mean that I can simply use the file access methods that I would in a stand alone application? Or is it more complicated than that? Is it server specific? (i'm using Sun's Application Server by the way).
Let me theorize how I think I could do it and someone tell me if this is right.
I have a form that the user goes to. They fill out the info and hit the submit button. They are directed to a JSP page that pulls the information and places it inside a java bean, this bean is then passed to a class I made that creates the XML file on the server (using typical file access as i would in a stand alone app, specifically the java.io classes). The JSP page also provides a link to the newly created file allowing the user to download it.
Someone point me in the right direction here. Thanks for the help.
It depends on your design of course but you don't have to create a file at all. You can create the xml doc in memory and stream that out to the browser either as the response to the form submission or as a link to a servlet on the form response page.
Code:Form->(request)servlet->(response)xml stream
or
/-->(session)bean---------------------\
Form->(request)servlet->(response)page w/link->(request)servlet->(response)xml stream