Servlet创建的三种方式

源代码路径:https://git.oschina.net/kxtry/servletwaycreate
以下是基于servlet3.0API而创建的。
0.pom.xml的准备


        
            javax.servlet
            javax.servlet-api
            3.0.1
        
    

1.ServletContainerInitializer的方式。

在resources/META-INF/services/javax.servlet.ServletContainerInitializer文件中增加以下内容。
HandlerInitializer,如果是有包名,则需要添加上包名如hello.HandlerInitializer
public class ServletManualReg extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("ServletManualReg.hello");
    }
}
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import javax.servlet.annotation.HandlesTypes;
import java.util.Set;

@HandlesTypes({HandlerInitializer.class})
public class HandlerInitializer implements ServletContainerInitializer {
    public void onStartup(Set> c, ServletContext servletContext)
            throws ServletException {
        System.out.println("onStartup");
        ServletRegistration registration = servletContext.addServlet("ServletManualReg", "hello.ServletManualReg");
        registration.addMapping("/manual_reg");
        System.out.println("leaving onStartup");
    }
}

2.使用ServletContextListener注册

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

public class DynRegServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.getWriter().print("DynRegServlet.hello");
    }
}
import javax.servlet.*;
import javax.servlet.annotation.WebListener;

@WebListener
public class DynRegListener implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent arg0) {
    }

    public void contextInitialized(ServletContextEvent sce) {
        ServletContext servletContext = sce.getServletContext() ;
        Servlet dynRegServlet = null ;
        try {
            dynRegServlet = servletContext.createServlet(DynRegServlet.class) ;
        } catch (ServletException e) {
            e.printStackTrace();
        }
        ServletRegistration.Dynamic dynamic = servletContext.addServlet("DynRegServlet", dynRegServlet) ;
        dynamic.addMapping("/dynamic_reg") ;
    }
}

3.使用WebServlet注解注册


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

/**
 * 使用@WebServlet实现了,自动注册。
 */
@WebServlet(name = "ServletNotationReg", urlPatterns = "/notation_reg")
public class ServletNotationReg extends HttpServlet {
    public ServletNotationReg(){
        int i = 0;
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().print("ServletNotationReg.hello");
    }
}