问什么说servlet只能访问web-inf/lib下的类?

问题描述

在《How tomcat works》这本书中说道:“servlet不能访问其他的类,即使这些类被包含在运行着当前tomcat的jvm的CLASSPATH中。servlet只能访问WEB-INF/lib目录下的类库。 我写了个servlet试了一下,在servlet能访问到其他的类呀,这句话到底怎么理解呀,一直理解不了?

解决方案

Servlet只允许访问WEB-INF/目录及其子目录下面的类以及部署在WEB-INF/lib目录下的类库。所以一个servlet容器需要一个自己的加载器,该加载器遵守一些特定的规则来加载类。在Catalina中,加载器使用org.apache.catalina.Loader接口表示。估计是这样的 如我们有两个web应用a 和 b a是访问不到b的 但可以访问父加载器的(如tomcat/lib下的)类加载器的问题。Bootstrap - This class loader contains the basic runtime classes provided by the Java Virtual Machine, plus any classes from JAR files present in the System Extensions directory ($JAVA_HOME/jre/lib/ext). NOTE - Some JVMs may implement this as more than one class loader, or it may not be visible (as a class loader) at all.System - This class loader is normally initialized from the contents of the CLASSPATH environment variable. All such classes are visible to both Tomcat internal classes, and to web applications. However, the standard Tomcat 6 startup scripts ($CATALINA_HOME/bin/catalina.sh or%CATALINA_HOME%bincatalina.bat) totally ignore the contents of the CLASSPATH environment variable itself, and instead build the System class loader from the following repositories:$CATALINA_HOME/bin/bootstrap.jar - Contains the main() method that is used to initialize the Tomcat 6 server, and the class loader implementation classes it depends on.$CATALINA_HOME/bin/tomcat-juli.jar - Package renamed Jakarta commons logging API, and java.util.logging LogManager.Common - This class loader contains additional classes that are made visible to both Tomcat internal classes and to all web applications. Normally, application classes should NOT be placed here. All unpacked classes and resources in $CATALINA_HOME/lib, as well as classes and resources in JAR files are made visible through this class loader. By default, that includes the following:annotations-api.jar - JEE annotations classes.catalina.jar - Implementation of the Catalina servlet container portion of Tomcat 6.catalina-ant.jar - Tomcat Catalina Ant tasks.catalina-ha.jar - High availability package.catalina-tribes.jar - Group communication package.el-api.jar - EL 2.1 API.jasper.jar - Jasper 2 Compiler and Runtime.jasper-el.jar - Jasper 2 EL implementation.jasper-jdt.jar - Eclipse JDT 3.2 Java compiler.jsp-api.jar - JSP 2.1 API.servlet-api.jar - Servlet 2.5 API.……WebappX - A class loader is created for each web application that is deployed in a single Tomcat 6 instance. All unpacked classes and resources in the /WEB-INF/classes directory of your web application archive, plus classes and resources in JAR files under the /WEB-INF/lib directory of your web application archive, are made visible to the containing web application, but to no others.Therefore, from the perspective of a web application, class or resource loading looks in the following repositories, in this order:Bootstrap classes of your JVMSystem class loader classses (described above)/WEB-INF/classes of your web application/WEB-INF/lib/*.jar of your web application$CATALINA_HOME/lib$CATALINA_HOME/lib/*.jarAs mentioned above, the web application class loader diverges from the default Java 2 delegation model (in accordance with the recommendations in the Servlet Specification, version 2.3, section 9.7.2 Web Application Classloader). When a request to load a class from the web application's WebappX class loader is processed, this class loader will look in the local repositories first, instead of delegating before looking. There are exceptions. Classes which are part of the JRE base classes cannot be overriden.For some classes (such as the XML parser components in J2SE 1.4+), the J2SE 1.4 endorsed feature can be used. Last, any JAR containing servlet API classes will be ignored by the classloader. All other class loaders in Tomcat 6 follow the usual delegation pattern.
解决方案二:
http://sishuok.com/forum/blogPost/list/0/4120.html

时间: 2024-08-30 20:25:44

问什么说servlet只能访问web-inf/lib下的类?的相关文章

Java的web应用lib下有很多jar包要依赖,可不可以把这些jar打成一个jar吗?

问题描述 Java的web应用lib下有很多jar包要依赖,可不可以把这些jar打成一个jar吗?打成一个jar会影响应用吗? 问题补充:to 蔡华江:啥意思吗,行还是不行 解决方案 引用不一定行.如果你要打包的话,需先将原来的jar解开,然后再打成1个jar包.有些jar包(比较少,一般加密用途的jar包会有)可能牵涉到数字签名的问题,解压后再打包可能会有问题.你可以试下,呵呵 我也有过这样的念头,但是没尝试过. 一般来说是不行的, 我就做过这样的事情.当时我们的一个WEB项目依赖了将近80个

J2ME程序与Servlet通讯访问Access数据库

access|servlet|程序|访问|数据|数据库   作者:关文柏 时间:2005年5月26日 现在J2ME联网的技术很多,本人也只是刚刚接触这方面,这几天帮别人调试了一些这方面的程序,学到了不少东西,下面就谈谈我学习的过程. 首先这种程序可以简化的分解成三个层面,1,J2ME的客户端(MIDlet程序)2,服务器端的servlet(servlet程序)3,数据库(Access或者是SQL等) 下面我通过一个小例子来实现通过手机客户端发送请求,然后服务器端的servlet响应,servle

Ajax基础教程(4)- 实现基本Ajax技术 4.8 访问Web服务

多年以来一直存在一个软件工程问题:从一台机器调用另一台机器上的服务或方法,即使这些机器使用完全不同的硬件或软件.对于这个问题,最近提出的解决方案是Web服务.几年前,Web服务大受吹捧,它的头上围绕着耀眼的光环,有些人认为Web服务就是分布式软件开发的"圣杯".后来,它的光芒逐渐黯淡下来,Web服务最终找到了自己合适的位置,它是支持异构计算机系统相互操作的一种有用的工具. Web服务通常用作为计算机系统之间的通信管道,这与CORBA(公共对象请求代理体系结构).RMI(远程方法调用)或

winCE 通过http协议访问 web服务器(C++)

问题描述 winCE 通过http协议访问 web服务器(C++) 小弟我弄了好久,想在winCE 操作系统上用C++语言实现通过HTTP 协议访问服务器.问度娘也没弄清,希望大神们可以帮帮小弟,给小弟我一个C++的demo!万分感谢!! 解决方案 基于HTTP协议的Web服务器基于Http协议的Web服务器HTTP Web服务器研究之二 HTTP协议

网站首页(index.html)访问不了, servlet可以访问

问题描述 网站首页(index.html)访问不了, servlet可以访问 工程原来是个静态网站,部署在tomcat中,页面全是html,运行正常.后来需要查询数据库,就转成了动态网站,加了一个servlet. 现在工程结构如下: index.html是首页,我放在项目根目录. web.xml内容如下: 我在eclipse中调试发现,在浏览器中输入http://localhost:8080/ProjectName/UserServlet, 是可以进到到UserServlet的get方法中的.

eclipse + JBoss 5 + EJB3开发指南(13):在Servlet中访问应用程序管制EntityManager对象

本文为原创,如需转载,请注明作者和出处,谢谢! 上一篇:eclipse + JBoss 5 + EJB3开发指南(12):使用命名查询执行JPQL      在前面的文章中使用的都是容器管制的EntityManager对象.这类EntityManager对象是由EJB容器创建的,在这种情况下,EntityManager对象的生命周期完全由EJB容器来管理.在SessionBean中可以使用如下的代码来通知EJB容器创建EntityManager对象: @PersistenceContext(un

限制一个Ip只能访问一次的asp代码

  限制一个Ip只能访问一次,转载于bacde的技术博客,现在将asp代码分享给大家: <% '///////////////////////////////////////////////////// '// // '//作用:一个IP地址只允许访问本页一次 // '//作者:BaCde 2010年3月1日 // '//引用:<!-- #include file="Check_Ip.asp" --> // '// // '//////////////////////

不能访问Web服务器的解决方法

访问Web服务器是许多局域网用户经常要做的一项"功课",在频繁访问过程中,不少朋友积累了一些Web服务器访问经验,这些经验常常会帮助他们快速解决一些无法访问的小故障. 访问Web服务器是许多局域网用户经常要做的一项"功课",在频繁访问过程中,不少朋友积累了一些Web服务器访问经验,这些经验常常会帮助他们快速解决一些无法访问的小故障.不过,本文下面贡献出来的Web服务器不能访问故障现象却比较特别,如果不加细细分析,单纯以经验来解决故障时,多半容易走弯路:为了帮助各位朋

ASP技术访问WEB数据库

web|访问|数据|数据库 一. 访问WEB数据库的多种方案 目前在WINDOWS环境下有多种访问WEB数据库的技术,主要有: 1.公共网关接口CGI(Common Gateway Interface) CGI是较早实现的技术.适用于多种服务器平台,如UNIX.WINDOWS等,但CGI的开发成本高.维护困难.功能有限.不具备事务处理功能.占用服务器资源较多. 2. INTERNET数据库连接器IDC(Internet Database Connector) IDC集成在ISAPI(Intern