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("<html>");
out.println("<head>");
out.println("<title>Hello World</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>这是:MyServlet1</h1>");
out.println("</body>");
out.println("</html>");
} }

然后在 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("<html>");
out.println("<head>");
out.println("<title>Hello World</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>这是:myServlet2</h1>");
out.println("</body>");
out.println("</html>");
} }

测试: 可通过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
*/
@WebListener
public 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
*/
@WebListener
public 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);

springboot-11-servlet, listener, fitlter的添加的更多相关文章

  1. springboot之filter/listener/servlet

    简介 SpringBoot可以简化开发流程,但是在其中如何使用传统的J2EE servlet/listener/filter呢 @Bean配置 在Configuration类中加入filter和ser ...

  2. SpringBoot 配置 Servlet、Filter、Listener

    SpringBoot 配置 Servlet.Filter.Listener 在SpringBoot应用中,嵌入式的 Servlet 3.0+ 容器不会直接使用 ServletContainerInit ...

  3. SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet有两种方式: 1.通过注解扫描完成Servlet组件的注册: 2.通过方法完成Servlet组件的注册: 现在简单记录一下两种方式的实现 1.通过注解扫描完成Se ...

  4. springboot整合servlet

    在idea新建项目 这个是pom.xml文件需要添加的依赖包 <properties> <java.version>1.8</java.version> </ ...

  5. SpringBoot嵌入式Servlet配置原理

    SpringBoot嵌入式Servlet配置原理 SpringBoot修改服务器配置 配置文件方式方式修改,实际修改的是ServerProperties文件中的值 server.servlet.con ...

  6. 【SpringBoot】03.SpringBoot整合Servlet的两种方式

    SpringBoot整合Servlet的两种方式: 1. 通过注解扫描完成Servlet组件注册 新建Servlet类继承HttpServlet 重写超类doGet方法 在该类使用注解@WebServ ...

  7. 2、SpringBoot整合之SpringBoot整合servlet

    SpringBoot整合servlet 一.创建SpringBoot项目,仅选择Web模块即可 二.在POM文件中添加依赖 <!-- 添加servlet依赖模块 --> <depen ...

  8. Servlet, Listener 、 Filter.

    Java Web的三大组件:Servlet, Listener . Filter. 使用Listener监听器:八大监听器: 第一组:用于监听Servlet三个域对象的创建与销毁 1. Servlet ...

  9. 【使用篇二】SpringBoot整合Servlet(1)

    两种方式: 通过注解扫描完成 Servlet组件的注册 通过方法完成 Servlet组件的注册 一.通过注解扫描完成 Servlet 组件的注册 1. 编写Servlet类 /** * SpringB ...

随机推荐

  1. windows下用C++获取本机IP地址

    BSTR CamUtilsCtrl::GET_TERM_IP(void){ AFX_MANAGE_STATE(AfxGetStaticModuleState()); CString strResult ...

  2. Delphi 动态与静态调用DLL(最好的资料)

    摘要:本文阐述了 Windows 环境下动态链接库的概念和特点,对静态调用和动态调用两种调用方式作出了比较,并给出了 Delphi 中应用动态链接库的实例. 一.动态链接库的概念    动态链接库(  ...

  3. 在Windows 7上安装Team Foundation Server(TFS)的代理服务器(Agent)

    自2009年微软发布Windows 7以来,经过8年的市场验证,Windows 7已经成为史上应用最为广泛的操作系统.但是面对技术变化的日新月异,2015年微软正式停止了对Windows 7的主流支持 ...

  4. android应用搬家的实现

    android手机上的应用搬家功能,具体的介绍和原理参考: 系统目录及应用搬家的研究 应用搬家的实现 这里主要是作为一个补充,因为上面两篇文章虽然讲的挺好的,但是给出的例子不能直接运行,还是需要一些准 ...

  5. MySQL远程连接失败,MySQL远程连接出现Using password:YES错误的解决办法

    相信很多实用MYSQL的朋友都遇到过这种问题,就是MySQL使用localhost能够连接成功,但是使用IP连接却出现Using password:YES或者其它的连接错误.今天就把解决方法给大家说一 ...

  6. VS2017 无法使用"XXX"附加到应用程序

    可能是启用了腾讯的网游,可以关闭游戏,再试一下,如果还是不行,重启一下就可以了.好像是游戏的什么防篡改的作用

  7. 极大提高Web开发效率的8个工具和建议(含教程)

    面对复杂的 Web 应用的开发,良好的流程和工具支持是必不可少的,它们可以让日常的开发工作更加顺畅.更加高效.本文介绍了6个Web开发利器以及相关的教程,帮助你在开发.调试.集成和发布过程极大地提高效 ...

  8. centos7 安装django

    环境:centos7.4  ,python用的venv 3.6 ,django 2.1 注意点:django2.2再使用venv环境的时候会报SQList版本不足问题,升级数据库太费劲,不如重来 安装 ...

  9. Elasticsearch java API (23)查询 DSL Geo查询

    地理查询编辑 Elasticsearch支持两种类型的地理数据: geo_point纬度/经度对字段的支持,和 geo_shape领域,支持点.线.圆.多边形.多等. 这组查询: geo_shape  ...

  10. webpack快速入门——集中拷贝静态资源

    工作中会有一些已经存在但在项目中没有引用的图片资源或者其他静态资源(比如设计图.开发文档), 这些静态资源有可能是文档,也有可能是一些额外的图片.项目组长会要求你打包时保留这些静态资源, 直接打包到制 ...