简介

SpringBoot可以简化开发流程,但是在其中如何使用传统的J2EE servlet/listener/filter呢

@Bean配置

在Configuration类中加入filter和servlet的registration

@Bean
public FilterRegistrationBean registFilter() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new Filter(){
@Override
public void init(FilterConfig filterConfig) throws ServletException {
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("in filter");
chain.doFilter(request, response);
} @Override
public void destroy() {
} });
registration.addUrlPatterns("/*");
registration.setOrder(1);
return registration;
} @SuppressWarnings("serial")
@Bean
public ServletRegistrationBean registServlet() {
ServletRegistrationBean servletRegist=new ServletRegistrationBean();
servletRegist.setServlet(new HttpServlet (){
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getWriter().write("inside servlet");
}
});
servletRegist.addUrlMappings("/registedServlet");
return servletRegist;
}

运行项目就可以访问servlet和filter了,在registrationBean中可以配置路径和实例。

使用Servlet3.0

如果使用Servlet3.0的话,可以在Configuration类上加@ServletComponentScan("类路径")

并开发对应的filter、listener、servlet

@WebFilter
public class CustFilter implements Filter{ @Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub } @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("in filter2");
chain.doFilter(request, response);
} @Override
public void destroy() {
// TODO Auto-generated method stub } } @WebListener
public class CustListener implements HttpSessionListener { @Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("session created");
} @Override
public void sessionDestroyed(HttpSessionEvent se) {
// TODO Auto-generated method stub }
} @WebServlet(urlPatterns={"/cust"})
public class CustServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getSession().setAttribute("a", "1");
resp.getWriter().write("cust servlet");
}
}

在访问localhost:8080/cust时,对应的filter、listener、servlet都会被访问到

springboot之filter/listener/servlet的更多相关文章

  1. Javaweb里面的filter,listener,servlet

    Filter 1Filter是什么:是过滤器简称 2Filter有什么作用:在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装 ...

  2. springboot添加对listener,servlet,filter的支持

    比较常用的方式就是使用注解来添加对 监听器,过滤器,servlet的支持. 1.首先在启动类上添加  @ServletComponentScan  开启 对监听器,过滤器,servlet的注解扫描. ...

  3. tomcat及springboot实现Filter、Servlet、Listener

    tomcat实现: 核心类org.apache.catalina.startup.ContextConfig //支持注解 see:org.apache.catalina.deploy.WebXml ...

  4. Spring boot 注册Filter , Listener, Servlet

    1: ServletRegistrationBean   Servlet @Bean public ServletRegistrationBean myServlet(){ ServletRegist ...

  5. spring mvc 的配置 及interceptor filter listener servlet 配置

    创建 三个类 分别实现 Filter  ServletContextListener  HttpServlet 在springboot 启动类中@bean加入 2 ,实现 ServletContext ...

  6. SpringBoot初始教程之Servlet、Filter、Listener配置(七)

    1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...

  7. SpringBoot初始教程之Servlet、Filter、Listener配置

    1.介绍通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个jar ...

  8. SpringBoot初始教程之Servlet、Filter、Listener配置详解

    1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...

  9. SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式

    在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...

随机推荐

  1. 初识Angular

    一.AngularJs简介 1.AngularJS使用了不同的方法,它尝试去补足HTML本身在构建应用方面的缺陷.AngularJS通过使用我们称为标识符(directives)的结构,让浏览器能够识 ...

  2. JavaScript(二) DOM

    当网页被加载时,浏览器会创建页面的文档对象模型(Document Object Model)通过 HTML DOM,可访问 JavaScript HTML 文档的所有元素. 通过 id 查找 HTML ...

  3. Vue - 内部指令

    1.插值 A:<span>TEXT:{{text}}</span> {{text}}会被相应的数据类型text属性值替换,当text值改变时候,文本中的值也会相应的发生变化 B ...

  4. Gym 100646 F Tanks a Lot RMQ

    Problem F: Tanks a Lot Imagine you have a car with a very large gas tank - large enough to hold what ...

  5. Xcode 8 Simulator Stop Logging too much info

    按照以下内容设置即可:

  6. jQuery参数学习与整理

    bind---可同时为元素嵌套多个事件. blur---当输入框焦点失去时发生的事件(获得焦点参数focus与之同理) change---当元素值改变时发生的事件 click---单击事件 dbcli ...

  7. kindeditor-4.1.3工具使用技巧:如何在编辑区上传图片并保存绝对路径

    MVC项目开中用到了KindEditor图片上传工具,需要在编辑区内上传图片,并将图片的URL保存为符合如下格式地址,如http://192.168.0.111/uploadImg/imgName.j ...

  8. C# 中对 ArrayList 的排序

    ArrayList 元素 //目录条目类 public class FolderItem { public string filename; public string filetype; publi ...

  9. HDU 2492 Ping pong (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=2000 ...

  10. zepto区别于jquery获取select表单选中的值

    在jquery下,我们获取select表单选中的值通常是通过$('select').val()来实现,这样的方式简单又明了,或者通过$('select option[selected]').text( ...