博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springBoot(5)---整合servlet、Filter、Listener
阅读量:4086 次
发布时间:2019-05-25

本文共 4119 字,大约阅读时间需要 13 分钟。

springBoot整合servlet、Filter、Listener需要在pom中引入如下包:

org.springframework.boot
spring-boot-starter-web

一、springBoot整合servlet

springBoot使用servlet的API有两种方法

方法1、使用@ServletComponentScan注解

package com.wzy.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;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(name = "myServlet",urlPatterns = "/myServlet")//上面这段配置声明该类为servlet程序,等同于在web.xml中配置
映射@ServletComponentScan //该注解的作用是让springBoot扫描@webServlet等注解@SpringBootApplication //声明为springBoot应用public class MyFirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("执行了MyFirstServlet的doGet方法......"); } //启动类 public static void main(String[] args) { SpringApplication.run(MyFirstServlet.class,args); }}

测试:浏览器中访问http://localhost:8080/myServlet,控制台打印结果如下:

方法2、使用@Bean注解

第1步、写一个HelloServlet类,继承HttpServlet类

package com.wzy.test;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 HelloServlet extends HttpServlet {    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        System.out.println("执行了HelloServlet的doGet方法......");    }}

第2步

package com.wzy.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class BeanTest {    public static void main(String[] args) {        SpringApplication.run(BeanTest.class,args);    }    //注册servlet    @Bean    public ServletRegistrationBean getServletRegistrationBean(){        ServletRegistrationBean bean=new ServletRegistrationBean(new HelloServlet());        //设置访问路径        bean.addUrlMappings("/helloServlet");        return bean;    }}

第3步、测试

浏览器输入:http://localhost:8080/helloServlet

控制台打印结果如下:

二、springBoot整合Filter

使用@WebFilter注解

package com.wzy.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.web.servlet.ServletComponentScan;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import java.io.IOException;@WebFilter(filterName = "myFilter",urlPatterns = "/myFilter")public class MyFilter implements Filter {    /**     * 初始化     * */    @Override    public void init(FilterConfig filterConfig) throws ServletException {    }    /**     * 执行拦截     * */    @Override    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {        System.out.println("执行了前面代码");        //放行,执行目标资源:/myFilter        filterChain.doFilter(servletRequest,servletResponse);        System.out.println("执行了后面代码");    }    /**     * 销毁     * */    @Override    public void destroy() {    }    public static void main(String[] args) {        SpringApplication.run(MyFilter.class,args);    }}

 

三、springBoot整合Listener

使用@WebListener注解

package com.wzy.test;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;@WebListenerpublic class MyListener implements ServletContextListener {    @Override    public void contextInitialized(ServletContextEvent sce) {        System.out.println("servletContext对象创建了");    }    @Override    public void contextDestroyed(ServletContextEvent sce) {        System.out.println("servletContext对象被销毁了");    }}

 

 

 

转载地址:http://fmuii.baihongyu.com/

你可能感兴趣的文章
Flutter Boost的router管理
查看>>
Android Flutter混合编译
查看>>
微信小程序 Audio API
查看>>
[React Native]react-native-scrollable-tab-view(进阶篇)
查看>>
Vue全家桶+Mint-Ui打造高仿QQMusic,搭配详细说明
查看>>
React Native for Android 发布独立的安装包
查看>>
React Native应用部署/热更新-CodePush最新集成总结(新)
查看>>
react-native-wechat
查看>>
基于云信的react-native聊天系统
查看>>
网易云音乐移动客户端Vue.js
查看>>
ES7 await/async
查看>>
ES7的Async/Await
查看>>
React Native WebView组件实现的BarCode(条形码)、(QRCode)二维码
查看>>
每个人都能做的网易云音乐[vue全家桶]
查看>>
JavaScript专题之数组去重
查看>>
Immutable.js 以及在 react+redux 项目中的实践
查看>>
Vue2.0全家桶仿腾讯课堂(移动端)
查看>>
React+Redux系列教程
查看>>
react-native 自定义倒计时按钮
查看>>
19 个 JavaScript 常用的简写技术
查看>>