我想知道在servlet中为什么我们在同一个程序中一起使用doGet和doPost方法。它有什么用处?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);
}
}
感谢
什么时候你真的想区分servlet中的get和post?这将是什么样的用例(或需求)? – 2012-02-05 10:19:54
GET应该获取资源。它必须是幂等的,不应该修改服务器中的任何东西。 POST不是幂等的,用于创建,更新或删除服务器上的某些内容。当必须使用GET时,没有理由使用POST,并且在使用POST时使用GET是不好的做法,如果用户刷新页面或浏览历史记录,可能会导致各种问题。通常使用GET来显示表单,并通过POST提交。 – 2012-02-05 10:23:13
'doGet()'为'/ users'返回一个列表,'doPost()'为同一个URL(servlet)创建新的用户。当然,我可以将这两种方法委托为一个,然后使用['HttpServletRequest.getMethod()'](http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html# getMethod())但它只会使问题复杂化。 – 2012-02-05 10:25:06