前言
Servlet的继承关系
Servlet(父)->GenericServlet(子)->HttpServlet(孙)
通常开发B/S架构的应用只需要继承HttpServlet即可
创建一个自定义的Servlet
- 创建一个自定义的Servlet,继承HttpServlet方法
@WebServlet()
urlPatterns = "/"
:配置请求路径
doGet()
:处理Get请求
doPost()
:处理Post请求
1 2 3 4 5 6 7 8 9 10 11 12 13
| @WebServlet(urlPatterns = "/") public class MyServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } }
|
省略
- 通常情况下,Get请求和Post请求的处理方式相同,此时可以将Post请求通过doGet处理即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @WebServlet(urlPatterns = "/") public class MyServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); this.doGet(req, resp); } }
|
完成
参考文献
哔哩哔哩——黑马程序员