rlegs
rlegs

Reputation: 81

How do I "import" a HTML file from a shared drive into a JSP from my war file?

Preface: I am an inexperienced java programmer handed one of his first assignments. If I do not ask the question correctly or do not give enough detail, please let me know.

I am trying to import a HTML page that is saved on my C drive. I am trying to import it to the content portion (div id="content") of a JSP file that exists in a war file. I have already figured out that I can not use jsp:include, #include, @include file because the file exists outside the war file. I also figured out that c:import and iFrame do not work.

My goal is to make the contents of the html file that is saved in on my c drive appear in the contents of the jsp (visible on the web page).

Am I on the right track with this <% File f = new File("c:\\temp\\filename.html").......%>

I have searched stackoverflow and the only topic that came close was "How to Include a file outside the application (war) using jsp include." It did not really get me where I needed to go. Maybe the answer is right in front of me but I couldnt see it.

Upvotes: 1

Views: 1909

Answers (4)

Kennet
Kennet

Reputation: 5796

My suggestion is to use http form upload in your jsp application. In that case your file can be in any place that is accesible in your filesystem instead of hardcoding it to be in a certain place. Usage http://commons.apache.org/fileupload/using.html

Good tutorial on http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html Some useful hints can also be found in the video, http://www.youtube.com/watch?v=BLamJlRg9Ws

Upvotes: -1

outofBounds
outofBounds

Reputation: 691

Try

<% File f = new File("c:\\temp\\filename.html");
   BufferedReader in = new BufferedReader(new FileReader(f));
   while (in.readLine() != null) {
     out.println(blah blah blah);          
   } 
   in.close();
%>

reading the File and Printing it to the JSP should work,

Upvotes: 0

Marcelo
Marcelo

Reputation: 4608

Do you want clients to see the contents of filename.html located on your server? If so, why don't you just get it inside your project/war?

Or do you want clients to see the contents of a filename.html they have on their computers? If so, you might be able to just add an iframe with that source... but you'll run into many security-related problems, since browsers won't ordinarily let you do that.

Upvotes: 0

BalusC
BalusC

Reputation: 1108632

JSP/JSTL does not offer tags which support this. You'd need to do it using pure Java. You just have to write it to the response yourself.

Here's one of the simplest ways:

<%
    Reader reader = new FileReader("c:/path/to/external/file.html");
    try {
        for (int i = 0; (i = reader.read()) != -1;) {
            out.write(i);
        }
    } finally {
        try { reader.close(); } catch (IOException ignore) {}
    }
%>

You could wrap it in a custom tag to keep your JSP free of scriptlet clutter, or you could read it into a String in a servlet and pass it to JSP EL scope.

Upvotes: 3

Related Questions