Servlet Context

Servlet Context

Container Provider 负责提供ServletContext的实现.

A ServletContext is rooted at a known path within a Web server. For example, a
servlet context could be located at http://www.mycorp.com/catalog . All requests
that begin with the /catalog request path, known as the context path, are routed to
the Web application associated with the ServletContext .

在每一个容器中都有一个ServletContext, 但是当容器被部署在不同的JVM上时, 每一个JVM中有一个ServletContext

Configuration methods

增加了动态增加servlet, filter等函数.

ServletContext中也可以放置Attributes来和Servlet Container交互, 但是都是存在在一个JVM上的, 不可能跨JVM存在.

另外, 提供了一些对于配置的API, 主要是关于三者: Servlet, Filter ,ContextListener; 具体在编程时参考Javadoc

Context Attributes

所有绑定到同一个Context上的Servlet可以访问同样的一些attribute object. 事实上, 该类属性是被分配在JVM上的一块共享空间的, 如果需要在分布式的环境下使用, 则需要把这些属性放在session,或者是database,或者JavaBean中.

Resources

ServletContext访问静态资源的方法, 如下:

getResource
getResourceAsStream

两个函数都是先搜寻Context所在的根目录, 然后是WEB-INF/lib下面Jar包中的META-INF/resources目录, 但是搜寻Jar包下资源的顺序是不定的.但是对于动态资源如jsp, 则是不适用的, 需要根据分发请求来决定.

Multiple Hosts and ServletContexts

当多台服务器公用一个IP地址的时候, 会使用"virtual hosting", 而每一台host应该拥有独立的servlet context, 之间不能被共享.

原文地址:https://www.cnblogs.com/putuotingchan/p/8630940.html