How to get the value of drop down list? - java

I want to get the value to of drop down list, to send back to server to match something.
Update:
Sorry, i has to be clear on my question. I am using javascript to get values in client side and sending those back to server with DWR & processing them with JAVA code.
<select><option selected="selected" value="1">EEE</option><option value="2">ECE</option><option value="3">IT</option><option value="4">CSE</option><option value="5">MECH</option></select>
'
<input id="id" type="text" size="5"/>
<input id="name" type="text" size="15"/>
<input id="age" type="text" size="5"/>
<input id="age" type="text" size="5"/>
'
I want to get the values(1,2,3,4,5) ALONG with the other Name, Id, Age values.
I can get field text using dwr.util.getValues().
How can I get that select option value?

Since you specify no server-side language, I assume you want in HTML/JS. So, use this code in javascript, assuming the ID of your combobox is combo1:
<script type="text/javascript">
var combo1 = document.getElementById("combo1");
var val = combo1.options[combo1.selectedIndex].text;
//this will show the value in a Dialog Box
alert(val);
</script>

<select id="id"> ...
var opts = dwr.util.byId("id").options;

Try giving your select element a name attribute:
<select name='department'>...

Related

Passing Data from JSP to Servlets from HTML page dynamically

I have a JSP page which reads data from HTML and has CSS,Jquery code in it .
Now my webpage in jsp has two text labels and a add button next to them.
User can enter any no of values in the text field.
Now my requirement is that every time user enters the alue in these fields and clicks on add then that data should be passed on to my servlet. Servlet will basically do some validation and return a boolean variable.
Based on the value of this boolean, I shall change the appearance of my text boxes.
This is required to be done for every time user clicks on Add button.
How can I achieve this ?
My HTML code :
<div id="id1" name="id1" style="display: none;">Add a node: </br>
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP"> <input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="button" value="Add" name="addnodebutton" id="addnodebutton"/>
</div>
The value in ipaddress and port shall be passed on to my servlet and depending on return parameter, their appearance should change.
Can anyone enlighten me how this is actually going to work ?
TIA :)
For passing data to and from a servlet, you have options.
Option 1- You can wrap your html in a form tag and set the action/method properties for your servlet/http method like below:
<form method="POST" action="servletname">
<input type="text" name="ipaddress" id="ipaddress" placeholder="Enter Node IP">
<input type="text" name="port" id="port" placeholder="Enter Node Port">
<input type="submit" value="Add" name="addnodebutton" id="addnodebutton"/>
</form>
The submit would send a request with the input to your servlet. You would then need to handle your request parameters in your servlet, set your values/flags in your response object and forward to the user or jsp/html page of your choice.
Option 2- You can make an ajax call from your jsp, process your input and return a response to your page asynchronously. Example below:
A Simple AJAX with JSP example

Populate textbox with info from combobox

I need to create a jsp page to edit data from an oracle database.
I thought that the easiest thing I could do was to create a combobox showing important info
(in this case: id+ name of the material) but I have problems filling the text fields.
My idea is to fill those fields after the text on the combobox changes, but I don't really know how to do it. I know that with the info of the combobox, I have to do a query to obtain the data and then recall the jsp or something, but I don't exactly if I have to call a Servlet or what is the best way to do it.
Sorry if this question is repeated, but I didn't find any posts helpful.
Some code:
<sql:setDataSource dataSource="bd/login"/>
<sql:transaction>
<%--Vamos a hacer una consulta a la bd--%>
<sql:query var="resultado">
select * from material order by codigo
</sql:query>
</sql:transaction>
<form name="formActualizar" action="../ServletActualizarDatos" method="POST">
<select name="select" styleId = "tempId" onchange="ActualizarDatos()">
<option selected="selected"> -- Elija material --</option>
<c:forEach var="material" items="${resultado.rows}">
<option value="${material.codigo}">${material.codigo} ${material.nombre}</option>
</c:forEach>
</select><br/><br/>
Código<br/>
<input type="text" name="codigo" value="" /></p>
Nombre<br/>
<input type="text" name="nombre" value=""/></p>
Resistencia<br/>
<input type="text" name="resistencia" value="" /></p>
<input type="submit" name="bt" value="Actualizar Material" />
</form>
<script>
function ActualizarDatos()
{
String texto = this.options[this.selectedIndex].text;
Material m = ServletActualizarDatos.ObtainMaterialFromCombo(text);
this.form['codigo'].value=String.valueOf(m.getCodigo());
this.form['nombre'].value=m.getNombre();
this.form['resistencia'].value=String.valueOf(m.getResistencia());
}
</script>
Thank you for your help.
///CODE UPDATED, DOESN'T SEEMS TO WORK YET, THE EVENT IS NOT CALLED
You need jvascript to handle event when select box change to set the values of input fields from this select box selected value and selected option.
<select name="select" styleId = "tempId" onchange="this.form['codigo'].value=this.value;this.form['nombre'].value=this.options[this.selectedIndex].text;">

How to send label parameter from JSP to servlet?

