• SpringBoot(10) Servlet3.0的注解:自定义原生Servlet、自定义原生Listener


    一、自定义原生Servlet

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

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

     1 @WebServlet(name = "userServlet",urlPatterns = "/v1/api/test/customs")
     2 public class UserServlet extends HttpServlet{
     3 
     4      @Override
     5      public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     6          
     7          resp.getWriter().print("custom sevlet");
     8          resp.getWriter().flush();
     9          resp.getWriter().close();
    10      }
    11 
    12     @Override
    13     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
    14             throws ServletException, IOException {
    15         this.doGet(req, resp);
    16     }
    17     
    18 }

    二、自定义原生Listener

    常用的监听器 servletContextListener、httpSessionListener、servletRequestListener

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

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

     1 @WebListener
     2 public class CustomContextListner implements ServletContextListener{
     3 
     4     @Override
     5     public void contextInitialized(ServletContextEvent sce) {
     6         System.out.println("======contextInitialized========");
     7         //启动时触发
     8     }
     9 
    10     @Override
    11     public void contextDestroyed(ServletContextEvent sce) {
    12         System.out.println("======contextDestroyed========");
    13         
    14     }
    15 
    16 }
     1 @WebListener
     2 public class RequestListener implements ServletRequestListener {
     3 
     4     @Override
     5     public void requestDestroyed(ServletRequestEvent sre) {
     6         // TODO Auto-generated method stub
     7         System.out.println("======requestDestroyed========");
     8     }
     9 
    10     @Override
    11     public void requestInitialized(ServletRequestEvent sre) {
    12         System.out.println("======requestInitialized========");
    13         
    14     }
    15 
    16 }
  • 相关阅读:
    C++内存管理
    多线程和多进程的区别(C++)
    如何用C语言封装 C++的类,在 C里面使用
    C/C++将一个整型数组拼接成一个字符串
    C代码中如何调用C++ C++中如何调用C
    Application对象的使用-数据传递以及内存泄漏
    《鸟哥的Linux私房菜》读书笔记二
    《鸟哥的Linux私房菜》读书笔记一
    greenDaoMaster的学习研究
    Handler 引起的内存泄露
  • 原文地址:https://www.cnblogs.com/platycoden/p/9810569.html
Copyright © 2020-2023  润新知