Form.action傳值問題,form.action傳值

來源:互聯網
上載者:User

Form.action傳值問題,form.action傳值

通過瀏覽器地址欄輸入url並通過?傳遞參數請求資源時,?後面的參數叫做 "查詢字串",會觸發後台Servlet的doGet(),因為通過瀏覽器地址欄直接存取的方式是GET方式。


下面順便瞭解一下form的method屬性對傳參的影響。

原始表單:
<form id="myForm">
<label>使用者名稱:</label>
<input name="username" type="text">
<input type="submit" value="提交">
</form>


第一種情況:
上面表單的action屬性不寫時,直接點擊“提交”按鈕,會觸發表單的提交事件,此時,通過Firefox瀏覽器的控制台可以發現:
訊息頭中有如下關鍵資訊:(1)請求地址後面通過'?'傳遞參數(2)請求方式為GET

 




參數資訊: 查詢字串

 




指定form的method屬性時:
<form id="myForm" method="post">
<label>使用者名稱:</label>
<input name="username" type="text">
<input type="submit" value="提交">
</form>

第二種情況:
訊息頭中有如下關鍵資訊:(1)請求地址後面沒有傳遞任何參數(2)請求方式為POST

 



參數資訊: 表單資料

 




上面兩種方式的相應都是重新整理後顯示當前頁面,因為form沒有指定action屬性,預設提交的地址就是當前頁面。

現在通過指定action屬性訪問Servlet來進一步瞭解method屬性。

TestServlet.java核心源碼:

@WebServlet("/servlet/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print("doGet()");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print("doPost()");
}

}


第三種情況:method="get" action="/test/servlet/TestServlet"

<form id="myForm" method="get" action="/test/servlet/TestServlet">
<label>使用者名稱:</label>
<input name="username" type="text">
<input type="submit" value="提交">
</form>

上面的訊息頭,參數和第一種情況是一樣的,不過後台響應了一個doGet().

 





第四種情況: method="post" action="/test/servlet/TestServlet"

<form id="myForm" method="post" action="/test/servlet/TestServlet">
<label>使用者名稱:</label>
<input name="username" type="text">
<input type="submit" value="提交">
</form>

上面的訊息頭,參數和第一種情況是一樣的,不過後台響應了一個doPost().

 





TestServlet.java 添加擷取參數代碼:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request.getParameter("type");
response.getWriter().print("doGet() get type="+param);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String param = request.getParameter("type");
response.getWriter().print("doPost() get type="+param);
}


第五種情況,method="get" 在action後面傳參action = "/servlet/TestServlet?type=01",同時在後台Servlet中擷取參數

此時瀏覽器地址欄:http://localhost:8080/test/servlet/TestServlet?username=

 



可以發現,背景doGet()是擷取不到參數type的.
同時發現,type參數是沒有加入到“查詢字串”的

 






第六種情況,method="post" 在action後面傳參action = "/servlet/TestServlet?type=01",同時在後台Servlet中擷取參數

 




可以發現,背景doPost()是可以擷取參數type的。

同時,參數一欄和以前有些不同:既有“查詢字串”又有“表單資料”

 





所以上面給我們一種啟示,就是通過?在form的action後面傳遞參數時,我們需要手動指定form的method="post",不然是擷取不到?後面的參數的;
思維誤區:之前我們在瀏覽器地址欄通過url傳遞參數,也是通過?來實現的,它最終也是調用doGet()方式,所以我們就認為在的表單(預設method="get")中action後面可以通過?來傳參。


最終原因:(個人總結)get方式提交表單資料時,會重組url,它只會將form的表單資料群組裝成“查詢字串”,提交到form的action中指定的url,所以原來通過?方式傳遞的參數是不會提交的,因為url重組了就丟失了。

但是post方式提交表單時,它會將表單資料和?後面的參數,分開儲存,一起提交給form中action指定的url。

(參考資源)HTML Form: why action can't have get value in it?
http://stackoverflow.com/questions/3548795/html-form-why-action-cant-have-get-value-in-it

 




 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.