SpringBoot下注冊Servlet三大元件(Servlet,Filter,Listener)
阿新 • • 發佈:2018-12-21
由於SpringBoot是通過jar包的形式執行,其內部嵌入了Servlet容器,專案中不存在web.xml檔案,所以註冊三大元件就不能通過web.xml檔案註冊。
1.註冊servlet
建立servlet
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class MyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("自定義servlet"); } }
註冊servlet
import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.peng.demo.servlet.MyServlet; @Configuration public class MyServerConfig { //註冊servlet @Bean public ServletRegistrationBean<MyServlet> myServlet() { ServletRegistrationBean<MyServlet> servletRegistrationBean = new ServletRegistrationBean<MyServlet>(new MyServlet(),"/myServlet");//對映地址 return servletRegistrationBean; } }
2.註冊Filter
建立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 MyFilter implements Filter { //銷燬 @Override public void destroy() { } //過濾 @Override public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain chain) throws IOException, ServletException { System.out.println("filter執行啦!!!!!!!!!!"); chain.doFilter(arg0, arg1); } //初始化 @Override public void init(FilterConfig arg0) throws ServletException { } }
註冊Filter
//註冊Filter
@Bean
public FilterRegistrationBean myFilter(){
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new MyFilter());//指定攔截的filter
filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));//指定攔截路徑
return filterRegistrationBean;
}
3.註冊Listener
建立Listener
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("web應用關閉啦!!!!!!!!!!!!!");
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("web應用啟動啦!!!!!!!!!!!!!");
}
}
註冊Listener
//註冊listener
@Bean
public ServletListenerRegistrationBean myListener() {
ServletListenerRegistrationBean servletListenerRegistrationBean=new ServletListenerRegistrationBean(new MyListener());
return servletListenerRegistrationBean;
}
4.總結
SprongBoot註冊SpringMVC的DispatcherServlet就是通過這種方式註冊的
DispatcherServletRegistrationBean為ServletRegistrationBean的實現類,預設攔截地址通過this.serverProperties.getServlet().getPath()獲取,即攔截所有(包括靜態資源,不包括jsp)
所以可以通過server.path修改前端控制器預設攔截路徑。