I have this code in one jsp and i want to sent to the another and display selected value and correct answer. In the first page i have this code:
<form method="post" action="result.jsp" >
<p>choose answer</p>
<select name="fill">
<option value="0">Fill</option>
<option value="1">England</option>
<option value="2">China</option>
<option value="3">France</option>
</select>
<input type="submit" name="sent" />
</form>
and then the second jsp:
<body>
<jsp:declaration>
String s = "";
</jsp:declaration>
<jsp:scriptlet>
s = request.getParameter("fill");
</jsp:scriptlet>
<h1>Your answer is <jsp:expression>s </jsp:expression>and correct is England</h1>
</body>
And it returns null, why?
Related
I'm doing a java web application using jsp and servlets.
I have a form and I retrieve different values from my db to populate an option. This is my form:
<form class="form" action ="<%=request.getContextPath()%>/aggiungiLibro" method ="post">
<div class="form__group">
<input type="text" placeholder="Titolo" class="form__input" name = "titolo"/>
</div>
<div class="form__group">
<input type="text" placeholder="Quantità disponibile" class="form__input" name="quantita" />
</div>
<div class="form__group">
<select class="form__input" name="autore">
<c:forEach items="${listaAutori}" var = "autore" >
<option value="${autore.idAutore}"><c:out value="${autore.nome} ${autore.cognome}" /></option>
</c:forEach>
</select>
</div>
<button class="btn btn-light" type="submit">Inserisci nuovo libro</button>
</form>
In the servlet I retrive my data with
String titolo = request.getParameter("titolo");
int quantita = Integer.parseInt(request.getParameter("quantita"));
int autore = Integer.parseInt(request.getParameter("autore"));
I get right values for 'titolo' and 'quantita' but I get the string '${autore.nome}' for 'autore'.
It seems like it doesn't replace the string with the actual value.
The field idAutore is spelt right.
(In the jsp page I see the correct values for ${autore.nome} ${autore.cognome})
Anyone can help me please? What I'm doing wrong?
I solved replacing this:
<option value="${autore.idAutore}">
with this:
<option value="<c:out value='${autore.idAutore}' />">
Well in the project that I am working there's a database table containing the following fields :
From the database table I want to show beds that's available in the ward that's not occupied when the ward is selected , then in bed select tag should show only available beds .
The jsp form select tag code is :
<div class="form-group">
<label for="course">Ward No:</label>
<div class="form-select">
<select name="Ward" id="course">
<option value="0">--Select Ward No---</option>
<option value="ward1">1</option>
<option value="ward2">2</option>
<option value="ward3">3</option>
<option value="ward4">4</option>
<option value="ward5">5</option>
<option value="ward6">6</option>
<option value="ward7">7</option>
<option value="ward8">8</option>
<option value="ward9">9</option>
<option value="ward10">10</option>
<option value="ward11">11</option>
<option value="ward12">12</option>
</select>
<span class="select-icon"><i class="zmdi zmdi-chevron-down"></i></span>
</div>
</div>
<div class="form-group">
<label for="bed">Bed No:</label>
<div class="form-select">
<select name="Bed" id="course">
<option value="0">--Select Ward No---</option>
<option value="bed1">1</option>
<option value="bed2">2</option>
<option value="bed3">3</option>
<option value="bed4">4</option>
<option value="bed5">5</option>
<option value="bed6">6</option>
<option value="bed">7</option>
<option value="bed8">8</option>
<option value="bed9">9</option>
<option value="bed10">10</option>
<option value="bed11">11</option>
<option value="bed12">12</option>
</select>
<span class="select-icon"><i class="zmdi zmdi-chevron-down"></i></span>
</div>
</div>
So the question is how can I do this ?
I am new java development and this is my first project , so please excuse if any silly mistakes are found.
Thank you in advance for all helping out , its much appreciated.
The simple answer to your question is AJAX
Do an AJAX call to get the available beds from DB after selecting the ward number.
Form the bed dropdown dynamically.
Example:
<select name="Ward" id="course" onChange="fetchAvailableBeds()">
......
</select>
function fetchAvailableBeds()
{
var wardNo = $("#course").val();
var queryUrl = "REST_URL";
$.ajax({
url : URL,
data : "wardNo="+wardNo,
type : "POST",
success : function(data, textStatus, jqXHR)
{
var responseJSON = jqXHR.responseText;
var responseParsedJSON = JSON.parse(responseJSON);
var bedDetails = responseParsedJSON.bedDetails;
if(bedDetails.length > 0)
{
$('#bed').empty().append('<option>--Select Bed--</option>');
for(var i=0;i<bedDetails.length;i++)
{
$('#bed').append('<option value="'+bedDetails[i].name+'">'+bedDetails[i].id+'</option>' );
}
}
else
{
$('#bed').empty().append( '<option>--No Beds Available--</option>');
}
}
});
}
After selecting the ward number you can generate the select tag as string from database using for loop and print out the result on html
I have a dropdown which consist the language names. I am setting the value and displaying name of the dropdown by using a hashmap.
<form action="TextTranslation" method="post" class="form" role="form" >
<div class="row">
<div id = "imageView" class="col-lg-8 center-block ">
<div class="btn-group">
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> </option>
<%
System.out.println((String)key);
}
String name = request.getParameter("country");
request.setAttribute("code", name);
%>
</select>
</div>
<input type="submit" class= "btn btn-image" value="Translate">
Search Text
</div>
</div>
</form>
Values are passed correctly to dropbox as it print all the values in console. the set attribute is accessed in the particular servlet. But it gives a null value. Do you have any idea?Thank you in advance
UPDATED
<select name="country">
<%
Map<String,String> langCode = x.getCountryList();
for( Object key :langCode.keySet() )
{%>
<option value="<%=(String)key%>"><%=langCode.get(key) %> /option>
<% System.out.println((String)key);
}
String name = request.getParameter("country");
%>
</select>
<input type="hidden" name="code" value = <%= name%>/> .
In the servlet I used,
request.getParameter("code");
update your jsp likewise,
<form...>
...
<input type="hidden" name="code" value = <%= name%>/>
....
</form>
then get it from your servlet likewise,
request.getParameter("code"); // will return value of code
NOTE :
Remove from your jsp-code if above solution you gonna implement then,
request.setAttribute("code", name);
I dynamically generate the select tag using jstl like below.But I am failed to select the option in jquery.
JSP:
<c:forEach var="p" items="${model.multiphase}" varStatus="row">
<select class="form-control status" name="status">
<option value="Notyetstarted">Not Yet Started</option>
<option value="Inprogress">InProgress</option>
<option value="Onhold">OnHold</option>
<option value="Closed">Closed</option>
</select>
<input type="hidden" id="upstatus_${row.index}" value="<c:out value='${p.getProjectPhase().getPhaseStatus()}'/>" />
</c:forEach>
the above jstl generates the below html
<select class="form-control status" name="status">
<option value="Notyetstarted">Not Yet Started</option>
<option value="Inprogress">InProgress</option>
<option value="Onhold">OnHold</option>
<option value="Closed">Closed</option>
</select>
<input type="hidden" id="upstatus_0" value="Closed" />
<select class="form-control status" name="status">
<option value="Notyetstarted">Not Yet Started</option>
<option value="Inprogress">InProgress</option>
<option value="Onhold">OnHold</option>
<option value="Closed">Closed</option>
</select>
<input type="hidden" id="upstatus_1" value="Onhold" />
I have used below jquery for selecting the option.
$(".status option").each(function () {
alert("fd");
$(this).find("option").filter(function () {
return $(this).val() == $("#upstatus_" + index).val();
}).prop('selected', true);
});
How to preselect the value?
Any help will be greatly Appreciated!!!
change to this:
$(".status").each(function () {
var that = $(this);
$(this).find('option').filter(function (index) {
console.log($(this).parent().next(":hidden").val());
return $(this).val() === that.next(":hidden").val()
}).prop('selected', true);
});
Sample Demo
Updated Sample Demo
-> : jquery not enters in the alert.
because you have .each() iteration on the option not on the select element.
Although you can change to this also:
$(".status").each(function (i) { // add a index "i" here
$(this).find('option').filter(function () {
return $(this).val() === $("#upstatus_"+i).val() // use that here
}).prop('selected', true);
});
Sample for this.
<html>
<head>
<style>
div { color:red; }
</style>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<select name="sweets" multiple="multiple">
<option>Chocolate</option>
<option selected="selected">Candy</option>
<option>Taffy</option>
<option selected="selected">Caramel</option>
<option>Fudge</option>
<option>Cookie</option>
</select>
<div></div>
<script>
$("select").change(function() {
var str = "";
$("select option:selected").each(function() {
str += $(this).text() + " ";
});
$("div").text(str);
}).change();
</script>
</body>
</html>
This my code I want to display on console in jsp on Select change passing the value.I want to Print onslected item in Jsp page
<%
String k=request.getParameter("sweets");
out.println(k);
%>
Like this I want to Print on select item data please help me
Enclose select box with-in form tag and in
onchange method of select , trigger form submit
<form id="resultform" action="resultPage.jsp">
<select name="sweets" multiple="multiple">
<option>Chocolate</option>
<option selected="selected">Candy</option>
<option>Taffy</option>
<option selected="selected">Caramel</option>
<option>Fudge</option>
<option>Cookie</option>
</select>
</form>
In jquery method write
$("select").change(function() {
$('#resultform').submit();
}