2 回答

TA貢獻1744條經驗 獲得超4個贊
在doPost里面調用doGet而已,協(xié)議不同,但是實現(xiàn)邏輯相同,所以直接調用即可。
doGet方法提交表單的時候會在url后邊顯示提交的內容,所以不安全。而且doGet方法只能提交256個字符(1024字節(jié)),而doPost沒有限制,因為get方式數(shù)據(jù)的傳輸載體是URL(提交方式能form,也能任意的URL鏈接),而POST是HTTP頭鍵值對(只能以form方式提交)。
通常使用的都是doPost方法只要在servlet中讓這兩個方法互相調用就行了,例如在doGet方法中這樣寫:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
擴展資料:
doPost用于客戶端把數(shù)據(jù)傳送到服務器端,也會有副作用。但好處是可以隱藏傳送給服務器的任何數(shù)據(jù)。Post適合發(fā)送大量的數(shù)據(jù)。
例:
jsp頁代碼:
<form action="/doPostt_servlet" method="post">
………
<textarea cols="50" rows="10"></textarea>
………
</form>
servlet代碼:
public class doPostt_servlet extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse esponse) throws IOException,ServletException {
request.setCaracterEncoding(“gb2312”);//漢字轉碼
PrintWriter out = response.getWriter();
out.println("The Parameter are :"+request.getParameter("name2"));
}
}

TA貢獻1829條經驗 獲得超9個贊
因為前臺頁面請求的時候有兩種方式:
<form method="get">
</form>
這個提交到后臺請求的就是doget方法
<form method="post">
</form>
這個提交到后臺請求的就是dopost方法
兩個方法里面的內容是一樣的,之所以這樣調用,是避免代碼重復使用。
添加回答
舉報