Link to home
Start Free TrialLog in
Avatar of rpong
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?
ASKER CERTIFIED SOLUTION
Avatar of radarsh
radarsh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rpong
rpong

ASKER

that seems way too easy. ok thanks for the help
Avatar of jessegivy
...got me again radarsh.. best to also check for null in case of first visit to page.

String name_of_your_input_field=request.getParameter("name_of_your_input_field")==null?"":request.getParameter(name_of_your_input_field");

...and if you've got multiselect boxes, you'll need to getParameterValues("name_of_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_input_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
Avatar of rpong

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
>>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=request.getParameter("name_of_your_input_field")==null?"":request.getParameter(name_of_your_input_field");

is exactly the same as this:

String name_of_your_input_field;
if(request.getParameter("name_of_your_input_field")==null)
{
   name_of_your_input_field="";
}
else
{
   name_of_your_input_field=request.getParameter("name_of_your_input_field")
}

...the first line with short if notation is just a shorthand which was developed with getting/setting parameters in mind.

Cheers,

JI