springboot 之自定义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;

/**
 * 测试URL
 * http://localhost:8080/test/v1/testServlet
 */
@WebServlet(name = "myServlet", urlPatterns = "/test/v1/testServlet")
public class MyServlet  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().print("hello servlet");
        resp.getWriter().flush();
        resp.getWriter().close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

 

参考这篇文章:

https://blog.csdn.net/qq_43352519/article/details/100605150

 

你可能感兴趣的:(springboot,自定义servlet)