<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>webcompontent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>webcompontent</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.yl.webcompontent.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/** * 注册servlet */
@WebServlet(urlPatterns = "/hello")
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 {
System.out.println("web servlet...");
}
}
package com.yl.webcompontent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan({"com.yl.webcompontent.filter","com.yl.webcompontent.servlet","com.yl.webcompontent.listener"})
public class WebcompontentApplication {
public static void main(String[] args) {
SpringApplication.run(WebcompontentApplication.class, args);
}
}
package com.yl.webcompontent.listener;
import org.springframework.web.context.request.RequestContextListener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.annotation.WebListener;
/** * 自定义监听器 */
@WebListener
public class MyListener extends RequestContextListener {
@Override
public void requestInitialized(ServletRequestEvent requestEvent) {
System.out.println("listener init...");
}
@Override
public void requestDestroyed(ServletRequestEvent requestEvent) {
System.out.println("listener destroy...");
}
}
package com.yl.webcompontent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan({"com.yl.webcompontent.filter","com.yl.webcompontent.servlet","com.yl.webcompontent.listener"})
public class WebcompontentApplication {
public static void main(String[] args) {
SpringApplication.run(WebcompontentApplication.class, args);
}
}
package com.yl.webcompontent.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
/** * 自定义拦截器 */
@WebFilter(urlPatterns = "/*")
public class MyFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("filter init...");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("do my filter....");
chain.doFilter(request,response);
}
@Override
public void destroy() {
System.out.println("filter destroy...");
}
}
package com.yl.webcompontent;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan({"com.yl.webcompontent.filter","com.yl.webcompontent.servlet","com.yl.webcompontent.listener"})
public class WebcompontentApplication {
public static void main(String[] args) {
SpringApplication.run(WebcompontentApplication.class, args);
}
}
package com.yl.filter.filter;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.servlet.*;
import java.io.IOException;
@Component
@Order(99)
public class MyFilter03 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("do myFilter03...");
chain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
package com.yl.filter.filter;
import javax.servlet.*;
import java.io.IOException;
public class MyFilter05 implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
System.out.println("do myFilter05...");
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
package com.yl.filter.filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
@Configuration
public class FilterConfiguration {
@Bean
FilterRegistrationBean<MyFilter04> filter04FilterRegistrationBean() {
FilterRegistrationBean<MyFilter04> bean = new FilterRegistrationBean<>();
//设置优先级
bean.setOrder(90);
//指定过滤器
bean.setFilter(new MyFilter04());
//设置拦截路径
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
@Bean
FilterRegistrationBean<MyFilter05> filter05FilterRegistrationBean() {
FilterRegistrationBean<MyFilter05> bean = new FilterRegistrationBean<>();
//设置优先级
bean.setOrder(89);
//指定过滤器
bean.setFilter(new MyFilter05());
//设置拦截路径
bean.setUrlPatterns(Arrays.asList("/*"));
return bean;
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/weixin_41359273/article/details/120463907
内容来源于网络,如有侵权,请联系作者删除!