04-SpringBoot之WEB(二)——整合Servlet、Filter、Listener
阿新 • • 發佈:2018-12-25
1. Servlet
1.1 自定義Servlet
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by HuangJun
* 9:42 2018/11/9
*/
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.setContentType("text/html;charset=utf-8");
resp.getWriter().write("This is MyServlet");
}
}
1.2 註冊Servlet
在上文建立的 WebConfig 類中新增如下程式碼:
@Bean
public ServletRegistrationBean servletRegistrationBean() {
return new ServletRegistrationBean(new MyServlet(),"/myServlet" );
}
1.3 執行結果
2. Filter
2.1 自定義過濾器
import javax.servlet.*;
import java.io.IOException;
/**
* Created by HuangJun
* 10:02 2018/11/9
*/
public class TimeFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("=======初始化過濾器=========");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
long start = System.currentTimeMillis();
filterChain.doFilter(request, response);
System.out.println("filter 耗時:" + (System.currentTimeMillis() - start));
}
@Override
public void destroy() {
System.out.println("=======銷燬過濾器=========");
}
}
2.2 註冊過濾器
在上文建立的 WebConfig 類中新增如下程式碼:
@Bean
public FilterRegistrationBean timeFilter() {
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
TimeFilter timeFilter = new TimeFilter();
registrationBean.setFilter(timeFilter);
List<String> urls = new ArrayList<>();
urls.add("/*");
registrationBean.setUrlPatterns(urls);
return registrationBean;
}
2.3 執行結果
3. Listener
3.1 自定義Listener
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* Created by HuangJun
* 10:28 2018/11/9
*/
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("監聽器初始化...");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
3.2 註冊Listener
在上文建立的 WebConfig 類中新增如下程式碼:
@Bean
public ServletListenerRegistrationBean<MyListener> servletListenerRegistrationBean() {
return new ServletListenerRegistrationBean<MyListener>(new MyListener());
}
3.3 執行結果
觀察控制檯列印輸出,結果如下圖
自定義 Servlet、Filter 和 Listener 的另一種配置方式,修改引導類,程式碼如下:
import com.springboot.web.filter.TimeFilter;
import com.springboot.web.listener.MyListener;
import com.springboot.web.servlet.MyServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import javax.servlet.DispatcherType;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.util.EnumSet;
@SpringBootApplication
public class WebApplication implements ServletContextInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// 配置 Servlet
servletContext.addServlet("servletTest",new MyServlet())
.addMapping("/myServlet");
// 配置過濾器
servletContext.addFilter("timeFilter",new TimeFilter())
.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*");
// 配置監聽器
servletContext.addListener(new MyListener());
}
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}