views:

75

answers:

2

I'm in charge of updating an existing java app for an embedded device (a copier).

One of the things I want to do is create a servlet which allows the download of all the files in our sandboxed directory on the device (which will include the application log files, local caches, etc). At the moment these files are all in a single directory with no subdirectories.

Basically what I'd like to do is as follows:

  • Log.log
  • Log.log.1
  • Log.log.2
  • SomeLocalCache.txt
  • AnotherLocalCache.txt

where each line is a clickable link allowing download of the file.

However, my HTML experience is basically nil, and my familiarity with the Java API is still fairly rudimentary, so I'm looking for some advice on the proper way to go about it.

I've read through all the samples provided, and here's what I'm thinking.

I can create a servlet at a specified URL on the device which will call into my code. Let's call this /MyApp.

I add another link below that, let's call it /MyApp/Download.

When this address it reached in a browser, it displays the list of files.

This list will have to be created on the fly. I can create an HTML template file and put it in the res folder (this seems to be the recommended method for the device in question), but the whole list of files/links will need to be substituted in at run time. Here's an example I found using <ol>+<li> tags for the list and <a> tags for the links. I can generate that on the fly pretty easily. Is that a reasonable way to go?

e.g.

<ol>
  <li>
    <a href=".../MyApp/Download/Log.log">Log.log</a>
  </li>
  <!--more <li> elements-->
</ol>

Clicking on an individual file will link to /MyApp/Download/File.ext which will then trigger the file download via my servlet (I've found this code which looks promising for the actual download).

The device will require users to log before they are allowed to access the /MyApp link or any sub-links, and I can additionally require that the logged in user be an admin before allowing file download, which together seems like sufficient security in this case (heavy security is not required for these files).

So am I missing anything big or is this a reasonable plan of engagement?

+1  A: 

EDIT

Judging by this link http://stackoverflow.com/questions/1039827/when-to-use-ul-or-ol-in-html Many people are going to hammer the answer and comment below because they say it is important to put semantic information into the HTML.

My point is simply this -- the only difference is browsers will display one with bullet points (as OP seems to want) and one with numbers (as the OP does not want.) I suggest he change the HTML to the way he wants it to render, or leave it as is, and make some CSS changes.

Yes there is a semantic difference between the two... they will both still render in order as defined here http://www.w3.org/TR/html401/struct/lists.html Is the HTML the place to put semantic information? I think not, code that generated the HTML is the correct place. Your cohesion may vary.

I won't change my original comment for the sake of history.

END EDIT

Seems fine to me -- however <ol> is not really used any more, I'd go with <ul>. Don't worry, it is still ordered as you would expect.

The reason for this is the only difference between the two was browsers would automatically number (render with a number before) ordered lists. However, with CSS all the rendering control can be in the CSS (including numbering) and everyone is happy.

Hardly anyone uses the auto number anymore. In fact via CSS, lists can and are used for all sorts of crazy things, including CSS menuing systems.

Hogan
*not really used any more* : This makes no sense. You use an `<ol>` if you want an ordered list (with index numbers) and you use an `<ul>` if you want an unordered list (with just bullets).
BalusC
See my additional notes above BalusC, it can all be done via CSS and with more control to the front end coder. Also, in his examples he does NOT have numbers.
Hogan
You should not use CSS to create an ordered list. `<ol>` and `<ul>` have basically two different semantic meanings. That is like `<em>` and `<strong>`, both are semantically valuable. Of course you can use CSS to change the *look* of the ordered list.
Felix Kling
Well, it sounds like in my case `<ul>` is actually both more accurate semantically and the way I would like it to render (with bullet points rather than numbers). So that's what I will go with.
Lawrence Johnston
Accepted due to the helpful advice and the fact that nobody else seems to have found any reason to answer.
Lawrence Johnston
A: 

Here's a summary you need to do:

  1. You can use File#listFiles() to get a File[].
  2. You can use JSTL c:forEach to iterate over an array.
  3. You can use HTML <ol>, <ul> or <dl> elements to display a list.
  4. You can use HTML <a> element to display a link.
  5. You can use a Servlet to write an InputStream of a local file to OutputStream of the response. Remember to pass at least Content-Type, Content-Length and Content-Disposition along.
  6. You can make use of request pathinfo to pass file identifier safely. E.g. map servlet on /files/* and let link point to http://example.com/files/path/to/file.ext and in the servlet you can get /path/to/file.ext by request.getPathInfo().

A basic and solid servlet example can be found here: FileServlet. If you want to add resume and compressing capabilities, then you may find the improved FileServlet more useful.

That said, most appservers also just supports directory listing by default. Tomcat for example supports it by default. You can just define another <Context> in Tomcat's server.xml with a docBase of C:/path/to/all/files and a context path of /files (so that it's accessible by http://example.com/files.

<Context docBase="/path/to/all/files" path="/files" />

That's basically all. No homegrown code/html/servlet needed.

BalusC