request.getParameter()取值為空白的問題

來源:互聯網
上載者:User

本文主要解決form表單屬性enctype值為multipart/form-data時request.getParameter()取值為空白的問題

剛修改一個靜態頁面,在Servlet裡面用request.getParameter()方法擷取頁面參數時無論如何都只是獲得null值。可是,建立一個簡單的帶form的jsp頁面測試卻可以正常獲得頁面參數。我是鬱悶啊!向人求救,被告知是沒有添加method="post"的原因。可這解決方案卻並不靈驗!於是又逐行刪除原代碼來尋找問題根源,最後我發現form標記跟測試中的不太一樣,如下:

Code:
  1. <form id="form" action="login" method="post" enctype="multipart/form-data">  

而測試程式中的form標記中卻沒有enctype標記。於是這行代碼引起我的重視,遂寫了下面的測試:

Code:
  1. <form id="form1" action="login" method="post" enctype="multipart/form-data">  
  2.     post**multipart/form-data<br/>  
  3.     name:<input type="text" name="name"/><br/>  
  4.     <input type="submit" value="Submit"/>  
  5. </form>  
  6. <form id="form2" action="login" method="get" enctype="multipart/form-data">  
  7.     get***multipart/form-data<br/>  
  8.     name:<input type="text" name="name"/><br/>  
  9.     <input type="submit" value="Submit"/>  
  10. </form>  
  11. <form id="form3" action="login" method="post">  
  12.     post<br/>  
  13.     name:<input type="text" name="name"/><br/>  
  14.     <input type="submit" value="Submit"/>  
  15. </form>  
  16. <form id="form4" action="login" method="get">  
  17.     get<br/>  
  18.     name:<input type="text" name="name"/><br/>  
  19.     <input type="submit" value="Submit"/>  
  20. </form>  

分別提交四個表單,結果是form1表單提交是擷取的值為null,而form2,form3,form4表單提交後擷取的值正常。看來還是應該多注意這個enctype屬性啊!

在w3school網站上又找到如下資訊:

 

定義和用法

enctype 屬性規定在發送到伺服器之前應該如何對錶單資料進行編碼。

預設地,表單資料會編碼為 "application/x-www-form-urlencoded"。就是說,在發送到伺服器之前,所有字元都會進行編碼(空格轉換為 "+" 加號,特殊符號轉換為 ASCII HEX 值)。

 

屬性值
描述
application/x-www-form-urlencoded 在發送前編碼所有字元(預設)
multipart/form-data

不對字元編碼。

在使用包含檔案上傳控制項的表單時,必須使用該值。

text/plain 空格轉換為 "+" 加號,但不對特殊字元編碼。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.