码迷,mamicode.com
首页 > 编程语言 > 详细

Springboot - -web应用开发-Servlets, Filters, listeners

时间:2018-08-23 00:16:24      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:throws   gis   int   tps   return   private   注释   example   png   

一.Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 Filter、 Listener等等

二.在spring boot中的三种实现方式

 方法一:通过注册ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制

servlet类:

package com.demo.example.util.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;



public class CustomServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("servlet get method");
		doPost(request , response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("servlet get method");
		response.getWriter().write("Hello world");
	}
	

}

filter类:
技术分享图片
package com.demo.example.util.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;



public class CustonFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("do destroy");
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("do filter");
		chain.doFilter(request, response);

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("do init");
	}

}

 Listener类:

package com.demo.example.util.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class CustomListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		// TODO Auto-generated method stub
		System.out.println("servletContextEvent");

	}

	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		// TODO Auto-generated method stub
		System.out.println("servletContextEvent");
	}

}

 

注册bean(第一种方式)

代码注册:(在SpringBootDemoFilerServletLisApplication类中)

@Bean
	public ServletRegistrationBean<CustomServlet> servletRegistrationBean() {
		return new ServletRegistrationBean<CustomServlet>(new CustomServlet(), "/customServlet");
	}
	
	@Bean
	public FilterRegistrationBean<CustonFilter> filterRegistrationBean(){
		return new FilterRegistrationBean<CustonFilter>(new CustonFilter(),servletRegistrationBean() );
	}
	
	@Bean
	public ServletListenerRegistrationBean<CustomListener> servletListenerRegistrationBean(){
		return new ServletListenerRegistrationBean<CustomListener>(new CustomListener());
	}

 第二种代码注册:(类似一)

@SpringBootApplication
public class SpringBootDemoFilerServletLisApplication implements ServletContainerInitializer{
	

	
	@Override
	public void onStartup(Set<Class<?>> arg0, ServletContext servletContext) throws ServletException {
		// TODO Auto-generated method stub
		servletContext.addServlet("CustomServlet", new CustomServlet()).addMapping("/customServlet");
		servletContext.addFilter("CustonFilter", new CustonFilter()).addMappingForServletNames(EnumSet.of(DispatcherType.REQUEST), true, "CustomServlet");
		servletContext.addListener(new CustomListener());
	}

 第三种基于注释:

首先:在 SpringBootDemoFilerServletLisApplication 类中添加

技术分享图片

 

在listener
技术分享图片

在servlet

技术分享图片

在filter (  "/*" 代表过滤全局  )

技术分享图片

 



Springboot - -web应用开发-Servlets, Filters, listeners

标签:throws   gis   int   tps   return   private   注释   example   png   

原文地址:https://www.cnblogs.com/durenniu/p/9520910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!