Tomcat web.xml中context-param,listener的作用及加载过程

本文介绍了Tomcat web.xml中<context-param>和<listener>的作用及加载过程,如容器读取web.xml,创建ServletContext并处理参数和监听等。还提及Servlet三大组件的配置方式,从传统web.xml配置到Servlet 3.0注解简化,以及Servlet的基本概念和在web.xml中的配置示例。

Tomcat web.xml中context-param,listener的作用及加载过程

<context-param>的作用:
web.xml的配置中<context-param>配置作用(可在listener和servlet中使用,而

ContextLoaderListener及contextConfigLocation则由spring mvc来实现,可读取applicationContext.xml等springbean配置

1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener> 和 <context-param></context-param>

2.紧接着,容器创建一个ServletContext(上下文),这个WEB项目所有部分都将共享这个上下文.

3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.

4.容器创建<listener></listener>中的类实例,即创建监听.

5.在监听中会有contextInitialized(ServletContextEvent args)初始化方法,在这个方法中获得ServletContext = ServletContextEvent.getServletContext();
context-param的值 = ServletContext.getInitParameter("context-param的键");

6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.
换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.

7.举例.你可能想在项目启动之前就打开数据库.
那么这里就可以在<context-param>中设置数据库的连接方式,在监听类中初始化数据库的连接.

8.这个监听是自己写的一个类,除了初始化方法,它还有销毁方法.用于关闭应用前释放资源.比如说数据库连接的关闭.

如:


 
  1. <!-- 加载spring的配置文件 -->

  2. <context-param>

  3. <param-name>contextConfigLocation</param-name>

  4. <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-

  5. INF/jason-servlet.xml</param-value>

  6. </context-param>

  7. <listener>

  8. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  9. </listener>

又如: --->自定义context-param,且自定义listener来获取这些信息


 
  1. <context-param>

  2. <param-name>urlrewrite</param-name>

  3. <param-value>false</param-value>

  4. </context-param>

  5. <context-param>

  6. <param-name>cluster</param-name>

  7. <param-value>false</param-value>

  8. </context-param>

  9. <context-param>

  10. <param-name>servletmapping</param-name>

  11. <param-value>*.bbscs</param-value>

  12. </context-param>

  13. <context-param>

  14. <param-name>poststoragemode</param-name>

  15. <param-value>1</param-value>

  16. </context-param>

  17. <listener>

  18. <listener-class>com.laoer.bbscs.web.servlet.SysListener</listener-class>

  19. </listener>

自定义listener


 
  1. public class SysListener extends HttpServlet implements ServletContextListener {

  2. private static final Log logger = LogFactory.getLog(SysListener.class);

  3. public void contextDestroyed(ServletContextEvent sce) {

  4. //用于在容器关闭时,操作

  5. }

  6. //用于在容器开启时,操作

  7. public void contextInitialized(ServletContextEvent sce) {

  8. String rootpath = sce.getServletContext().getRealPath("/");

  9. System.out.println("-------------rootPath:"+rootpath);

  10. if (rootpath != null) {

  11. rootpath = rootpath.replaceAll("\\\\", "/");

  12. } else {

  13. rootpath = "/";

  14. }

  15. if (!rootpath.endsWith("/")) {

  16. rootpath = rootpath + "/";

  17. }

  18. Constant.ROOTPATH = rootpath;

  19. logger.info("Application Run Path:" + rootpath);

  20. String urlrewrtie = sce.getServletContext().getInitParameter("urlrewrite");

  21. boolean burlrewrtie = false;

  22. if (urlrewrtie != null) {

  23. burlrewrtie = Boolean.parseBoolean(urlrewrtie);

  24. }

  25. Constant.USE_URL_REWRITE = burlrewrtie;

  26. logger.info("Use Urlrewrite:" + burlrewrtie);

  27. 其它略之....

  28. }

  29. }

 上面大致理解其作用和过程,后面在

<servlet>
        <servlet-name>initExmple</servlet-name>
        <servlet-class>com.servlet.Exmple</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>

 

后在initExmple类中使用@Resource时总是出错,只能使用

 WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext()); 
 RedisUtil redisUtil = (RedisUtil)wac.getBean("redisUtil");才可以

 

@Resource

 RedisUtil redisUtil;是会报javax.naming.NamingException: Cannot create resource instance异常

1、Servlet 三大组件 Servlet、Filter、Listener 在传统项目中需要在 web.xml 中进行相应的配置。Servlet 3.0 开始在 javax.servlet.annotation 包下提供 3 个对应的 @WebServlet、@WebFilter、@WebListener 注解来简化操作。

2、@WebServlet、@WebFilter、@WebListener 写在对应的 Servlet、Filter、Listener 类上作为标识,从而不需要在 web.xml 中进行配置了。

3、应用中这三个注解默认是不被扫描的,需要在项目启动类上添加 @ServletComponentScan 注解, 表示对 Servlet 组件扫描。

 

Servlet介绍

1.Servlet不是框架,它是java库里面的一个类,Servlet是服务器端运行的一个程序。

2.当web容器启动的时候并且执行的时候,Servlet类就会被初始化。

3.用户通过浏览器输入url时,请求到达Servlet来接收并且根据servlet配置去处理。

通常项目中会用到不同的web容器,我这里用到是比较常见的Tomcat。在eclipse里面创建一个java web项目,会有一个WEB-INF的文件夹,为了不轻易被外界访问到,这个文件夹底下的文件都是受保护的。文件夹中包括了一个很重要的配置文件,web.xml,我们要实现的不同Servlet也要在这里配置才能使用。servlet在web.xml中的配置如下:

1

2

3

4

5

6

7

8

9

10

11

12

<servlet> 

     <servlet-name>LoginServlet</servlet-name>                                3

 

    <servlet-class>demo.servlet.LoginServlet</servlet-class>               4

</servlet> 

 

<servlet-mapping> 

    <servlet-name>LoginServlet</servlet-name>                                 2

 

    <url-pattern>login</url-pattern>                                                   1

 

</servlet-mapping> 

 访问顺序为1—>2—>3—>4,其中2和3的值必须相同。

url-pattern  标签中的值是要在浏览器地址栏中输入的 url,可以自己命名,这个 url 访问名为 servlet-name 中值的 servlet,两个  servlet-name 标签的值必须相同,因为通过 servlet 标签中的 servlet-name 标签映射到 servlet-class  标签中的值,最终访问 servlet-class 标签中的 class。

 

<servlet>
        <servlet-name>contextLoader</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>contextLoader</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file>
    </welcome-file-list>(路径入口)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值