The File Tag Library

Apr 9, 2001

0

The File Tag Library

Posted in : JSP on by : master
Sponsor: nha tien che QSBSteel

Deployment

  1. Download the File tag library.
  2. Put the library in a directory accessible to your web application (in my case: /u/joeo/epesh.com/main-site/WEB-INF/lib/, where the web application is at .../main-site/.)
  3. Reference the library in your web application’s configuration file (web.xml) like this:
    <taglib>
      <taglib-uri>filetags</taglib-uri>
      <taglib-location>/WEB-INF/lib/filetags-1.1-dev.jar</taglib-location>
    </taglib>
    

    If you’re using Tomcat, this isn’t correct. For this you’ll need to expand the jarfile, and refer to the taglib.tld file directly. I don’t know when Jakarta is planning on changing this.

  4. In your JSP, reference the tag library with a line similar to this:
    <%@ taglib uri="filetags" prefix="file" %>

    This will allow you to use tags named similar to: <file:read name="filename" />

read

<file:read
  name="<%= filename %>"
  { absolute="<%= boolean %>" }
/>

This tag substitutes the contents of the file referenced by the name attribute into the output stream. filename is assumed to be relative to the page source, unless the absolute attribute has a true value.

This is used most in my own code to show example code, as follows:

<pre><view:escape><file:read name="mytestpage.html" /></view:escape></pre>

lastModified

<file:lastModified
  name="<%= filename %>"
  { absolute="<%= boolean %>" }
/>

This tag outputs the local system date and time of the last modification of the file referenced by the name attribute. filename is assumed to be relative to the page source, unless the absolute attribute has a true value.

The date formatting here needs to be improved to include Locale.

size

<file:size
  name="<%= filename %>"
  { absolute="<%= boolean %>" }
/>

This tag outputs the size in bytes of the file referenced by the name attribute. filename is assumed to be relative to the page source, unless the absolute attribute has a true value.

Development Notes

There is an overlap between JSP’s <jsp:include /> directive and <file:read />. Astute watchers already know (although no-one’s told me about it directly yet.) Both include passive content, ostensibly from a filesystem. Honestly, I screwed up: I wrote <file:read /> before I realised the JSP tag existed, and got used to using it; plus, it fits into the library scheme (i.e., “filesystem access”) orthogonally.

There will probably be no equivalent <file:write />, due to system security issues… until JSP has a way to say “only allow these users to write to the file system” on a tag-by-tag level.


I am concerned about the use of an attribute for the filename; I would like to see thoughts on whether you think the filename should go in the body of the tag instead, or possibly in both. Having the content in an attribute means that dynamic inclusion looks something like this:

<file:read name="<%= myFilename %>" />

The use of the expression syntax (the <%= myFilename %> bit) is the only way to get dynamic content into the attribute. With the use of body content instead, we gain some flexibility, as in the following examples:

<file:read>
  <jsp:getProperty name="myBean" property="sourcefile" />
</file:read>

<file:read>
  <%= myFilename %>
</file:read>

<file:read>
  <jndi:getResource name="env/filename" />
</file:read>

Of course, there’s nothing preventing a combination of the two (allowing the name attribute and having it override the body content, or vice versa) except then you have a stylistic and usage problem. Does the attribute override the body? Why? Why not? I don’t like having an unclear usage pattern. Your thoughts?

History

  • 3 Jan 2001
    Removed System.out.println from size because it was unnecessary and highly annoying.
  • 28 Dec 2000
    Added note about <file:write /> and noting <file:read />‘s overlap with JSP’s built-in <jsp:include />, as well as trying to address why that’s left in the tag library instead of removing the duplication.
  • 26 Dec 2000
    Documentation written, put online on http://epesh.com/filetags.jsp.
  • Unknown
    Originally authored, put on Sourceforge in the IN16 project.

License

This library is released under the Artistic License.

Sample Page

A sample page demonstrating these tags can be found at http://epesh.com/testfile.jsp.

Thought for the Moment
Proverbs 19:25:

Smite a scorner, and the simple will beware: reprove one that has understanding, and he will gain knowledge.