美文网首页
springboot配置servlet、过滤器、监听器

springboot配置servlet、过滤器、监听器

作者: 晓晓_1931 | 来源:发表于2022-12-26 10:16 被阅读0次
    创建springboot项目,maven工程,打包方式为jar包
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.3.3.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
            </dependency>
    
            <!-- servlet 依赖. 想在springboot中使用容器对象,必须引入此依赖-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <scope>provided</scope>
            </dependency>
        </dependencies>
    

    下面所有操作能被springboot接管,必须在主启动类上加@ServletComponentScan注解

    package com.boot;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
     
    @SpringBootApplication
    @ServletComponentScan//扫描当前包及其子包下的所有Servlet、Filter、Listener
    public class StartApplication {
         public static void main(String[] args) {
                SpringApplication.run(StartApplication.class, args);
            }
    }
    
    1、配置servlet
    创建一个MyServlet类,继承HttpServlet类
    package com.boot.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet(urlPatterns = "/myServlet")
    public class MyServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            // 创建响应输出流
            PrintWriter out = response.getWriter();
            // 向客户端响应信息
            out.println("<html>");
            out.println("  <head><title>Servlet</title></head>");
            out.println("  <body>");
            out.println("你好,欢迎来到Servlet的世界!");
            out.println("  </body>");
            out.println("</html>");
            // 关闭响应输出流
            out.close();
        }
     
    }
    
    
    2、配置Filter
    创建一个MyFilter类,实现Filter接口
    package com.boot.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;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebFilter(urlPatterns="/*")
    public class MyFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
        }
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
                throws IOException, ServletException {
            //功能
            System.out.println("过滤器执行了……");
            HttpServletRequest request = (HttpServletRequest)req;
            HttpServletResponse response = (HttpServletResponse)resp;
            //设置请求信息的字符编码(必须在使用request之前进行设置)
            request.setCharacterEncoding("utf-8");
            //使客户端浏览器并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
            response.setContentType("text/html;charset=utf-8");
            //设置响应信息的字符编码(必须在创建响应输出流之前进行设置)
            response.setCharacterEncoding("utf-8");
            
            //调用过滤器链中的下一个资源
            chain.doFilter(request, response);
        }
    
        @Override
        public void destroy() {
        }
    }
    
    
    3、配置Listener
    创建一个MyListener类,实现ServletContextListener接口
    package com.boot.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    @WebListener
    public class MyListener implements ServletContextListener {
        // 服务器启动时会自动触发此方法
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("服务启动了...");  
        }
    
        // 服务器停止时会自动触发此方法
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            System.out.println("服务停止了...");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:springboot配置servlet、过滤器、监听器

          本文链接:https://www.haomeiwen.com/subject/tglnqdtx.html