您的位置:首页 > 其它

对Servlet容器的补充

2016-04-07 15:15 274 查看
【0】README

0.1)本文是对
一个简单的servlet容器 的补充;

【1】Servlet容器

1.1)通过一个简单的servlet容器这篇博文,我们看到:其中的核心代码是 类加载器, 然而,在我follow 其代码,分别在命令行 和 Eclipse 执行时,得到了不同的执行结果; 参见我的提问 对于URLClassLoader,Eclipse执行正确,而命令行执行抛出异常 这个问题描述;

1.2)抛出异常的原因: 显然是 类加载器没有找到要加载类所在的dir, 然而,正如你所见,我在 URLClassLoader的源代码中已经 指定了加载路径;

try {
// create a URLClassLoader, 创建类载入器(类加载器是干货代码 )
URL[] urls = new URL[1];
URLStreamHandler streamHandler = null;
File classPath = new File(Constants.WEB_ROOT);
// the forming of repository is taken from the createClassLoader method in
// org.apache.catalina.startup.ClassLoaderFactory
String repository = (new URL("file", null, classPath.getCanonicalPath() + File.separator)).toString() ;
// file:E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\webroot\
// the code for forming the URL is taken from the addRepository method in
// org.apache.catalina.loader.StandardClassLoader class.
urls[0] = new URL(null, repository, streamHandler);
// urls[0] = file:E:/bench-cluster/cloud-data-preprocess/HowTomcatWorks/webroot/
loader = new URLClassLoader(urls);
}

1.3)解决方法:正如你在 “一个简单的servlet容器” 这篇博文中所见,我需要再 命令行运行该程序时,手动添加 类加载路径到 classpath, 这样才能执行成功,否则失败。



Attention)我纳闷的地方在于:明明在上述代码中,我已经在 URLClassLoader的类构造器中指明了 类加载路径,为什么我还要在命令行手动设置类加载路径到 classpath,那岂不是我在URLClassLoader 中指明的 类加载路径是 invalid ? 也就是上述我在技术问题中所提出的问题,但是这个问题一直没有人来解答。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: