rpong
asked on
Form parameter passing to a JSP online tutorials.
I was asked to take this form populated with values pulled from a DB. In a nutshell, the user makes modifications to the form and clicks SUBMIT. The parameters should be passed to a JSP using the POST method where we are using HQL (Hibernate SQL extension) to update the database.
The problem I have is that I'm not understanding how to retrieve a passed value from the form to a JSP. What I do know is that the form attributes are passed and are identified by the name, ex: <INPUT type="text" id="myInput">. What I don't understand is how to retrieve that value if my form looks like the following:
<FORM action="/test.jsp" method="post">
<INPUT type="text" id="myInput">
</form>
What's the code look like in the test.jsp in order to retrieve the value in myInput?
The problem I have is that I'm not understanding how to retrieve a passed value from the form to a JSP. What I do know is that the form attributes are passed and are identified by the name, ex: <INPUT type="text" id="myInput">. What I don't understand is how to retrieve that value if my form looks like the following:
<FORM action="/test.jsp" method="post">
<INPUT type="text" id="myInput">
</form>
What's the code look like in the test.jsp in order to retrieve the value in myInput?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
...got me again radarsh.. best to also check for null in case of first visit to page.
String name_of_your_input_field=r equest.get Parameter( "name_of_y our_input_ field")==n ull?"":req uest.getPa rameter(na me_of_your _input_fie ld");
...and if you've got multiselect boxes, you'll need to getParameterValues("name_o f_select") ;
...just look in API for request interface. Also to display the value after the page has been posted you'll need to add:
<INPUT type="text" id="myInput" value="<%=name_of_your_inp ut_field%> ">
...obviously "name_of_your_input_field" is supposed to be the actual value of the name attribute for the element in question, should be the same as id
String name_of_your_input_field=r
...and if you've got multiselect boxes, you'll need to getParameterValues("name_o
...just look in API for request interface. Also to display the value after the page has been posted you'll need to add:
<INPUT type="text" id="myInput" value="<%=name_of_your_inp
...obviously "name_of_your_input_field"
ASKER
<%
request.getParameter("test ") ;
%>
didn't work
but <%= request.getParameter("test ") %> did.
note removing the ";"
https://www.experts-exchange.com/questions/21740032/EASY-POINTS-I-know-you're-not-working-JSP-parameter-passing-using-Post-form.html
request.getParameter("test
%>
didn't work
but <%= request.getParameter("test
note removing the ";"
https://www.experts-exchange.com/questions/21740032/EASY-POINTS-I-know-you're-not-working-JSP-parameter-passing-using-Post-form.html
>>but <%= request.getParameter("test ") %> did.
note removing the ";"
these are two different code examples. The method you're using is intended for outputting plaint text into the html file. The one radarsh was using is meant to include processing to be done on the server side, can include database queries, check if the form as been submitted, set variables which can then be used to set value attributes for your elements, loops conditions, etc.
It's important for you to realize that unless you ALWAYS pass "test" as a parameter to this page it will be null the first time you visit the page. I know you're just foolin around, but it's something to keep in mind. In case you're not familiar with the short if statement I used in the first post I made I thought you might wanna know that this:
String name_of_your_input_field=r equest.get Parameter( "name_of_y our_input_ field")==n ull?"":req uest.getPa rameter(na me_of_your _input_fie ld");
is exactly the same as this:
String name_of_your_input_field;
if(request.getParameter("n ame_of_you r_input_fi eld")==nul l)
{
name_of_your_input_field=" ";
}
else
{
name_of_your_input_field=r equest.get Parameter( "name_of_y our_input_ field")
}
...the first line with short if notation is just a shorthand which was developed with getting/setting parameters in mind.
Cheers,
JI
note removing the ";"
these are two different code examples. The method you're using is intended for outputting plaint text into the html file. The one radarsh was using is meant to include processing to be done on the server side, can include database queries, check if the form as been submitted, set variables which can then be used to set value attributes for your elements, loops conditions, etc.
It's important for you to realize that unless you ALWAYS pass "test" as a parameter to this page it will be null the first time you visit the page. I know you're just foolin around, but it's something to keep in mind. In case you're not familiar with the short if statement I used in the first post I made I thought you might wanna know that this:
String name_of_your_input_field=r
is exactly the same as this:
String name_of_your_input_field;
if(request.getParameter("n
{
name_of_your_input_field="
}
else
{
name_of_your_input_field=r
}
...the first line with short if notation is just a shorthand which was developed with getting/setting parameters in mind.
Cheers,
JI
ASKER