springboot之filter/listener/servlet
简介
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的更多相关文章
- Javaweb里面的filter,listener,servlet
Filter 1Filter是什么:是过滤器简称 2Filter有什么作用:在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以使用Decorator(装 ...
- springboot添加对listener,servlet,filter的支持
比较常用的方式就是使用注解来添加对 监听器,过滤器,servlet的支持. 1.首先在启动类上添加 @ServletComponentScan 开启 对监听器,过滤器,servlet的注解扫描. ...
- tomcat及springboot实现Filter、Servlet、Listener
tomcat实现: 核心类org.apache.catalina.startup.ContextConfig //支持注解 see:org.apache.catalina.deploy.WebXml ...
- Spring boot 注册Filter , Listener, Servlet
1: ServletRegistrationBean Servlet @Bean public ServletRegistrationBean myServlet(){ ServletRegist ...
- spring mvc 的配置 及interceptor filter listener servlet 配置
创建 三个类 分别实现 Filter ServletContextListener HttpServlet 在springboot 启动类中@bean加入 2 ,实现 ServletContext ...
- SpringBoot初始教程之Servlet、Filter、Listener配置(七)
1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...
- SpringBoot初始教程之Servlet、Filter、Listener配置
1.介绍通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个jar ...
- SpringBoot初始教程之Servlet、Filter、Listener配置详解
1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...
- SpringBoot学习笔记(6)----SpringBoot中使用Servlet,Filter,Listener的三种方式
在一般的运用开发中Controller已经大部分都能够实现了,但是也不排除需要自己实现Servlet,Filter,Listener的方式,SpringBoot提供了三种实现方式. 1. 使用Bean ...
随机推荐
- 学习微信小程序之css9内边距
padding内边距 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- D3中数据与DOM element绑定之data() enter() exit()浅析
几个非常有用的links: [1] three little circles. http://bost.ocks.org/mike/circles/ [2] How selection works. ...
- Service and controller in angularJs
Separation of concern is at the heart while designing an AngularJS application. Your controller must ...
- 第四章 springboot + swagger
http://www.cnblogs.com/java-zhao/p/5348113.html
- 4.3 多线程进阶篇<中>(GCD)
更正:队列名称的作用的图中,箭头标注的有些问题,已修正 本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 本文源码 Demo 详见 Gith ...
- REGEXP 正则的实现两个字符串组的匹配。(regexp)
主要懂3个mysql的方法:replace[替换] regexp[正则匹配] concat[连接] 由于某些原因,有时候我们没有按照范式的设计准则而把一些属性放到同一个字符串字段中.比如 ...
- Mac无法写入移动硬盘
1.使用mac 磁盘工具,格式化移动硬盘 频繁穿插于mac于pc之间者,大批量拷贝大型文件者,请用exfat 首先,排除Fat32,虽然这是兼容性最好的文件格式,但一个不支持4g以上文件的格式(现在随 ...
- Python中的map( )和reduce( )
1.变量可以指向函数,也可以使用变量和参数的形式完成函数调用. 2.那么函数名是什么呢?函数名其实就是指向函数的变量!对于abs()这个函数,完全可以把函数名abs看成变量,它指向一个可以计算 ...
- 【JBOSS】数据库连接配置小结
数据库驱动位置: %JBOSS_HOME%\server\default\lib目录下. 数据库配置文件位置:JBOSS_HOME\docs\examples\jca\XXXX-ds.xml < ...
- CentOS 7 学习笔记(一)时间管理
1 获取当前时间 [root@limt01 ~]# date 2015年 05月 22日 星期五 01:30:50 CST 2 获取当前日期 [root@limt01 ~]# date "+ ...