springboot开发之配置Servlet三大组件(Servlet、Filter、Listener)
1、首先我们自己定义三大组件:MyServlet.java、MyFilter.java、MyListener.java
MyServlet.java
package com.gong.springbootcurd.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class MyServlet extends HttpServlet { //处理get请求 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req,resp); } //处理post请求 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("hello world"); } }
MyFilter.java
package com.gong.springbootcurd.filter; import javax.servlet.*; import java.io.IOException; public class MyFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("myfilter process"); filterChain.doFilter(servletRequest,servletResponse);; } @Override public void destroy() { } }
MyListener.java
package com.gong.springbootcurd.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("contextInitialized..web应用启动了"); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("contextDestroyed..web应用关闭了"); } }
(2)我们自定义注册三大组件的文件:MyServletConfig.java
package com.gong.springbootcurd.config; import com.gong.springbootcurd.filter.MyFilter; import com.gong.springbootcurd.listener.MyListener; import com.gong.springbootcurd.servlet.MyServlet; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletListenerRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.Filter; import javax.servlet.Servlet; import javax.servlet.ServletContextListener; import java.util.Arrays; @Configuration public class MyServletConfig { //注册三大组件 @Bean public ServletRegistrationBean myServlet(){ ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>( new MyServlet(),"/myServlet" ); registrationBean.setLoadOnStartup(1); return registrationBean; } @Bean public FilterRegistrationBean myFilter(){ FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); registrationBean.setFilter(new MyFilter()); registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet")); return registrationBean; } @Bean public ServletListenerRegistrationBean myListener(){ ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<MyListener>(new MyListener()); return registrationBean; } }
我们启动服务器,并输入localhost:8080/curd/myServlet,可以看到:
MyServlet起作用了。
在控制台:
MyListener起作用了,MyFilter也起作用
点击紫色框中按钮关闭服务:
MyListener其作用了。