how to know that how many dynamic controls created? - java

I have created dynamic row on click of button in table using following code :
<script type="text/javascript">
var counter = 1;
function displayResult()
{
counter++;
document.getElementById("myTable").insertRow(-1).innerHTML = '<td><select name="list_dispatch_state" id="list_dispatch_state"><option value="01">01</option><option value="02">02</option><option value="03">03</option></select></td><td><input type="text" name="txt_email'+ counter +'" id="txt_email'+ counter +'" value='+ counter +'></td>';
}
</script>
<body>
<form action="Dogetdat" method="post">
<table id="myTable" border="1">
<tr>
<th>Select</th>
<th>Value</th>
</tr>
</table>
<br />
<button type="button" onclick="displayResult()">Insert new row</button>
<input type="submit">
</form>
</body>
my question is that, on click of button new row and control inside it created but when I click on submit then form submitted to servlet page.
Then how servlet will know that how many data are received ?
Because in servlet I ll get data using
String str1= request.gerParameter("txt_email");
how servlet will know that how many variable it have to create and what will be the name of that ? what will I have to pass in request.gerParameter(""); ?

You have to use the following request.getParameterValues and get the result as an array
String emails[] = request.getParameterValues("txt_email");

Related

Page can't capture data

This is my HTML page where there will be multiple rows depending on how many data will be in JSON file. The first problem I have is that I have problem with listing down all the color options that can be found in the JSON file. For now it only display only 1 value which is according to the fruit. I do not know what code I should include in the doPost method in Java
<form id="config_form" action="iLoveFruits" method="Post" enctype="multipart/form-data">
<table>
<c:forEach items="${fruits}" var="fruit" varStatus="count">
<tbody>
<tr>
<td><input type="text" name="fruit" id="fruit" value="${fruit.type}"></td>
<td>
<select name="color"><option id="color">${color}</option></select>
</td>
<input type="file" id="image" name="image" var="image" accept=".img,.IMG"/>
<label for="image">Upload</label>
</tr>
</tbody>
<button type="submit" id="submitBtn">Submit</button>
</form>
This is the codes under my doPost method where the file will be uploaded upon pressing the submit button. The issue I am facing is if there is 2 rows for example, whichever "Upload" button I click, it will display the file name under the first row. For example, I want to upload a image for the 3 row of the HTML page, after uploading the image, the name of the file will be shown in the first rather than the third. I do not know how to solve thi s please assist me
for (FileItem uploadItem : uploadItems) {
if (uploadItem.isFormField()) {
String fieldName = uploadItem.getFieldName();
if(fieldName.equals("fruit"))
{
String fruit = uploadItem.getString();
newOutput.put("fruit",fruit);
}
else if(fieldName.equals("color"))
{
String color = uploadItem.getString();
newOutput.put("color",color);
if (cert.toLowerCase().endsWith(".img")) {
File temp = new File(file_dir + File.separator + color);
uploadItem.write(temp);
}
}
}
}
I am trying to get the filename in the textbox according to the row that I am changing

How to get input value from one jsp page to another jsp page?

I am trying to get a one-page input value to another page while clicking the order button.
while I will take a number of item value and click the order button, it will carry the value the order page. The page code is here,
<td><%=rs.getString("product_price")%></td>
<td> <input type="number" name="no_item" value="1" /></td>
<td class="text-center" width="250">
Order
Edit
Delete
</td>
The order page code is here,
<%
statement = connection.createStatement();
String u=request.getParameter("u");
String item_num =request.getParameter("no_item");
int num=Integer.parseInt(u);
String Data = "select * from products_tbl where id='"+num+"'";
rs = statement.executeQuery(Data);
String product_price;
while (rs.next()) {
%>
<input type="hidden" name="id" value='<%=rs.getString("id")%>'/>
<div class="form-group">
<h4 style="float:left; padding-right:8px;">Product Name:</h4> <h4> <%=rs.getString("product_name")%> </h4>
</div>
<div class="form-group">
<%
product_price = rs.getString("product_price"); int num1 = Integer.parseInt(product_price); %>
</div>
<%= item_num %>
<%= num1 %>
<%
}
%>
Onclick of order button add the input value in url query string. you have need to use java script in jsp page.
<script>
function order(page, id){
input_value = document.getElementById('no_item').value;
location.href= page+"?u="+id+"&no_item="+input_value;
}
</script>
Add the onclick function within order button.
<a onclick="order('order.jsp', '<%=rs.getString("id")%>')" class="btn btn-success">Order</a>
In order.jsp page you will get the input value.
request.getParameter("no_item");

Why am I getting parameter value as null?

I hooked login.jsp and wrote following code in it:-
<script>
function a(){
var otp=document.getElementById("otpText").value;
document.location.href="?otp="+otp;
<% System.out.println(request.getParameter("otp")); %> //printing null
return false;
}
</script>
<form name="otp" method="post" action="" >
Enter otp:
<input type="text" name="otpText" id="otpText"/>
<input type="submit" value="Submit" onClick="return a()"/>
</form>
I typed some value inside otpText and that value is displayed in the url when I submitted the form. But when I am printing that value as in above code,it is printing null. I need that value as I want to store the same into session.Please help, any help would be appreciated.
If you change your code to
var otp=document.getElementById("otpText").value;
console.log (otp); // or alert (otp);
document.location.href="?otp="+otp;
you will see the value.

Creating Multiple textboxes dynamically

I am making a web application in which I pass a Int value from a servlet to next jsp page like this :
request.setAttribute("n",n);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/sharingfilesselection.jsp");
dispatcher.forward(request, response);
Now on the next page i.e sharingfilesselection.jsp I want that n textboxes are created dynamically each with a different id as i need to store the values of these textboxes in my database.
The N is obtained on next jsp page by this :
Object N=request.getAttribute("n");
How this can be done using javascript ?Please help
You can do this using JSTL:
<c:forEach var="i" begin="1" end="${n}">
Input ${i}: <input type="text" name="txtDynamic_${i}" id="txtDynamic_${i}" />
<br />
</c:forEach>
Try this in your
.javascript
<%String n=(String)request.getAttribute("n");%>
var n=<%=n%>;
for(var i=0;i<n;i++{
$(".exac").append("<input type="text" id='textbox"+i+"'></input>");
}
}
.html
<div class="exac">
</div>
Working sample here
html
<div id="container"><input type="button" onclick="createTextBox(5)" value="create textbox">
JS
function createTextBox(n) {
for (var i = 0; i < n; i++) {
var textBox = document.createElement("input");
textBox.setAttribute("id", "text_" + i);
document.getElementById("container").appendChild(textBox);
}}

Passing parameters to Javascript from jsp

I have a javascript function that I need to call on click of the delete button -
JavaScript:
function test(var1) {
alert(var1);
}
JSP code (logs is an ArrayList of length 10):
<table>
<%
for(int i=0; i<len; i++)
{
%>
<tr>
<td><%out.println(logs[i].getItemName());%></td>
<td><%out.println(logs[i].getItemDesc()); %></td>
<td> <input class="submit" type="submit" value="Delete" onclick="test(<%logs[i].getItemName();%>);"/></td>
</tr>
<%
}
%>
</table>
This will print the output as -
Name1 Description1 Delete_Button
Name2 Description2 Delete_Button
.
.
Name10 Description10 Delete_Button
I am not getting how to send the correct itemname value to the JavaScript, when I click on a corresponding row's delete button. Currently I am getting the value as undefined in the JavaScript alert when I click any of the delete buttons.
onclick='test("<%=logs[i].getItemName();%>");'

Categories