ssm 项目中web.xml 配置以及相关知识详解
2020-03-08 12:38
671 查看
web.xml 作用
在javaEE项目中通常会用web.xml文件,这个文件的作用是初始化配置信息,但是如果不需要修改配置初始化信息也可以不用web.xml文件。
web.xml 文件加载过程
当ssm项目启动的时候,比如在tomcat中或者别的web容器,tomcat会去读取web.xml文件。然后依次加载context-param -> listener -> filter -> servlet 结点,归根结底这就是一个web容器生成serveltContext 的过程。最初读取context-param是为了得到键值对,后面需要这些键值对的时候用来使用相当于一个properties文件。
servelet 生命周期
初始化阶段
- 加载servlet类
- 创建servletconfig对象 此对象包含初始化配置信息,并与ServletContext对象关联。
- 创建servlet对象调用init(ServletConfig sc) :容器在创建好servlet对象后调用该方法进行初始化。使servlet与ServletConfig对象关联。ServletContext对象也产生关联,可以直接getServletContext(),进入此初始化阶段有两种情况:1.客户端第一次发出对该servlet的请求;2.在web.xml中设置了元素。
运行阶段
- 创建req和res对象。
- 调用service(Request req,Response res) 负责响应客户请求(根据路径访问指定servlet),执行doGet或doPost方法,返回响应,返回响应后销毁res和req对象。
销毁阶段
- destroy 释放servlet占用的资源,关闭应用服务器时自动调用该方法。
- servlet容器销毁ServletConfig对象。servelet
serveletContext 生命周期
servlet容器在启动一个web应用时就会创建一个ServletContext对象:一个web应用只有一个ServletContext对象,这个web应用下的所有servlet共享同一个ServletContext对象,通过这个ServletContext对象来访问此servlet容器内(服务器,包括多个web应用)的各种资源。
同web应用的生命周期。是最早被创建,最晚被销毁的对象。
serveletContextListener 监听web程序启动和销毁
当web容器启动web应用的时候会调用contextInitialized方法,调用完该方法后再创建filter实例
当web容器终止web应用的时候会调用contextDestroyed方法,在调用之前会销毁所有的servelet和filter再执行该方法
web.xml 如何配置spring以及原理
web.xml
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
使用了一个监听器来初始化Spring 容器
web.xml 如何配置spring mvc以及原理
web.xml
<servlet> <servlet-name>springDispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/SpringMVCConfiguration.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name>springDispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
直接声明springDispatcherServlet
常用其他配置
web.xml
<display-name>WMS</display-name> 配置web应用名字 <filter> 配置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> <error-page> 配置错误码对应的页面 <error-code>404</error-code> <location>/errorPage/404Error.html</location> </error-page> <error-page> <error-code>500</error-code> <location>/errorPage/500Error.html</location> </error-page>
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- ssm框架下web项目,web.xml配置文件的作用(详解)
- IDEA中Tomcat相关——如无法访问localhost:8080欢迎页面,无法配置web.xml以及server.xml,设置download.xml下载目录无效等问题
- SSM项目的web.xml的配置
- web.xml配置文件详解以及web.xml 组件加载顺序
- Java的Web项目中Web.xml配置文件元素详解
- java ssm框架入门(三)正式项目的web.xml配置
- ssm框架下的网上书城项目的开发--web.xml的配置
- ssm框架下web项目,web.xml配置文件的作用
- Java Web项目web.xml配置详解与示例
- Struts配置文件详解,web.xml以及struts-cofig.xml,以及struts-cofig.xml中各个标
- web.xml文件配置详解以及实例说明
- SSM-4 web.xml 及相关spring配置文件
- web项目中web.xml配置详解
- Struts配置文件详解,web.xml以及struts-cofig.xml,以及struts-cofig.xml中各个标签的作用。
- j2ee项目开发web.xml文件加载及配置详解
- SSM项目从零开始到入门011-详解mybatis的XML 映射配置文件
- SSM整合之web.xml的相关配置
- ssm项目之web.xml,spring,springmvc,mybatis配置
- SSM项目整合【含最新版所有常用的相关jar包(maven依赖),以及所有基本配置文件】
- Java项目中web.xml配置详解