SpringBoot_10_Servlet

在spring boot中添加自己的Servlet有两种方法,代码注册Servlet和注解自动注册(Filter和Listener也是如此)

注解方式

在 SpringBootApplication 上使用@ServletComponentScan注解后,
Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns="/myServlet2/*", description="Servlet的说明")
public class CustomServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(">>>>>>>>>>doGet()<<<<<<<<<<<");
        doPost(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(">>>>>>>>>>doPost()<<<<<<<<<<<");
        resp.setContentType("text/html"); 
        PrintWriter out = resp.getWriter(); 
        out.println(""); 
        out.println(""); 
        out.println("Hello World"); 
        out.println(""); 
        out.println(""); 
        out.println("

这是:myServlet2

"); out.println(""); out.println(""); } }

代码方式

http://412887952-qq-com.iteye.com/blog/2292472

你可能感兴趣的:(SpringBoot_10_Servlet)