I am working on a web applet. I am using JDBC to connect to a psql database, in which I am running a query to return. Right now I have a drop down list that will help determine how the query will be structured. My drop down list options are set up like this:
<option value="Location A">Location A</option>
Is there anyway that I can pass this value into my java function which header looks like:
<%
out.print("HOW DO I PRINT THE ABOVE VARIABLE);
%>
Regards,
Sudo!!
if your html is like
<select name='foobar'>
<option value='0'>0</option>
<option value='0'>1</option>
</select>
When submitted to your JSP, normally you can do
request.getParameter("foobar");
Related
I have defined a select(drop down) in my jsp page. When user will select one of these options i want to display some text boxes. I will use jsp tags to display those textboxes. But I don't know how can i get value of select tag in jsp on the same page. Here is my code:
<div class="col2" >
<select class="textbox" id="noo" name="noo" style="margin-top:10px;" required>
<option value="">Select</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
</div>
<%
int noo=
%>
What should i write after = to get the selected value in variable?
This is possible if you use JSTL. You would need to add the jstl.jar file to your lib folder but then the selected value in the variable would be obtained using
${param.name}
JSP pages compile at server side not client side. So,its not possible that you select an option value and take action on it.
Yes, this similar thing you can do by JQuery or JavaScript.
If you really want to take support of server side code than ajax can help you.
Send selected value to servlet by ajax set response in session or request and iterate session or request in jsp .
JSP is a server side language. Whole html code will be generated before it sends a response to web browser.
So If you want to change something with respect to the value selected by user,
you must be using a client side scripting language like javascript.
Or submit value to server and generate a new page with respect to based on value submiteted.
There are two options:
To submit the form and get the attributes in another jsp or servlets and then return the value using the request.setAttribute(). After which you can get the attributes in the jsp part to display
You can use jquery that is get the value of the select tag after an event occurs e.g after selection e.g
var data = $('#noo').find(":selected").text();
Then display the data using a class or id selector.e.g
$('p').val(data)
Say I have
<select id="year" name="year">
<option value=""></option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
</select>
In a form that I pass to my servlet. How can I get the selected INDEX of the list? I have tried looking online but all I can see are answers on how to get the VALUE.
I want the index, because I want to be able to save and load the parameters of a form, but I have dynamically created select lists with values that change when other values in the form change. So simply getting the current index to reload the form would be a lot simpler.
I need to use a servlet because I want to be able to save and load this data from a file, and I don't believe you can locally save files using only javascript. I know I can LOAD using only javascript though, but the problem lies in the saving for now.
I know you can get the selected value of the list by doing something like
request.getParameter("year");
But I've found no way so far of getting the selected index.
Thanks for any help!
How can I get the selected INDEX of the list?
You can't - all that gets sent to the server when you submit the form is the value of the selected option (e.g. year=2009).
You'd have to add some client-side JavaScript to populate a hidden field in the form with the selected index whenever the selection changes.
i have a question when i try to capture the value of dropdown list from jsp file to servlet.
<select name="extraroom">
<c:if test="${booking.roomType ne 'single'}">
<c:forEach var="i" begin="0" end="${booking.count}">
<option value="<c:out value="${i}"/>"><c:out value="${i}"/></option>
</c:forEach>
</c:if>
</select>
i use Integer.parseInt(request.getParameter("extraroom"))
i have many drop down lists according to different roomType.
but the result in java servlet always get the value of the first dropdown lists...
what should i change in servlet?
Thank you.
For a select to be retrieved by a server the select has to
be inside a form and
have a unique name identifier inside (it; best the identifier is unqiue across the entire DOM tree)
I am new to a Java environment and JSP. My question is, is is possible to detect the value of a select dropdown option and create a small if statement depending on this value?
For example:
<select id="my_select">
<option value="-1">Option 1</option>
<option value="0">Option 2</option>
</select>
<c:if test="${id eq -1}">
Show this
</c:if>
<c:if test="${id gt -1}>
Show this
</c:if>
Thanks guys!
No, you have to first realize the lifecycle of the JSP. The output is formed on the server and sent to the client. Then the client performs some actions and possibly submits a form back to the server.
In your case you have two options:
use javascript
submit the form holding the dropdown, and depending on ${param.id} show one or the other
My form has a custom element like below, created using custom ajax:
<select jwcid="testtest <at> Any">
<option value="x">California -- CA</option>
<option value="y">Colorado -- CO</option>
<option value="z">Connecticut -- CN</option>
</select>
After the form is submitted, how do I get the value of this custom html element?
cycle.getPage().getComponents().get("testtest") ?
If I understand you correctly, you have a form element generated not by Tapestry, but by something else.
First of all, jwcid has no place in your HTML code, it's only used in Tapestry component templates. Second, the select element must have a name attribute or else your browser won't submit it at all:
<select name="name-of-element">
...
</select>
To get the submitted value on the server side, use cycle.getParameter("name-of-element") in your page/component class.