对Servlet容器的补充

【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 ? 也就是上述我在技术问题中所提出的问题,但是这个问题一直没有人来解答。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值