servlet 3.0中三大组件的注册

三大组件(Servlet,Filter,Listener)的传统的注册

 

 

1、创建三大组件

package com.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MyServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
   
    public MyServlet() {
        super();
    }

    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        doGet(request, response);
    }

}

 

package com.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFilter implements Filter{
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String param01=filterConfig.getInitParameter("param01");
        String param02=filterConfig.getInitParameter("param02");
        System.out.println(param01);
        System.out.println(param02);
    }

    @Override
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
            throws IOException, ServletException {
        System.out.println("过滤请求");
        arg2.doFilter(arg0, arg1);
    }
    
    @Override
    public void destroy() {
        System.out.println("过滤请求完成");
        Filter.super.destroy();
    }

}

 

package com.web.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;



public class MyListener implements ServletContextListener {

    
    public MyListener() {
      
    }

    
    public void contextDestroyed(ServletContextEvent sce)  { 
       
    }

    
    public void contextInitialized(ServletContextEvent sce)  { 
        sce.getServletContext();
        System.out.println("ServletContext创建开始");
    }
    
}

 2、在web.xml中进行注册

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">

<!-- 注册监听器 -->
<listener>
    <listener-class>com.web.listener.MyListener</listener-class>
</listener>

<!-- 注册Servlet -->
<servlet>
    <servlet-name>myServlet</servlet-name>
    <servlet-class>com.web.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
      <servlet-name>myServlet</servlet-name>
      <url-pattern>/myServlet</url-pattern>
 </servlet-mapping>
    

    
    <!-- 注册过滤器 -->
    <filter>
        <filter-name>myFilter</filter-name>
        <filter-class>com.web.filter.MyFilter</filter-class>
        <init-param>
            <param-name>param01</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>
            <param-name>param02</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>myFilter</filter-name>
        <url-pattern>/myServlet</url-pattern>
    </filter-mapping>
     
     
</web-app>

 

 Servlet 3.0之后注册三大组件(可以采用注解,注解不介绍)

在实现javax.servlet.ServletContainerInitializer接口的实现类中进行注册

 

package com.tomcat.learn;

import java.util.Set;

import com.web.filter.MyFilter;
import com.web.listener.MyListener;
import com.web.servlet.MyServlet;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

public class ContainerInitializer implements ServletContainerInitializer{

    @Override
    public void onStartup(Set<Class<?>> arg0, ServletContext arg1) throws ServletException {
        //注册Servlet
        ServletRegistration.Dynamic myServlet=arg1.addServlet("myServlet", MyServlet.class);
        myServlet.addMapping("/myServlet");
        
        //注册过滤器
        FilterRegistration.Dynamic myFilter= arg1.addFilter("myFilter", MyFilter.class);
        myFilter.addMappingForUrlPatterns(null, false, "/myServlet");
        
        //注册Listener
        arg1.addListener(MyListener.class);
        
        
    }

}

 如果时在Spring应用中,还可以在org.springframework.web.WebApplicationInitializer实现类中的onStartup方法中进行注册

 

 

 

posted @ 2020-02-17 20:37  阿瞒123  阅读(268)  评论(0编辑  收藏  举报