JVM如何唯一标识不同应用程序中不同文件夹中同名的JSP

发布于 2024-12-05 06:07:17 字数 396 浏览 1 评论 0原文

JVM 如何唯一地识别不同应用程序中不同文件夹中具有相同名称的 JSP?

更清楚地说,假设两个应用程序(war 的)部署在服务器 A1 和 A2 上。现在A1在文件夹F11和F12中有Random.jsp(2个jsp名称相同但代码不同),同样A2在F21和F22中有Random.jsp。

当部署代码并将 jsp 转换为 Servlet 时,我相信所有 4 个 JSP 的 Servlet 名称也将相同。那么 JVM 如何针对各个请求唯一地识别它们呢?

如果它仅限于单个应用程序,我会假设 JVM 在 servlet 转换期间会使用文件夹名称作为包,但它是否可以跨应用程序工作。

另请指教,JVM 是否通过为生成的 servlet 声明不同的包或使用不同类的一些内部映射结构来处理这种区别(第二个选项听起来很奇怪)

How does JVM uniquely identify JSP's with same name in different folder across different applications?

To be more clear Say two applications (war's) are deployed on server A1 and A2. Now A1 has Random.jsp in folder F11 and F12 (2 jsp with identical names but different code), similarly A2 has Random.jsp in F21 and F22.

When the code is deployed and jsp's are translated into Servlets I believe the Servlet name would be same as well for all 4 JSP's. So how does JVM identify them uniquely for respective requests?

Had it been limited to a single application, I would have assumed JVM would use folder names as packages during servlet translation but does it work across applications.

Also please enlighten, whether JVM takes care of this distinction by declaring different packages for the generated servlet or using some internal mapping structures of different classes (this second option sounds quite weird)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-12-12 06:07:17

这是依赖于实现的。 TOMCAT 的做法是使用一个 work 目录,其中包含 A1.warA2.war 的单独文件夹。这样,尽管“Random.jsp”为两个 WAR 生成相同的 servlet 名称,但它们被放置在不同的文件夹中,因此不会出现混淆。

例子:

/usr/java/tomcat/work/Catalina/localhost/A1/org/apache/jsp/Random_jsp.java
/usr/java/tomcat/work/Catalina/localhost/A2/org/apache/jsp/Random_jsp.java

That is implementation dependent. The way TOMCAT does it, is using a work directory with separate folders for A1.war and A2.war. That way, despite "Random.jsp" generating the same servlet name same for both WARs, they're placed in different folders so there's no possible confusion.

Example:

/usr/java/tomcat/work/Catalina/localhost/A1/org/apache/jsp/Random_jsp.java
/usr/java/tomcat/work/Catalina/localhost/A2/org/apache/jsp/Random_jsp.java
何以畏孤独 2024-12-12 06:07:17

您提到的问题不仅限于 JSP 文件(被转换为类)。 Web容器的工作是确保容器内的不同Web应用程序不会相互干扰。每个应用程序都可以具有具有相同包和相同名称的类文件。最佳示例:每个 Web 应用程序都有一些通用库,例如​​ log4j,但版本不同。

解决方案是以一种非常有创意的方式使用 Java 的 ClassLoader。一个类只有加载它的类加载器和所有子类加载器(类加载器形成树状层次结构)才知道。 webcontainer基本上为每个WebApp打开一个新的类加载器,每个类加载器可以加载相同的类——Bingo。

注意:只要一切都正确完成,效果就很好。但是,如果这些类的实例“泄漏”到其他应用程序中,就会发生奇怪的事情......在这种情况下,类似的消息

ClassCastException....instance of class xyz.Foo is not an instance of class xyz.Foo

并不罕见。第一次看到你会摸不着头脑。

The problem you are mentioning is not limited to JSP files (which get translated into classes). It is the Webcontainer's job to ensure that different Webapplications within the container do not interfere. And each application CAN have classfiles with the same package and the same name. Best example: Each Webapp has some common library like log4j but with different versions.

The solution is to use Java's ClassLoader in a very creative way. A class is only known to the classloader which loaded it and to all child-classloader (classloader form a tree-like hierarchy). The webcontainer basically opens a new classloader for each WebApp, each Classloader can load the same class - Bingo.

Note: As long as everything is done properly, this works quite well. But if instances of these classes "leak" into the other application, strange things happen... A message like

ClassCastException....instance of class xyz.Foo is not an instance of class xyz.Foo

is not uncommon in that case. The first time you see that you WILL scratch your head.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文