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 ...
随机推荐
- If & Else 语句
If 语句 user= "吹Sir"passwd= "strong"username = input ("Username:")passwo ...
- Android下的屏幕适配
1080 100dp 300px720 100dp 200px 300px 1080px 比例是300/1080=0.277200px 720px
- windows 常用命令整合--脚本工具
到年终了,手里活不多了,平时就想着将平时一些常用的命令整合一下,于是下面的一个小小脚本就出来了... 好了,直接上菜:(http://files.cnblogs.com/files/hsuchan/c ...
- 国外程序员整理的Java资源大全分享
Java 几乎是许多程序员们的入门语言,并且也是世界上非常流行的编程语言.国外程序员 Andreas Kull 在其 Github 上整理了非常优秀的 Java 开发资源,推荐给大家. 译文由 Imp ...
- HDU5800 To My Girlfriend(DP)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5800 Description Dear Guo I never forget the mom ...
- 【转】查询oracle比较慢的session和sql
-查询最慢的sql select * from ( select parsing_user_id,executions,sorts command_type,disk_reads,sql_text f ...
- GRU(Gated Recurrent Unit) 更新过程推导及简单代码实现
GRU(Gated Recurrent Unit) 更新过程推导及简单代码实现 RNN GRU matlab codes RNN网络考虑到了具有时间数列的样本数据,但是RNN仍存在着一些问题,比如随着 ...
- JS /JQuery 获取变量为数字时 容易出错 可能不是数字类型
Javascript内置函数,原型为parseInt ( String s , [ int radix ] ),用于解析一个字符串,并返回一个整数. var $prod_kucun=$(this).n ...
- Linux Shell 脚本调试
方法如下所示:(1) 使用选项–x,启用shell脚本的跟踪调试功能: $ bash -x script.sh 运行带有-x标志的脚本可以打印出所执行的每一行命令以及当前状态.注意,你也可以使用sh ...
- CentOS利用postfix搭建邮件服务器
之前我用nodemailer通过163邮箱来发送邮件,不过没过几天就一直ETIMEDOUT,不知道什么原因,想着还是自己搭一个来发邮件可能靠谱点(flag?) 安装postfix CentOS 7 自 ...