javax.servlet.http.HttpServlet类是比Simple Servlet示例中显示的GenericServlet稍高级的基类。
HttpServlet类读取HTTP请求,并确定请求是否为HTTP GET,POST,PUT,DELETE,HEAD等,并调用一个相应的方法。
为了响应例如 仅HTTP GET请求,您将扩展HttpServlet类,并仅覆盖doGet()方法。 比如:
public class SimpleHttpServlet extends HttpServlet {
protected void doGet( HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("<html><body>GET response</body></html>");
}
}
复制代码HttpServlet类也可以覆盖每个HTTP方法(GET,POST等)的方法。所有方法如下:
- doGet()
- doPost()
- doHead()
- doPut()
- doDelete()
- doOptions()
- doTrace()
大多数情况下,我们只想响应HTTP GET或POST请求,因此只需重写这两种方法。并且二者可以相互调用:
public class SimpleHttpServlet extends HttpServlet {
protected void doGet( HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost( HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.getWriter().write("GET/POST response");
}
}
复制代码我们尽可能使用HttpServlet而不是GenericServlet。 HttpServlet比GenericServlet更容易使用,并且有更多的便捷方法。
本文介绍了javax.servlet.http.HttpServlet类的使用方法,包括如何通过扩展HttpServlet来响应HTTP GET和POST请求,以及如何实现doGet()和doPost()方法。此外,还讨论了如何让doGet()方法调用doPost()方法。

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



