getServletContext().getResource()引發的錯誤
tomcat4.1:
/**
* Return a URL object of a resource that is mapped to the
* specified context-relative path.
*
* @param path Context-relative path of the desired resource
*
* @exception MalformedURLException if the resource path is
* not properly formed
*/
public URL getResource(String path) throws MalformedURLException {
if (!path.startsWith("/"))
throw new MalformedURLException("Path '" + path +
"' does not start with '/'");
return (new URL(myResourceBaseURL, path.substring(1)));
}
tomcat 5.5:
public URL getResource(String path)
throws MalformedURLException {
System.out.println("qik:"+path);
if (path == null || !path.startsWith("/")) {
throw new MalformedURLException(sm.getString("applicationContext.requestDispatcher.iae", path));
}
path = normalize(path);
if (path == null)
return (null);
String libPath = "/WEB-INF/lib/";
if ((path.startsWith(libPath)) && (path.endsWith(".jar"))) {
File jarFile = null;
if (context.isFilesystemBased()) {
jarFile = new File(basePath, path);
} else {
jarFile = new File(context.getWorkPath(), path);
}
if (jarFile.exists()) {
return jarFile.toURL();
} else {
return null;
}
} else {
DirContext resources = context.getResources();
if (resources != null) {
String fullPath = context.getName() + path;
String hostName = context.getParent().getName();
try {
resources.lookup(path);
return new URL
("jndi", "", 0, getJNDIUri(hostName, fullPath),
new DirContextURLStreamHandler(resources));
} catch (Exception e) {
// Ignore
}
}
}
return (null);
}
---------------------------
程式的架構通過使用getResource時,不小心傳入了一個"",造成了在tomcat5上面運行出錯,提示Path does not start with "/"