So today at work a question came up about were you’re supposed to store your tld file for your JSP tags… in the webroot (ie: wwwroot/mytags.tld) or in the WEB-INF directory. From my work with with JSP and Servlets, I knew that mytags.tld was supposed to go in the WEB-INF directory, but I didn’t know why. So, why is it that way?
JSR-000053 JavaTM Servlet 2.3, page 61 of 257:
“A special directory exists within the application hierarchy named “WEB-INF”. This directory contains all things related to the application that aren’t in the document root of the application. The WEB-INF node is not part of the public document tree of the application. No file contained in the WEB-INF directory may be served directly to a client by the container. However, the contents of the WEB-INF directory are visible to servlet code using the getResource and getResourceAsStream method calls on the ServletContext. Hence, if the Application Developer needs access, from servlet code, to application specific configuration information that he does not wish to be exposed to the web client, he may place it under this directory. Since requests are matched to resource mappings case-sensitively, client requests for ‘/WEB-INF/foo’, ‘/WEb-iNf/foo’, for example, should not result in contents of the web application located under /WEB-INF being returned, nor any form of directory listing thereof.”
Still no mention of the TLD (tag library descriptor), although we now know that the WEB-INF directory is meant to be a secure place for application developers to place code without worrying about it being exposed to client requests.
So then you have to download JavaServer PagesTM 1.2 Specifications, page 112 of 268 says:
“Tag library descriptor files have names that use the extension “.tld”, and the extension indicates a tag library descriptor file. When deployed inside a JAR file, the tag library descriptor files must be in the META-INF directory, or a subdirectory of it. When deployed directly into a web application, the tag library descriptor files must always be in the WEB-INF directory, or some subdirectory of it.”
Ah, so according to the JSP Spec, we have to deploy the .tld file inside WEB-INF. But how are we supposed to reference the taglib once we have it deployed? Page 116 answers that:
“The explicit web.xml map provides a explicit description of the tag libraries that are being used in a web application. The implicit map from TLDs means that a JAR file implementing a tag library can be dropped in and used immediatedly through its stable URIs. The use of relative URI specifications in the taglib map enables very short names in the taglib directive. For example, if the map is:
then it can be used as:
<%@ taglib uri=”/myPRlibrary” prefix=”x” %>
Finally, the fallback rule allows a taglib directive to refer directly to the TLD. This arrangement is very convenient for quick development at the expense of less flexibility and accountability. For example, in the case above, it enables:
<%@ taglib uri=”/WEB-INF/tlds/PRlibrary_1_4.tld” prefix=”x” %>”
Fun for the whole family! Let’s all go and try to use our taglibs now!