整合Servlet方式一(通过注解扫描完成Servlet组件的注册)
1.创建Serverlet
2.修改启动类,添加 @ServletComponentScan
添加web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.1.RELEASE</version>
</dependency>
1.创建Serverlet
@WebServlet(name="FirstServlet", urlPatterns = "/first")
public class FirstServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response){
System.out.println("First Servlet...");
}
}
2.修改启动类,添加 @ServletComponentScan
@SpringBootApplication
@ServletComponentScan//在SpringBoot启动时会扫描@WebServlet,对该类进行实例化
public class SpringbootwebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootwebApplication.class, args);
}
}
通过:localhost:8080/first 访问
整合Servlet方式二(通过方法完成Servlet组件的注册)
1.创建Serverlet继承HttpServlet
2.创建Servlet配置类
1.创建Serverlet
public class SecondServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response){
System.out.println("Second Servlet...");
}
}
2.创建Servlet配置类
@Configuration
public class ServletConfig {
// 完成Servlet组件的注册
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
//添加该Servlet的访问路径
bean.addUrlMappings("/second");
return bean;
}
}
启动类
@SpringBootApplication
public class SpringbootwebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootwebApplication.class, args);
}
}
添加@Configuration相当于这个类是一个spring配置文件上下文,添加@Bean的这个方法会在spring容器启动的时候进行实例化
通过url:localhost:8080/second 访问
整合Filter方式一(通过注解扫描完成Filter组件注册)
1.创建Filter
2.修改启动类,添加@ServletComponentScan注解
1.创建Filter
//@WebFilter(filterName = "FirstFilter",urlPatterns = {"*.do","*.jsp"})
@WebFilter(filterName = "FirstFilter", urlPatterns = "/first")
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入First Filter");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("离开First Filter");
}
}
2.修改启动类,添加@ServletComponentScan注解
@SpringBootApplication
@ServletComponentScan
public class SpringbootwebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootwebApplication.class, args);
}
}
因为Filter本身属于Servlet的一个子技术,所以通过@ServletComponentScan也可以扫描到@WebFilter的类
通过url:localhost:8080/first 访问
整合Filter方式二(通过方法完成Filter组件注册)
1.创建Filter实现Filter
2.创建Filter配置类
1.创建Filter
public class SecondFilter implements Filter{
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入Second Filter");
filterChain.doFilter(servletRequest, servletResponse);
System.out.println("离开Second Filter");
}
}
创建Filter配置类
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean getFilterRegistrationBean(){
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
// bean.addUrlPatterns(new String[]{"*.do","*.jsp"});
bean.addUrlPatterns("/second");
return bean;
}
}
通过 url:localhost:8080/second
整合Listener方式一(通过注解扫描完成Listener组件注册)
1.编写Listener实现ServletContextListener
2.修改启动类添加@ServletComponentScan
1.编写Listener
@WebListener
public class FirstListener implements ServletContextListener{
//监听销毁的方法
public void contextDestroyed(ServletContextEvent event){
}
// 监听初始化的方法
public void contextInitialized(ServletContextEvent event){
System.out.println("Listener....Init.....");
}
}
2.修改启动类添加@ServletComponentScan
@SpringBootApplication
@ServletComponentScan
public class SpringbootwebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootwebApplication.class, args);
}
}
整合Listener方式二(通过方法完成Listener组件注册)
1.编写Listener
2.创建Listenter启动类
1.编写Listener
public class SecondListener implements ServletContextListener {
//监听销毁的方法
public void contextDestroyed(ServletContextEvent event){
}
// 监听初始化的方法
public void contextInitialized(ServletContextEvent event){
System.out.println("SecondListener....Init.....");
}
}
2.创建Listenter启动类
@Configuration
public class ListenerConfig {
@Bean
public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener());
return bean;
}
}