Springboot - -web应用开发-Servlets, Filters, listeners
2021-07-19 21:09
标签:throws gis int tps return private 注释 example png 一.Web开发使用 Controller 基本上可以完成大部分需求,但是我们还可能会用到 Servlet、 Filter、 Listener等等 二.在spring boot中的三种实现方式 方法一:通过注册ServletRegistrationBean、 FilterRegistrationBean 和 ServletListenerRegistrationBean 获得控制 servlet类: filter类: Listener类: 注册bean(第一种方式) 代码注册:(在SpringBootDemoFilerServletLisApplication类中) 第二种代码注册:(类似一) 第三种基于注释: 首先:在 SpringBootDemoFilerServletLisApplication 类中添加 在servlet 在filter ( "/*" 代表过滤全局 ) Springboot - -web应用开发-Servlets, Filters, listeners 标签:throws gis int tps return private 注释 example png 原文地址:https://www.cnblogs.com/durenniu/p/9520910.htmlpackage com.demo.example.util.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
public class CustomServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("servlet get method");
doPost(request , response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("servlet get method");
response.getWriter().write("Hello world");
}
}
package com.demo.example.util.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 CustonFilter implements Filter {
@Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("do destroy");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
System.out.println("do filter");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("do init");
}
}
package com.demo.example.util.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class CustomListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
// TODO Auto-generated method stub
System.out.println("servletContextEvent");
}
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
// TODO Auto-generated method stub
System.out.println("servletContextEvent");
}
}
@Bean
public ServletRegistrationBean
@SpringBootApplication
public class SpringBootDemoFilerServletLisApplication implements ServletContainerInitializer{
@Override
public void onStartup(Set
在listener
下一篇:python 初始函数
文章标题:Springboot - -web应用开发-Servlets, Filters, listeners
文章链接:http://soscw.com/index.php/essay/106385.html