Java web 加载过程

1.Web容器初始化过程

2.SpringMVC中web.xml配置

3.认识ServletContextListener

4.认识ContextLoaderListener

5.DispatcherServlet初始化(HttpServletBean • FrameworkServlet • DispatcherServlet)

6.ContextLoaderListener与DispatcherServlet关系

7.DispatcherServlet的设计

8.DispatcherServlet工作原理

 

 

web容器加载过程

1.启动web项目后,web容器首先回去找web.xml文件,读取这个文件。

2.容器会创建一个 ServletContext ( servlet 上下文),整个 web 项目的所有部分都将共享这个上下文。

3.容器将 转换为键值对,并交给 servletContext

4.容器创建 中的类实例,创建监听器。

5.容器加载filter,创建过滤器, 要注意对应的filter-mapping一定要放在filter的后面。

6.容器加载servlet,加载顺序按照 Load-on-startup 来执行

 

因此它的完整加载顺序就是 :ServletContext -> context-param -> listener-> filter -> servlet

 

介绍完了加载过程,在顺便了解下Servlet吧, 

1. 首先什么是servlet:

servlet是sun公司为开发动态web而提供的一门技术,用户若想用发一个动态web资源(即开发一个Java程序向浏览器输出数据),需要完成以下2个步骤: 
  1、编写一个Java类,实现servlet接口。 
  2、把开发好的Java类部署到web服务器中。 
  按照一种约定俗成的称呼习惯,通常我们也把实现了servlet接口的java程序,称之为Servlet 
 

2.servlet的运行过程:

1.浏览器发出请求,被web容器获取到 
2.Web服务器首先检查是否已经装载并创建了该Servlet的实例对象。如果是,则直接执行第④步,否则,执行第②步。 
3.装载并创建该Servlet的一个实例对象,调用Servlet实例对象的init()方法。 
4.创建一个用于封装HTTP请求消息的HttpServletRequest对象和一个代表HTTP响应消息的HttpServletResponse对象,然后调用Servlet的service()方法并将请求和响应对象作为参数传递进去。 
5.WEB应用程序被停止或重新启动之前,Servlet引擎将卸载Servlet,并在卸载之前调用Servlet的destroy()方法。

一般情况,servlet是在被请求的时候才去创建的,但是当添加时(Load-on-startup ),就会在初始化的时候创建它,利用这点特性,我们可以初始化创建数据库等等。

使用servlet时,一般都是继承httpServlet,然后分别实现doGet或者doPost方法,但是在这里面要注意的是,这servlet并不是线程安全的,多线程单实例执行的,当并发访问同一个资源的话,就有可能引发线程安全问题,

 

不过有一点需要注意的是: spring容器的加载要在servlet之后,因此在有些过滤器当中需要提前用到spring bean的时候,就需要改成 Listener 的方式

org.springframework.web.context.ContextLoaderListener

配置方式:

pring在web下的入口在配置文件web.xml的监听器中

<listener>
        <listener-class>
          org.springframework.web.context.ContextLoaderListener
        </listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
     <param-value>classpath:conf/spring/applicationContext.xml</param-value>
</context-param>

 

上述是在web.xml中的配置信息。

 

第一种情况:

public class UserAuthorizationFilter extends HttpServlet {

private WebApplicationContext wac;

    public void init(){
        方法一:wac =WebApplicationContextUtils.getRequiredWebApplicationContext(

             this.getServletContext());

        方法二:wac = WebApplicationContextUtils.getWebApplicationContext(
          this.getServletContext());

       方法一和方法二得到的结果是一样的。

     //wac的类型:org.springframework.web.context.support.XmlWebApplicationContext

    }

public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

     JdbcTemplate jdbcTemplate = (JdbcTemplate)wac.getBean("jdbcTemplate");

     String sql="select count(*) from customer where name='liwj' and password='1111111'";

     int num=jdbcTemplate.queryForInt(sql);
     if(num==1){   
       System.out.println("has");
      }else{   
      System.out.println("hasnot");     

   }

}

 

第二种情况:web.xml中配置spring中的filter

webx.xml中的配置

 <filter>
        <filter-name>myFilter</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>DelegatingFilterProxy</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

applicationContext.xml配置:

<bean id="myFilter" class="com.MyFilter"> //指名具体的filter类
    <property name="service">                    //需要注入的具体参数
        <ref bean="service"/>
    </property>
</bean> 

 

posted @ 2018-10-17 11:31  liuxm-刘小明  阅读(533)  评论(0编辑  收藏  举报