代码配置:
package org.example.webFilter.config; import org.example.webFilter.filter.FirstFilter; import org.example.webFilter.listener.FirstListener; import org.example.webFilter.servlet.FirstServlet; 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.ServletContextListener; import java.util.ArrayList; import java.util.EventListener; import java.util.List; @Configuration public class WebConfig { /** * servletRegistrationBean:(使用代码注册Servlet(不需要@ServletComponentScan注解)). */ @Bean public ServletRegistrationBean getFirstServlet() { ServletRegistrationBean registrationBean = new ServletRegistrationBean(); registrationBean.setServlet(new FirstServlet()); List<String> urlMappings = new ArrayList<String>(); // 访问,可以添加多个 urlMappings.add("/first"); registrationBean.setUrlMappings(urlMappings); // 设置加载顺序 registrationBean.setLoadOnStartup(1); return registrationBean; } /** * 注册拦截器 */ @Bean public FilterRegistrationBean getFirstFilter() { FirstFilter demoFilter = new FirstFilter(); FilterRegistrationBean registrationBean=new FilterRegistrationBean(); registrationBean.setFilter(demoFilter); List<String> urlPatterns=new ArrayList<String>(); //拦截路径,可以添加多个 urlPatterns.add("/*"); registrationBean.setUrlPatterns(urlPatterns); //设置注册顺序 registrationBean.setOrder(1); return registrationBean; } /** * 注册监听器 */ @Bean public ServletListenerRegistrationBean<ServletContextListener> getFirstListener() { ServletListenerRegistrationBean<ServletContextListener> registrationBean = new ServletListenerRegistrationBean<>(); registrationBean.setListener(new FirstListener()); // 设置注册顺序 registrationBean.setOrder(1); return registrationBean; } }
代码里面设置:
package org.example.webFilter.servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class FirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("FirstServlet正在执行doGet"); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("FirstServlet正在执行doPost"); //获取响应输出流PrinterWriter对象 PrintWriter out = resp.getWriter(); //使用输出流对象向客户端发送信息 out.println("I'm a FirstServlet");//I'm a FirstServlet 会在客户端显示 } @Override public void init(ServletConfig config) throws ServletException { System.out.println("FirstServlet正在执行init"); super.init(config); } }
注解设置:
package org.example.webFilter.servlet; import javax.servlet.ServletConfig; 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; import java.io.PrintWriter; @WebServlet(urlPatterns = "/second") public class SecondServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("SecondServlet正在执行doGet"); doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("SecondServlet正在执行doPost"); //获取响应输出流PrinterWriter对象 PrintWriter out = resp.getWriter(); //使用输出流对象向客户端发送信息 out.println("I'm a SecondServlet");//I'm a SecondServlet 会在客户端显示 } @Override public void init(ServletConfig config) throws ServletException { System.out.println("SecondServlet正在执行init"); super.init(config); } }
package org.example.webFilter.filter; import javax.servlet.*; import java.io.IOException; public class FirstFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("FirstFilter正在执行doFilter"); chain.doFilter(request, response);//放行 } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("FirstFilter正在执行init"); } @Override public void destroy() { System.out.println("FirstFilter正在执行destroy"); } }
package org.example.webFilter.filter; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; @WebFilter(filterName = "secondFilter", urlPatterns = "/*") public class SecondFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { System.out.println("SecondFilter正在执行doFilter"); chain.doFilter(request, response);//放行 } @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("SecondFilter正在执行init"); } @Override public void destroy() { System.out.println("SecondFilter正在执行destroy"); } }
package org.example.webFilter.servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class FirstServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("FirstServlet正在执行doGet"); doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("FirstServlet正在执行doPost"); //获取响应输出流PrinterWriter对象 PrintWriter out = resp.getWriter(); //使用输出流对象向客户端发送信息 out.println("I'm a FirstServlet");//I'm a FirstServlet 会在客户端显示 } @Override public void init(ServletConfig config) throws ServletException { System.out.println("FirstServlet正在执行init"); super.init(config); } }
package org.example.webFilter.servlet; import javax.servlet.ServletConfig; 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; import java.io.PrintWriter; @WebServlet(urlPatterns = "/second") public class SecondServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("SecondServlet正在执行doGet"); doPost(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("SecondServlet正在执行doPost"); //获取响应输出流PrinterWriter对象 PrintWriter out = resp.getWriter(); //使用输出流对象向客户端发送信息 out.println("I'm a SecondServlet");//I'm a SecondServlet 会在客户端显示 } @Override public void init(ServletConfig config) throws ServletException { System.out.println("SecondServlet正在执行init"); super.init(config); } }
package org.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan("org.example.webFilter") public class HelloWorldApplication { public static void main(String[] args) { SpringApplication.run(HelloWorldApplication.class, args); } }
package org.example.webFilter.listener; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class FirstListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("FirstListener监听器正在初始化"); System.out.println("servlet container:"+sce.getServletContext().getServerInfo()); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("FirstListener监听器正在 销毁"); } }
package org.example.webFilter.listener; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; @WebListener public class SecondListener implements ServletContextListener { @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("SecondListener监听器正在初始化"); System.out.println("servlet container:"+sce.getServletContext().getServerInfo()); } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("SecondListener监听器正在 销毁"); } }
标签:华章,配置,public,Listener,println,import,servlet,javax,out From: https://www.cnblogs.com/xiaobaibailongma/p/17065071.html