簡體   English   中英

將JSP中的隱藏輸入傳遞給Servlet時,它會產生null

[英]Hidden input in JSP produces null when passing it to the servlet

在我的JSP中,我執行以下操作:

<!-- Bank manager's permissions -->

<!--more stuff goes here -->
<fieldset>
  <legend>To open a new account</legend> 
  <form action="blablabla">    
      <input type="hidden" name="hdField" value="myValue" />  // note I pass a "myValue" as string 
      <a href="employeeTransaction1">Press here to continue</a>  
  </form>
</fieldset>

在我的Servlet中,我獲取了隱藏的輸入:

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


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        String getHiddenValue=request.getParameter("hdField");
        System.out.println("Hidden field Value :"+getHiddenValue);
        // forwards to the page employeeOpenNewAccount.jsp
        request.getRequestDispatcher("/WEB-INF/results/employeeOpenNewAccount.jsp").forward(request, response);
    }



}

然后System.out.println在控制台上生成: null

為什么我得到的null值不是我通過的實際值?

問候

編輯:

更改為:

<fieldset>
  <legend>To open a new account</legend> 
  <form action="/employeeTransaction1" method="GET">
      <input type="hidden" name="hdField" value="myValue"/>
      <a href="employeeTransaction1">Press here to continue</a>  
  </form>
</fieldset>

控制台上仍然顯示null

您試圖做的是將表單發送到服務器。 但是,實際上,您不這樣做。 您只需發出GET請求(當用戶單擊您的鏈接時: <a href="employeeTransaction1">Press here to continue</a>

如果要發送表單,請確保您正確設置了表單標簽的屬性,並向表單添加了一個提交按鈕

 <form action="/employeeTransaction1" method="GET">
 ...
 <input type="submit" value="Submit" />
 ...
 </form>

根據您發送表單的首選方式,可以將method="GET"參數更改為method="POST" ,並確保在servlet中,您可以在doPost()方法中處理表單

另外,如果您的目的不是將發件人發送到服務器,而只是傳遞隱藏輸入的值,則應將其值添加為GET請求中編碼的參數。 就像是:

  /employeeTransaction1?hdField=myValue

為此,您需要進行一些客戶端處理,即,當用戶單擊鏈接時,應將隱藏的輸入添加到get中,然后發出請求。

使用href標簽不會提交您的表單,即,它不會將表單中定義的參數傳遞給請求。 您應該改用輸入type =“ submit”按鈕標簽。 還要確保表單操作與您的@WebServlet定義匹配。

<fieldset>
  <legend>To open a new account</legend> 
  <form action="/employeeTransaction1">    
      <input type="hidden" name="hdField" value="myValue" />  // note I pass a "myValue" as string 
      <input type="submit" value="Submit" />
  </form>
</fieldset>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM