第六章 SpringBoot拦截器实战和Servlet3.0自定义Filter和Listener
06-2 Servlet3.0的注解自定义原生Servlet实战
1.SpringBoot基础项目




2.原生Servlet实战
package com.lcz.spring_demo11.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;
/**
* @author : codingchao
* @date : 2021-11-23 13:08
* @Description:
**/
@WebServlet(urlPatterns = "/testServlet",name = "userServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("Test Servlet");
resp.getWriter().flush();
resp.getWriter().close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
}
启动类
package com.lcz.spring_demo11;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
@ServletComponentScan
@SpringBootApplication
public class SpringDemo11Application {
public static void main(String[] args) {
SpringApplication.run(SpringDemo11Application.class, args);
}
}
添加注解@ServletComponentScan
效果图

本文介绍了如何在SpringBoot中使用拦截器,并对比了Servlet3.0中自定义Filter和Listener的实战应用,通过实例展示如何实现简单的HTTP请求处理。
845

被折叠的 条评论
为什么被折叠?