I have a jQuery dialogue box which contains values as checkboxes. On selecting the checkboxes I am storing the selected values into label. Next I have to send these values from label as parameter through form to servlet but I don't know how to complete it.
Here is my code:
<form action="CallTimer" method="GET">
<label class="button2">Set Date: </label>
<input type="text" name="date" id="date" size="4">
<input type="Submit" name="Submit" value="Submit" id="Submit">
<br/>
Select Reporting Level
<label class="button2" style="display:none" id="depart"> Department</label>
</form>
I am retrieving these parameters in my Servlet as:
String reportname=request.getParameter("depart");
System.out.println(reportname);
But it is returning null values. Please help me.
Thanks in advance.
You have to use hidden input field:
<input type="hidden" name="depart" />
You need to understand what gets passed on form submission and what is not. In a nutshell, only values of the input fields get sent to the server. You have several ways to solve your problem:
Write value to a hidden input field
Modify the query string (what gets sent after ? in your GET request) during form submission (using java script):
?...&depart=xxx

value input field in foreach to input field outside foreach

The problem I have is best explained with a code example:
I have the following VIEW.jsp:
<c:forEach var="widget" items="${widgets}">
<div class="drag">
<p>Id: ${widget.id}</p>
<input class="editWidget" type="image" src="/tis/img/icons/edit.png" alt="Edit widget">
<input class="idWidget" type="hidden" value="${widget.id}">
</div>
</c:forEach>
<div id="editDialog" title="Edit widget">
<fieldset>
<input class="editWidgetId" type="hidden" value="??" id="editWidgetId">
</fieldset>
</div>
editWidgetId should have its 'value' attribute filled with the value of idWidget inside the foreach loop. This value should be different for each element in the loop (element is selected by edit button).
The questions:
How can I get the value of one input field to another input field?
How can I do this when a foreach loop is present?
Thanks in advance
Given you want dynamic behavior, this work has to be executed on the client-side by JavaScript as at the server-side, you have many widgets to one editDialog and are lacking the client-side user event to make your decision.
What you want to do is assign a function handler (or statement in my example below) to each editWidget to change the value of the editWidgetId input box with the appropriate value:
<input onclick="document.getElementById('editWidgetId').value = '${widget.id}'" class="editWidget" type="image" src="/tis/img/icons/edit.png" alt="Edit widget" >
Haven't tested this but I hope you get the idea

Updating database record after displaying to user

I have a question on how to update a particular record in a database using JSTL and java servlets.
Ok to call one record, I use a servlet to run a query to populate a jsp page using this type of format.
ResultSet results = getRecords.executeQuery(); //run database query
Result result = ResultSupport.toResult(results);
request.setAttribute("result", result);
RequestDispatcher rd = request.getRequestDispatcher("/showReport.jsp"); //redirect to this page with query info
rd.forward(request, response);
In the jsp page, I use this format to populate the page.
the row number is the corresponding to the index of the value on how the query is run.
<c:forEach var="row" items="${result.rowsByIndex}">
<form action = "/runthis/servlet?id=${row[0]}" method = "get" >
Employee Name<input type="text" value="<c:out value="${row[2]}"/>" />
Department<select>
<option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option></select>
<input type="submit" value="Save">
</form>
</c:forEach>
So for instance, if someone changes the Employee Name, but leaves the Department field alone, or vice versa, how would I go about this?
I think I should be able to kick this form action to a "update servlet", but I would rather not "update" every element in the record if I do not need to.
Also I'm still learning web dev, so if you can think of a better way to prepopulate stuff, via javascript or whatever, I'm willing to try that as well, if it's a possible solution.
Thanks for any help!
i think you need to do something like this:
<c:forEach var="row" items="${result.rowsByIndex}">
<form action="/runthis/servlet" method="post">
<input type="hidden" name="id" value="<c:out value="${row[1]}"/>" />
<input type="hidden" name="EmployeeName_orig" value="<c:out value="${row[2]}"/>" />
Employee Name<input type="text" name="EmployeeName" value="<c:out value="${row[2]}"/>" />
<input type="hidden" name="Department_orig" value="<c:out value="${row[4]}"/>" />
Department
<select id="Department" name="Department">
<option value="<c:out value="${row[4]}"/>"><c:out value="${row[4]}"/></option>
</select>
<input type="submit" value="Save" />
</form>
</c:forEach>
and then, in your update servlet (here called /runthis/servlet) you do some form of update employee and set the name and department where the row is id. note that i send the id in a hidden field, but it could just as well be sent in the url as you mentioned in our discussion above!
about your remark on updating every element in a record:
this can be done in different ways. one way is to load the record from database first, and compare the fields to know what fields to update. but that would require an extra read from the database before the update.
another way (the one i have prepared for in my proposed solution) is to have hidden fields, one for each input in your form. this hidden field holds the original value for its input counterpart. this way you can compare the original value with the new value without having to do a read from the database. on the other hand, you're now transferring the double amount of data over the line for each post request.

Categories