文章目录
一、使用组件注册方式整合Servlet
1.创建自定义的Servlet类
创建一个继承HttpServet的类MyServlet
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
* 使用组件注册方式整合Servlet
* */
@Component //使用次注解将MyServlet类作为组件注入Spring容器
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello MyServlet"); //向页面输出
}
}
2.创建Servlet组件配置类
此配置类用来对Servlet相关组件进行注册
import com.itheima.servletComponent.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.conte