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 ...
随机推荐
- SpringMVC(三) RequestMapping修饰类
SpringMVC使用@RequestMapping 注解为控制器指定可以处理哪些URL请求. 可以用于类定义以及方法定义: 类定义:提供初步的请求映射信息.相对于WEB应用的根目录. 方法处:提供进 ...
- left join 多个表关联时,将表值置换
/****** Script for SelectTopNRows command from SSMS ******/ SELECT B.[GOODSID] ,A.INDUSTRY_CNAME ,C. ...
- jQuery 追加元素的方法如append、prepend、before,after(转)
1.jQuery append() 方法 jQuery append() 方法在被选元素的结尾插入内容. 实例 复制代码代码如下: $("p").append("Some ...
- JS简单解决并发量
经常在写代码的时候碰到这样的场景:页面初始化时显示loading页,同时启动多个ajax并发请求获取数据,当每个ajax请求返回时结束loading. 举个例子,一个下订单的页面,要查询常用地址信息. ...
- log4j 配置INFO 和DEBUG 分布输出至两个文件
博客地址: http://blog.csdn.net/wangchsh2008/article/details/8812857
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 命名困惑系列之一:关于state和status的粗浅研究
牛津高阶词汇的解释 state: CONDITION OF SB/STH 状态:the mental,emotional or physical condition that a person or ...
- clang: error: linker command failed with exit code 1 (use -v to see invocati
安装了 pod 的项目,如果仍旧打开 xcodeproject 文件, 运行时会弹出此 bug
- NoSql basic knowledge
The big picture to keep in mind first is: There are lots of articles and resources out there: http:/ ...
- ORA-12560: TNS: 协议适配器错误
解决方案: 在开始菜单搜索 services.msc,进入本地服务 将 OracleOraDb11g_home1ClrAgent,OracleOraDb11g_home1TNSListener,Ora ...