简体   繁体   中英

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

In my JSP I do the following :

<!-- 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>

And in my Servlet I grab the hidden input :

@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);
    }



}

And System.out.println produces : null at the Console

Why do I get a null of not the actual value is I pass ?

Regards

EDIT:

After changing to :

<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>

A null is still presented at the console .

What you are trying to do is to send a form to the server. But, in fact, you don't do that. You just issue a GET request (when the user clicks your link: <a href="employeeTransaction1">Press here to continue</a> )

If you want to send the form make sure you set the attributes of the form tag properly and add a submit button to the form :

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

Depending on your preferred way of sending the form, you can change the method="GET" paramater to method="POST" and make sure that in the servlet you handle the form in the doPost() method

Alternatively, if your purpose is not to send the from to the server but just to pass the value of the hidden input, you should add its value as a prameter encoded in the GET request. Something like:

  /employeeTransaction1?hdField=myValue

To achieve this, you need some client processing, ie when the user clicks the link, the hidden input should be added to the get and then the request should be issued.

Using an href tag does not submit your form, ie it does not pass the parameters defined in the form to the request. You should use input type="submit" or button tags instead. Also make sure the form action matches your @WebServlet definition.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM