一、自定义原生Servlet

1、启动类里面增加注解 @ServletComponentScan

2、Servlet上添加注解  @WebServlet(name = "userServlet",urlPatterns = "/v1/api/test/customs")

 @WebServlet(name = "userServlet",urlPatterns = "/v1/api/test/customs")
public class UserServlet extends HttpServlet{ @Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().print("custom sevlet");
resp.getWriter().flush();
resp.getWriter().close();
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
} }

二、自定义原生Listener

常用的监听器 servletContextListener、httpSessionListener、servletRequestListener

servletContextListener:服务启动,一般用于初始化加载

servletRequestListener:访问启动,一般用于统计

 @WebListener
public class CustomContextListner implements ServletContextListener{ @Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("======contextInitialized========");
//启动时触发
} @Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("======contextDestroyed========"); } }
 @WebListener
public class RequestListener implements ServletRequestListener { @Override
public void requestDestroyed(ServletRequestEvent sre) {
// TODO Auto-generated method stub
System.out.println("======requestDestroyed========");
} @Override
public void requestInitialized(ServletRequestEvent sre) {
System.out.println("======requestInitialized========"); } }
04-14 12:39