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
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)
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)
<td>Branch</td>
<td>
<select name="branch" type="text">
<option value="0">Select Branch</option>
<option value="CE">CE</option>
<option value="IT">IT</option>
</td>
<td>Batch</td>
i.e if i have branch field in my jsp form
so my branch firld contains 2 inputs as 1.CE & 2.IT.
So when i click on CE ,so it have to show me A1,A2,....A5 & when i click on IT, it will have to show B1...B5 from which we can select any one of the field.
So, pls help me for above program.
Here is my code.
Pls help me what code should i write in "Batch" field""
you have to register onChange() event of the branch and call an javascript function which will populate the batch field accordingly.
The index page of the site will ask user to select a stage area. I want to pass their selection from page to page until they log out or return to this selection.
I have searched long and hard but just don't know how to phrase that question to a web search. Just need a little push in the right direction. Form code below and thanks in advance.
<form name="pick" method="POST" action="http://wavepick.htm">
<table border="0">
<tr VALIGN="TOP">
<td width="177">
<select id="stageChoice" name="stageArea">
<option selected value=" ">(Select Staging Area)</option>
<option value="A">A </option>
<option value="B">B </option>
</select>
</div>
<input type="submit" name="Submit" value="Stage" name="B1">
<input type="hidden" name="request_id" value="wavepick">
</form>
You can store the selected value between pages in a session variable.
If you want to do this only with HMTL/Javascript, you have to submit this form via "GET" (instead "POST"), and at each page you need to send again "stageArea" to the another page, so:
Page1.html?stageArea=B
Page2.html?stageArea=B
Page3.html?stageArea=B
That's how you read GET parameters via Javascript: How can I get query string values in JavaScript?
Ya, it's very arduous just with HTML/Javscript.
But you have mentioned "log out", do you have back-end (like PHP, ASP.NET,etc..)? With back-end language it'll be much more easier, you could store stageArea on session once, and just read it at each page.
If you are using a backend language such as PHP you can store that value in a $_SESSION variable. and access it wherever you need during that session.
Here is the documentation for the session variables:
PHP Session
If not, let me know and i'll figure out a different method for you.
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.