servlet3.0的学习(四)

动态注册Servlets,Filters,Listeners

TestFilter.java
package web.servlet.dynamicregistration_war;

import java.io.*;
import javax.servlet.*;


public class TestFilter implements Filter {

    private String filterInitParam;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        filterInitParam = filterConfig.getInitParameter("filterInitName");
    }   

    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        req.setAttribute("filterInitName", filterInitParam);
        chain.doFilter(req, res);
    }

    @Override
    public void destroy() {
        // Do nothing
    }
}


TestServlet.java
package web.servlet.dynamicregistration_war;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class TestServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res)
            throws IOException, ServletException {

        if (!"servletInitValue".equals(getServletConfig().getInitParameter(
                        "servletInitName"))) {
            throw new ServletException("Missing servlet init param");
        }

        if (!"filterInitValue".equals(req.getAttribute("filterInitName"))) {
            throw new ServletException("Missing request attribute that was " +
                "supposed to have been set by programmtically registered " +
                "Filter");
        }

        if (!"listenerAttributeValue".equals(req.getAttribute(
                "listenerAttributeName"))) {
            throw new ServletException("Missing request attribute that was " +
                "supposed to have been set by programmtically registered " +
                "ServletRequestListener");
        }

        res.getWriter().println("HELLO WORLD!\n");
    }
}


TestServletRequestListener.java
package web.servlet.dynamicregistration_war;

import javax.servlet.*;

public class TestServletRequestListener implements ServletRequestListener {

    public void requestInitialized(ServletRequestEvent sre) {
        sre.getServletRequest().setAttribute("listenerAttributeName",
            "listenerAttributeValue");
    }
    
    public void requestDestroyed(ServletRequestEvent sre) {
        // Do nothing
    }

}


TestServletContextListener.java
package web.servlet.dynamicregistration_war;

import java.util.*;
import javax.servlet.*;


public class TestServletContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {

        ServletContext sc = sce.getServletContext();

        // 注册 Servlet
        ServletRegistration sr = sc.addServlet("DynamicServlet",
            "web.servlet.dynamicregistration_war.TestServlet");
        sr.setInitParameter("servletInitName", "servletInitValue");
        sr.addMapping("/*");
        // 注册过滤器
        FilterRegistration fr = sc.addFilter("DynamicFilter",
            "web.servlet.dynamicregistration_war.TestFilter");
        fr.setInitParameter("filterInitName", "filterInitValue");
        fr.addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST),
                                     true, "DynamicServlet");

        // 注册监听器
        sc.addListener("web.servlet.dynamicregistration_war.TestServletRequestListener");
    }   

    @Override
    public void contextDestroyed(ServletContextEvent sce) {  
        // Do nothing
    }
}


你可能感兴趣的:(java,Web,servlet)