对Servlet容器的补充
【0】README
0.1)本文是對(duì) 一個(gè)簡(jiǎn)單的servlet容器?的補(bǔ)充;
【1】Servlet容器
1.1)通過(guò)一個(gè)簡(jiǎn)單的servlet容器這篇博文,我們看到:其中的核心代碼是 類加載器, 然而,在我follow 其代碼,分別在命令行 和 Eclipse 執(zhí)行時(shí),得到了不同的執(zhí)行結(jié)果; 參見(jiàn)我的提問(wèn)?對(duì)于URLClassLoader,Eclipse執(zhí)行正確,而命令行執(zhí)行拋出異常?這個(gè)問(wèn)題描述;
1.2)拋出異常的原因: 顯然是 類加載器沒(méi)有找到要加載類所在的dir, 然而,正如你所見(jiàn),我在 URLClassLoader的源代碼中已經(jīng) 指定了加載路徑;
try {// create a URLClassLoader, 創(chuàng)建類載入器(類加載器是干貨代碼 )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.ClassLoaderFactoryString 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)解決方法:正如你在 “一個(gè)簡(jiǎn)單的servlet容器” 這篇博文中所見(jiàn),我需要再 命令行運(yùn)行該程序時(shí),手動(dòng)添加 類加載路徑到 classpath, 這樣才能執(zhí)行成功,否則失敗。
Attention)我納悶的地方在于:明明在上述代碼中,我已經(jīng)在 URLClassLoader的類構(gòu)造器中指明了 類加載路徑,為什么我還要在命令行手動(dòng)設(shè)置類加載路徑到 classpath,那豈不是我在URLClassLoader 中指明的 類加載路徑是 invalid ? 也就是上述我在技術(shù)問(wèn)題中所提出的問(wèn)題,但是這個(gè)問(wèn)題一直沒(méi)有人來(lái)解答。
總結(jié)
以上是生活随笔為你收集整理的对Servlet容器的补充的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: tomcat(2)一个简单的servle
- 下一篇: 2016第11届四川省高校计算机(软件)