博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot-11-servlet, listener, fitlter的添加
阅读量:4558 次
发布时间:2019-06-08

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

springboot中添加servlet, filter, listener有2种方式: 代码注册servlet 和自动注解注册(在使用druid监控有使用过)

代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。 也可以通过servletContextInitializer接口直接注册

在 SpringBootApplication 上使用@ServletComponentScan注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码。

编写测试类: 

Servlet:

1), 使用代码注册的方式

package com.iwhere.test.web.servlet;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * 通过代码注册servlet * 代码注册通过ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制。     也可以通过实现 ServletContextInitializer 接口直接注册。 * @author wenbronk * @time 2017年3月17日  下午2:23:21  2017 */public class MyServlet extends HttpServlet {    private static final long serialVersionUID = 6272693061640286219L;    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//        super.doGet(req, resp);        System.out.println("method deGet is run");        doPost(req, resp);    }        @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//        super.doPost(req, resp);        System.out.println("method doPost is run");                resp.setContentType("text/html");         PrintWriter out = resp.getWriter();         out.println("");         out.println("");         out.println("Hello World");         out.println("");         out.println("");         out.println("

这是:MyServlet1

"); out.println(""); out.println(""); } }

然后在 App.java (或者任意一类中, 该类必须@component被spring管理) 

/**     * 使用代码注册Servlet     * 此方法不需要加注解  @ServletComponentScan     * @return     */    @Bean    public ServletRegistrationBean myServlet() {         return new ServletRegistrationBean(new MyServlet(),"/myServlet/*");    }

2), 使用注解的方式实现: 

package com.iwhere.test.web.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;/** * 使用注解的方式注册servlet *  * @author wenbronk * @time 2017年3月17日 下午4:10:33 2017 */@WebServlet(urlPatterns = "/myServlet2/*", description = "Servlet注解方式")public class AnnotionServlet extends HttpServlet {    private static final long serialVersionUID = 1121465382990088487L;    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//        super.doGet(req, resp);        System.err.println("do Get");        doPost(req, resp);    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//        super.doPost(req, resp);        System.err.println("do post");        resp.setContentType("text/html");        PrintWriter out = resp.getWriter();        out.println("");        out.println("");        out.println("Hello World");        out.println("");        out.println("");        out.println("

这是:myServlet2

"); out.println(""); out.println(""); }}

测试: 可通过http://localhost:8080/springboot-learn/myServlet进行访问: (有加context配置)

 

Filter的配置:

此处只讲注解方式, 代码方式和servlet一样

package com.iwhere.test.web.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;/** * 使用注解的方式创建filter, 实现一个filter接口 * 必须加注解@ServletComponentScan * @author wenbronk * @time 2017年3月17日  下午4:48:19  2017 */@WebFilter(filterName="myFilter", urlPatterns="/*")public class MyFilter implements Filter {    @Override    public void destroy() {        System.out.println("filter is destroy");    }    @Override    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)            throws IOException, ServletException {        System.out.println("running filter");        arg2.doFilter(arg0, arg1);    }    @Override    public void init(FilterConfig arg0) throws ServletException {        System.out.println("filter is init");    }    }

之后必须在main类上添加包扫描或者指定类

@SpringBootApplication@EnableScheduling@EnableTransactionManagement(proxyTargetClass = true)@ServletComponentScan(basePackageClasses = {com.binfo.sqgf.config.SqlInjectFilter.class})public class SqgfApplication {

或者

 @SpringBootApplication

@EnableAutoConfiguration@EnableWebMvc@ServletComponentScan(basePackages = "com.fanyin.eghm")public class EghmApplication {    public static void main(String[] args) {        SpringApplication.run(EghmApplication.class, args);    }}

 

 

listener

listener有contextListener, sessionListener,pageListener, beanListener 好多种

servletContextListener;

package com.iwhere.test.web.listener;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;import javax.servlet.annotation.WebListener;/** * 使用注解的方式实现Contextlistener, 必须实现listener接口 *  * @author wenbronk * @time 2017年3月17日  下午4:51:24  2017 */@WebListenerpublic class MyListener implements ServletContextListener{    @Override    public void contextDestroyed(ServletContextEvent arg0) {        System.out.println("servletContext destroyed");    }    @Override    public void contextInitialized(ServletContextEvent arg0) {        System.out.println("servletContext init");    }}

sessionListener

package com.iwhere.test.web.listener;import javax.servlet.annotation.WebListener;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;/** * sessionListener, 监听session的创建和销毁 * @author wenbronk * @time 2017年3月17日  下午4:56:22  2017 */@WebListenerpublic class MySessionListener implements HttpSessionListener {    @Override    public void sessionCreated(HttpSessionEvent arg0) {        System.out.println("HttpSession created");    }    @Override    public void sessionDestroyed(HttpSessionEvent arg0) {        System.out.println("HttpSession destroyed");            }}

 

补充一点, 在listener中使用 springboot的 工厂

mongoTemplate = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()).getBean(MongoTemplate.class);

 

转载于:https://www.cnblogs.com/wenbronk/p/6567262.html

你可能感兴趣的文章
Spring基础2
查看>>
【灵异短篇】这个夜晚有点凉
查看>>
一点小问题
查看>>
pytest 10 skip跳过测试用例
查看>>
MVC身份验证及权限管理
查看>>
It was not possible to find any compatible framework version
查看>>
关于8.0.15版本的mysql下载与安装
查看>>
Redis主从复制看这篇就够了
查看>>
洛谷 P1202 [USACO1.1]黑色星期五Friday the Thirteenth 题解
查看>>
(4.20)SQL Server数据库启动过程,以及启动不起来的各种问题的分析及解决技巧...
查看>>
基本数据类型(数字和字符串)
查看>>
函数__装饰器
查看>>
linux system函数分析
查看>>
前端优化措施
查看>>
论学习汉语和学习编程的异同点
查看>>
linux img文件压缩及解压
查看>>
Linux 下的 scp
查看>>
理解同步,异步和延迟脚本
查看>>
MMS源码中异步处理简析
查看>>
XMind 6 如何画流程图
查看>>