ssh的web.xml学习笔记
2017-11-09 13:39
381 查看
web.xml 中的listener、 filter、servlet 加载顺序及其详解
首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:listener -> filter -> servlet
同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真正的加载顺序为:context-param -> listener -> filter -> servlet
对于某类配置节而言,与它们出现的顺序是有关的。以 filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。web 容器启动时初始化每个 filter 时,是按照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。
servlet 同 filter 类似,此处不再赘述。
由此,可以看出,web.xml 的加载顺序是:context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。
Listener元素指出事件监听程序类。
但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时,使用servlet-mapping元素。
可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。
而不用编辑使用这些文件的JSP页面。
的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。
web.xml文件中webAppRootKey属性是web项目的绝对路径,默认值是webApp.root,可以通过System.getProperty(“webApp.root”)来获取属性值或者在配置文件中通过${webApp.root}获得。
在web.xml中通过contextConfigLocation配置spring,contextConfigLocation
参数定义了要装入的 Spring 配置文件。在指定位置加载spring-context.
com.zttech.sys.listener.WebContextListener继承了org.springframework.web.context.ContextLoaderListener
ContextLoaderListener的作用就是启动Web容器时,自动装配spring-context.xml的配置信息
org.springframework.web.util.IntrospectorCleanupListener监听器主要负责处理由JavaBean Introspector使用而引起的缓冲泄露。
在整合spring容器时使用ContextLoaderListener,它实现了ServletContextListener监听器接口,
ServletContextListener只负责监听web容器启动和关闭的事件。
而RequestContextListener实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,web服务器
接收的每一次请求都会通知该监听器。
OpenSessionInViewFilter的主要功能是用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。目的是为了实现”Open Session in View”的模式。
授权与权限管理,DelegatingFilterProxy类的一些内部运行机制,其实主要作用就是一个代理模式的应用,可以把servlet 容器中的filter同spring容器中的bean关联起来。
WebStatFilter用于采集web-jdbc关联监控的数据。
DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。
url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。
spring-mvc加载spring-mvc*.xml来初始化上下文。
错误页面
首先可以肯定的是,加载顺序与它们在 web.xml 文件中的先后顺序无关。即不会因为 filter 写在 listener 的前面而会先加载 filter。最终得出的结论是:listener -> filter -> servlet
同时还存在着这样一种配置节:context-param,它用于向 ServletContext 提供键值对,即应用程序上下文信息。我们的 listener, filter 等在初始化时会用到这些上下文中的信息,那么 context-param 配置节是不是应该写在 listener 配置节前呢?实际上 context-param 配置节可写在任意位置,因此真正的加载顺序为:context-param -> listener -> filter -> servlet
对于某类配置节而言,与它们出现的顺序是有关的。以 filter 为例,web.xml 中当然可以定义多个 filter,与 filter 相关的一个配置节是 filter-mapping,这里一定要注意,对于拥有相同 filter-name 的 filter 和 filter-mapping 配置节而言,filter-mapping 必须出现在 filter 之后,否则当解析到 filter-mapping 时,它所对应的 filter-name 还未定义。web 容器启动时初始化每个 filter 时,是按照 filter 配置节出现的顺序来初始化的,当请求资源匹配多个 filter-mapping 时,filter 拦截资源是按照 filter-mapping 配置节出现的顺序来依次调用 doFilter() 方法的。
servlet 同 filter 类似,此处不再赘述。
由此,可以看出,web.xml 的加载顺序是:context-param -> listener -> filter -> servlet ,而同个类型之间的实际程序调用的时候的顺序是根据对应的 mapping 的顺序进行调用的。
web.xml配置详解
Web.xml常用元素<display-name></display-name>定义了WEB应用的名字
<description></description>声明WEB应用的描述信息
<context-param></context-param>context-param元素声明应用范围内的初始化参数。
<filter></filter>过滤器元素将一个名字与一个实现javax.servlet.Filter接口的类相关联。
<filter-mapping></filter-mapping>一旦命名了一个过滤器,就要利用filter-mapping元素把它与一个或多个servlet或JSP页面相关联。
<listener></listener>servlet API的版本2.3增加了对事件监听程序的支持,事件监听程序在建立、修改和删除会话或servlet环境时得到通知。
Listener元素指出事件监听程序类。
<servlet></servlet>在向servlet或JSP页面制定初始化参数或定制URL时,必须首先命名servlet或JSP页面。Servlet元素就是用来完成此项任务的。
<servlet-mapping></servlet-mapping>服务器一般为servlet提供一个缺省的URL:http://host/webAppPrefix/servlet/ServletName。
但是,常常会更改这个URL,以便servlet可以访问初始化参数或更容易地处理相对URL。在更改缺省URL时,使用servlet-mapping元素。
<session-config></session-config>如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存。
可通过使用HttpSession的setMaxInactiveInterval方法明确设置单个会话对象的超时值,或者可利用session-config元素制定缺省超时值。
<mime-mapping></mime-mapping>如果Web应用具有想到特殊的文件,希望能保证给他们分配特定的MIME类型,则mime-mapping元素提供这种保证。
<welcome-file-list></welcome-file-list>指示服务器在收到引用一个目录名而不是文件名的URL时,使用哪个文件。
<error-page></error-page>在返回特定HTTP状态代码时,或者特定类型的异常被抛出时,能够制定将要显示的页面。
<taglib></taglib>对标记库描述符文件(Tag Libraryu Descriptor file)指定别名。此功能使你能够更改TLD文件的位置,
而不用编辑使用这些文件的JSP页面。
<resource-env-ref></resource-env-ref>声明与资源相关的一个管理对象。
<resource-ref></resource-ref>声明一个资源工厂使用的外部资源。
<security-constraint></security-constraint>制定应该保护的URL。它与login-config元素联合使用
<login-config></login-config>指定服务器应该怎样给试图访问受保护页面的用户授权。它与sercurity-constraint元素联合使用。
<security-role></security-role>给出安全角色的一个列表,这些角色将出现在servlet元素内的security-role-ref元素
的role-name子元素中。分别地声明角色可使高级IDE处理安全信息更为容易。
<env-entry></env-entry>声明Web应用的环境项。
<ejb-ref></ejb-ref>声明一个EJB的主目录的引用。
<ejb-local-ref></ ejb-local-ref>声明一个EJB的本地主目录的应用。
<context-param> <param-name>webAppRootKey</param-name> <param-value>webapp.root</param-value> </context-param>
web.xml文件中webAppRootKey属性是web项目的绝对路径,默认值是webApp.root,可以通过System.getProperty(“webApp.root”)来获取属性值或者在配置文件中通过${webApp.root}获得。
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring-context*.xml</param-value> </context-param>
在web.xml中通过contextConfigLocation配置spring,contextConfigLocation
参数定义了要装入的 Spring 配置文件。在指定位置加载spring-context.
<listener> <description>spring监听器</description> <listenerclass>com.zttech.sys.listener.WebContextListener</listener-class> </listener>
com.zttech.sys.listener.WebContextListener继承了org.springframework.web.context.ContextLoaderListener
ContextLoaderListener的作用就是启动Web容器时,自动装配spring-context.xml的配置信息
<listener> <description>Introspector缓存清除监听器</description> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener>
org.springframework.web.util.IntrospectorCleanupListener监听器主要负责处理由JavaBean Introspector使用而引起的缓冲泄露。
<listener> <description>request监听器</description> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
在整合spring容器时使用ContextLoaderListener,它实现了ServletContextListener监听器接口,
ServletContextListener只负责监听web容器启动和关闭的事件。
而RequestContextListener实现ServletRequestListener监听器接口,该监听器监听HTTP请求事件,web服务器
接收的每一次请求都会通知该监听器。
<filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> <init-param> <param-name>singleSession</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
OpenSessionInViewFilter的主要功能是用来把一个Hibernate Session和一次完整的请求过程对应的线程相绑定。目的是为了实现”Open Session in View”的模式。
<filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
授权与权限管理,DelegatingFilterProxy类的一些内部运行机制,其实主要作用就是一个代理模式的应用,可以把servlet 容器中的filter同spring容器中的bean关联起来。
<filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class> <init-param> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG,*.woff,*.eot,*.ttf,*.css,*.ico,*.ICO,*.pdf,*.rar,*.zip,*.doc,*.docx,*.xml,*.xls,*.xlsx,*.ppt,*.pptx,/druid/*</param-value> </init-param> <init-param> <param-name>sessionStatMaxCount</param-name> <param-value>1000</param-value> </init-param> <init-param> <param-name>principalSessionName</param-name> <param-value>_curr_emp</param-value> </init-param> </filter> <filter-mapping> <filter-name>DruidWebStatFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
WebStatFilter用于采集web-jdbc关联监控的数据。
<servlet> <servlet-name>springServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:/spring-mvc*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>*.jhtml</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>*.htmlx</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/im/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>springServlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>
DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容器无缝集成,从而可以获得Spring的所有好处。
url-pattern:表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。
spring-mvc加载spring-mvc*.xml来初始化上下文。
<!-- 如果某个会话在一定时间内未被访问,服务器可以抛弃它以节省内存 --> <session-config> <session-timeout>30</session-timeout> </session-config>
<error-page> <error-code>500</error-code> <location>/WEB-INF/views/error/500.jsp</location> </error-page> <error-page> <error-code>404</error-code> <location>/WEB-INF/views/error/404.jsp</location> </error-page> <error-page> <error-code>400</error-code> <location>/WEB-INF/views/error/400.jsp</location> </error-page> <error-page> <error-code>403</error-code> <location>/WEB-INF/views/error/403.jsp</location> </error-page>
错误页面
相关文章推荐
- Java.Web学习笔记 XML
- JSP学习笔记之三——关于servlet配置web.xml出现的问题
- appfuse实例中的web.xml学习笔记之过滤器
- 【JAVA WEB】学习笔记——XML介绍
- web.xml配置项学习笔记
- [学习笔记]jsp web.xml文件的作用及基本配置
- spring mvc学习笔记(一)web.xml文件配置的一点重要信息
- WebService学习笔记-读取webxml.com.cn的天气服务信息
- WebService学习笔记-读取webxml.com.cn的天气服务信息
- Spring MVC学习笔记:web.xml中load-on-startup的作用
- Javaweb学习笔记二 web.xml相关配置
- JSF学习笔记-web.xml和faces-config.xml配置
- JavaWEB.xml解析技术SAX&DOM(学习笔记)
- Struts2学习笔记一:web.xml的配置
- J2EE Struts2 学习笔记002 ---- web.xml 解析
- Jsp&Servelet 学习笔记- 映射一个Servlet实名到web.xml中
- Web Service学习笔记:XML Web services 基础结构
- 【web开发学习笔记】web.xml配置简单分析一
- spring学习笔记 -- day12 ssh整合之基于XML的独立式整合
- JSF学习笔记-web.xml和faces-config.xml配置