目录
1.1??使用SpringBoot注解加继承HttpServet类注册
2.1??使用SpringBoot注解和实现ServletContextListener接口注册
2.2 ServletContextListener接口和配置类来进行注册
3.1??使用SpringBoot注解加实现Filter接口注册
Servlet是Java Web应用程序的基础,它提供了处理客户端请求的机制。Servlet三大组件是指Servlet、Filter和Listener,它们是Java Web应用程序的核心组件。
总之,Servlet三大组件是Java Web应用程序的核心组件,它们分别用于处理请求、拦截请求和监听事件,从而实现了一个完整的Java Web应用程序。
由于SpringBoot项目没有web.xml文件,所以无法在web.xml中注册web组件,SpringBoot有自己的方式注册web组件。
编写servlet,首先是要添加@WebServlet注解;代码如下:
package com.example.demo.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;
@WebServlet("/first")
public class FirstServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("First Servlet......");
super.doGet(req, resp);
}
}
启动类添加扫描Web组件用到的注解@ServletComponentScan
OK,直接运行看效果:访问:http://localhost:8080/first
是可以直接打印出来的。
ok,接下来我们讲解第二种注册方式,通过继承HttpServet类来进行注册,代码如下:
package com.example.demo.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class SecondServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("Second Servlet........");
super.doGet(req, resp);
}
}
这里我们需要新建一个配置类,将该Servlet加载到Spring容器中,配置类代码如下
package com.example.demo.config;
import com.example.demo.servlet.SecondServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ServletConfig {
// ServletRegistrationBean可以注册Servlet组件,将其放入Spring容器中即可注册Servlet
@Bean
public ServletRegistrationBean getServletRegistrationBean(){
// 注册Servlet组件
ServletRegistrationBean bean = new ServletRegistrationBean(new SecondServlet());
// 将Servlet组件添加访问路径
bean.addUrlMappings("/second");
return bean;
}
}
OK,然后我们点击运行,直接访问http://localhost:8080/second?
因此说明了两种注册方式都是可用的。?
原理和上面一样只不过是直接注解,无需配置类,代码如下
package com.example.demo.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener()
public class FirstListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("FirsListener.........");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContextListener.super.contextDestroyed(sce);
}
}
此时如果我们直接运行项目就会打印上面那句话,接下来我们直接运行项目,看看是否打印,
说明该方法是可行的,
同样我们去掉注解使用配置类注册 ,代码如下:
package com.example.demo.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class SecondListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("Second Listener...........");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
ServletContextListener.super.contextDestroyed(sce);
}
}
配置类代码入下:
package com.example.demo.config;
import com.example.demo.listener.SecondListener;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ListenerConfig {
// ServletRegistrationBean可以注册Servlet组件,将其放入Spring容器中即可注册Servlet
@Bean
public ServletListenerRegistrationBean getServletListenerRegistrationBean(){
// 注册Listener组件
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new SecondListener());
return bean;
}
}
?然后直接运行看看结果,
OK,同样也是没有问题的。?
和上面一样,代码如下:
package com.example.demo.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.Scanner;
//@WebFilter("/first")
public class FirstServletFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入First Filter");
Scanner scanner = new Scanner(System.in);
boolean flag = scanner.nextBoolean();
while(!flag){
System.out.println("要输入true才放行!!!");
flag = scanner.nextBoolean();
}
filterChain.doFilter(servletRequest,servletResponse);
scanner.close();
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
这里访问/frist的话/我们要输入true才进行放行,看看是否成功过滤,?
OK,这里是成功了的,接下来也是一样的操作?
代码如下:
package com.example.demo.filter;
import javax.servlet.*;
import java.io.IOException;
import java.util.Scanner;
public class SecondFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
Filter.super.init(filterConfig);
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("进入Second Filter");
Scanner scanner = new Scanner(System.in);
boolean flag = scanner.nextBoolean();
while(!flag){
System.out.println("要输入true才可以放行!!!");
flag = scanner.nextBoolean();
}
filterChain.doFilter(servletRequest,servletResponse);
scanner.close();
}
@Override
public void destroy() {
Filter.super.destroy();
}
}
配置类代码如下:
package com.example.demo.config;
import com.example.demo.filter.SecondFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig {
// ServletRegistrationBean可以注册Servlet组件,将其放入Spring容器中即可注册Servlet
@Bean
public FilterRegistrationBean getFilterRegistrationBean (){
// 注册filter组件
FilterRegistrationBean bean = new FilterRegistrationBean(new SecondFilter());
// 添加过滤路径
bean.addUrlPatterns("/second");
return bean;
}
}
同样这里访问/second的话/我们要输入true才进行放行,看看是否成功过滤,
OK,这里已经成功实现了!!!?