Hidden input in JSP produces null when passing it to the servlet
Solution 1
Using an href tag does not submit your form, i.e. 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>
Solution 2
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, i.e. when the user clicks the link, the hidden input should be added to the get and then the request should be issued.
Jack cole
Updated on June 04, 2022Comments
-
Jack cole almost 2 years
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 ConsoleWhy 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 .-
UVM almost 12 yearsare you submitting to the proper uri?
-
Razvan almost 12 yearscan you add the form opening tag ?
-
Jack cole almost 12 years@UVM: Yes, when the forward takes place , the result is correct , however the
hidden value
is null . beside that , everything else is okay . -
Jack cole almost 12 years@Razvan: I'm not using that actually . Is it really needed ?
-
-
Jack cole almost 12 years@Razvav: I still get a
null
when changing to this : ` <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> ` -
Jack cole almost 12 yearsEdited at the original post .
-
Jack cole almost 12 yearsI already have a
href
button , so why adding another one ? I've tested your suggestion ,but it still doesn't work - meaning I added theSubmit
but it keeps failing .And BTW - I don't need to send the form - just to send thehidden input
. -
Razvan almost 12 yearsof course there are a number of details which you probably miss when trying to send the form. If the hidden input value doesn't, change set your href to: employeeTransaction1?hdField=myValue. If it's not static you need to handle it in javascript to add it to the href