我想了解在servlet中为什么要在同一个程序中同时使用doGet和doPost方法。这样做有什么用处?以下代码的含义是什么?为什么要从doPost调用doGet方法?我对这段代码一点也不清楚。
public class Info extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
doGet(request, response);
}
}
谢谢
doGet()
用于/users
返回一个列表,doPost()
同样是针对相同的 URL(servlet)创建新用户。当然,我可以将这两种方法委托到一个单独的方法中,然后使用HttpServletRequest.getMethod()
,但这只会使事情更加复杂。 - Tomasz NurkiewiczdoGet()
。如果使用 POST 提交,则会调用doPost()
。如果您只实现了doPost()
,但表单却使用 GET,则 Servlet 容器将抛出异常。 - Tomasz Nurkiewicz