亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

您好,請問一下在httpservlet中的dopost方法中為什么要包含doget方法?有什么作用?

您好,請問一下在httpservlet中的dopost方法中為什么要包含doget方法?有什么作用?

千萬里不及你 2021-10-12 22:11:16
package com.itheima.filter.app2;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ServletDemo2 extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String content = request.getParameter("content");response.getWriter().write(content);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);-------為什么doPost()方法中要包含 doGet(request, response)方法???}}
查看完整描述

2 回答

?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

在doPost里面調用doGet而已,協議不同,但是實現邏輯相同,所以直接調用即可。

doGet方法提交表單的時候會在url后邊顯示提交的內容,所以不安全。而且doGet方法只能提交256個字符(1024字節),而doPost沒有限制,因為get方式數據的傳輸載體是URL(提交方式能form,也能任意的URL鏈接),而POST是HTTP頭鍵值對(只能以form方式提交)。

通常使用的都是doPost方法只要在servlet中讓這兩個方法互相調用就行了,例如在doGet方法中這樣寫:

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request,response);

}

擴展資料:    

doPost用于客戶端把數據傳送到服務器端,也會有副作用。但好處是可以隱藏傳送給服務器的任何數據。Post適合發送大量的數據。

例:

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"));

  }

}


查看完整回答
反對 回復 2021-10-18
?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

因為前臺頁面請求的時候有兩種方式:
<form method="get">

</form>
這個提交到后臺請求的就是doget方法

<form method="post">

</form>
這個提交到后臺請求的就是dopost方法

兩個方法里面的內容是一樣的,之所以這樣調用,是避免代碼重復使用。



查看完整回答
反對 回復 2021-10-18
  • 2 回答
  • 0 關注
  • 388 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號