Iterating a table row in jsp - java

1 [result page]2 [chooseMedicine page]How do i iterate a table row in my chooseMedicine.jsp page without loosing previous data selected by user on result.jsp page.I am loosing the previous selected medicine when i click on continue shopping.Is this possible by scriptlet tag?.....Thank you for your help.
<tr>
<td><input type="text" value="<%=request.getAttribute("Med")%>"></td>
<td><input type="text" value="<%=request.getAttribute("NumberOfTab")%>"</td>
<td><input type="text" value="<%=request.getAttribute("Cost")%>"/></td>
<td><input type="text" value="<%=request.getAttribute("TotalPrice")%>"/></td>
</tr>

You can try this
<% for(int i=0;i<10;i++){ %>
<tr>
<td><input type="text" value="<%=request.getAttribute("Med")%>"></td>
<td><input type="text" value="<%=request.getAttribute("NumberOfTab")%>" </td>
<td><input type="text" value="<%=request.getAttribute("Cost")%>"/></td>
<td><input type="text" value="<%=request.getAttribute("TotalPrice")%>"/></td>
</tr>
<% } %>
You can change the for loop condition based on your requirement.

Related

How to send and receive multiple radio buttons value from jsp to jsp

I have list of questions, each question have 4 options Excellent, Better, Good and Poor these questions need to be rated by user any 1 option for each question. I am able to fetch the QID, Question from the MySQL database and list them in table followed by 4 radio button and name of each group of radio button for each question is assigned as their QID. Here is code
<tr>
<td>Q. No.</td>
<td>Questions</td>
<td>Excellent</td>
<td>Better</td>
<td>Good</td>
<td>Poor</td>
</tr>
<%
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/feedback","root", "");
Statement st = con.createStatement();
ResultSet rs;
if(count>0){
rs = st.executeQuery("select * from question");
while(rs.next()){
int qno= rs.getInt(1);
String qname= rs.getString(2);
%>
<tr>
<td class="member"><%=qno %></td>
<td class="member"><%=qname %></td>
<td><input name="<%=qno %>" type="radio" value="1" required="true"/></td>
<td><input name="<%=qno %>" type="radio" value="2" required="true"/></td>
<td><input name="<%=qno %>" type="radio" value="3" required="true"/></td>
<td><input name="<%=qno %>" type="radio" value="4" required="true"/></td>
</tr>
<%
}%>
<tr>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
I am stuck here now, I am not able to figure how to send the value of selected radio buttons to the jsp page, where I have to fetch the valued and store them in database as 1/ 2/ 3/ 4 of each question.
Here the output of above code
enter image description here
Also I am submitting the view source of html code generated by jsp for reference -
<tr>
<td>1</td>
<td>How is your faculty communication</td>
<td><input name="1" type="radio" value="1" required="true"/></td>
<td><input name="1" type="radio" value="2" required="true"/></td>
<td><input name="1" type="radio" value="3" required="true"/></td>
<td><input name="1" type="radio" value="4" required="true"/></td>
</tr>
<tr>
<td>2</td>
<td>How is your study matrial</td>
<td><input name="2" type="radio" value="1" required="true"/></td>
<td><input name="2" type="radio" value="2" required="true"/></td>
<td><input name="2" type="radio" value="3" required="true"/></td>
<td><input name="2" type="radio" value="4" required="true"/></td>
</tr>
<tr>
<td>3</td>
<td>How your faculty language</td>
<td><input name="3" type="radio" value="1" required="true"/></td>
<td><input name="3" type="radio" value="2" required="true"/></td>
<td><input name="3" type="radio" value="3" required="true"/></td>
<td><input name="3" type="radio" value="4" required="true"/></td>
</tr>
Kindly give some code/ suggestion/ guidance.
Thank you to all in advance
After waiting for 1 hour, but I was trying in this period and found the how I can pass and retrieve values here is the solution -
I added 1 hidden field in my jsp where radio buttons are displayed, this hidden field will give me number of rows like this -
int totque=0;
rs = st.executeQuery("select * from question");
while(rs.next()){
++totque;
int qno= rs.getInt(1);
String qname= rs.getString(2);
String qnos=Integer.toString(qno);
%>
<tr>
<td class="member"><%=qnos %></td>
<td class="member"><%=qname %></td>
<td><input name="<%=qnos %>" type="radio" value="1" required/></td>
<td><input name="<%=qnos %>" type="radio" value="2" required/></td>
<td><input name="<%=qnos %>" type="radio" value="3" required/></td>
<td><input name="<%=qnos %>" type="radio" value="4" required/></td>
</tr>
<%
}%>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td><input type="hidden" id="thisField" name="inputName" value="<%=totque %>"></td>
<td><input type="submit" value="Submit" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
Then fetch the value of hidden field and the radio buttons values on another jsp page.
<% String hidden = request.getParameter("inputName");
int j=Integer.parseInt(hidden);
%>
<% String s[] = new String[j];
int k=0;
for (int i=0; i<j; i++){
k++;
s[i] = request.getParameter(Integer.toString(k));
out.println(s[i]);
}%>
I got the fine output as per my requirement. May this code help others.
And thank you for not replying my post, this make me learn lots of facts.
There is one more way to do this. You can assign same id lets say id="rb" to all the radio buttons and assign each a value using value="here you can use relevant filed from the DB". You will have a array of strings named cb whose values are assigned ffrom the DB field. Then on the next jsp retrieve the String[] cb using request.getParameter.Values("cb").

Sending data using <a href="#"> in the same page and display the result (FORM) using JSP without changing the (FORM) coding

<div id="main_search">
<table>
<tr>
<td>Product</td>
<td>:</td>
<td>
<input type="checkbox" name="product" class="prod_checkbx" value="Bilateral-VC">Bilateral-VC
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Bilateral Non-VC">Bilateral Non-VC
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget Domestic">Budget Domestic
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget High">Budget High
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget International">Budget International
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="IDD Buffet Base">IDD Buffet Base
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Premium">Premium
<br>
</td>
</tr>
<tr>
<td>Destination</td>
<td>:</td>
<td>
<input id="destination" type="text" name="destination" />
</td>
</tr>
<tr>
<td colspan="9" align="center">
<a id="search" name="search" class="button orange small" style="cursor:pointer" href="#">SEARCH</a>
</td>
</tr>
</table>
</div>
<div id="search_result" style="overflow: auto;">
<h2>Floor Price Edit</h2>
<table id="result-tbl" class="searchTable">
<tr>
<th>Destination</th>
<th>Product</th>
</tr>
<tr>
<% String dest=r equest.getParameter( "destination"); %>
<td>
<%=d est %>
</td>
</tr>
<% if (dest !=n ull) { %>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3307/icars" user="root" password="" />
<sql:query var="dest_result" dataSource="${dbsource}">
SELECT * from icx_pricing_desk WHERE DESTINATION LIKE '
<%=d est %>%'
</sql:query>
<c:forEach var="row" items="${dest_result.rows}">
<tr>
<td>
<c:out value="${row.DESTINATION}" />
</td>
</tr>
</c:forEach>
<% } %>
<td>
<% String products[]=r equest.getParameterValues( "product"); %>
<% if(products !=n ull) { %>
<% for(int i=0; i<products.length; i++) { %>
<%=products[i]%>
<sql:query var="prod_result" dataSource="${db }">SELECT * FROM icx_pricing_desk WHERE PRODUCT = '
<%=products[i]%>'</sql:query>
<c:forEach var="row" items="${prod_result.rows}">
<tr>
<td>
<c:out value="${row.PRODUCT}" />
</td>
</tr>
</c:forEach>
<% } } %>
</td>
</table>
</div>
This is some kind of form without <form> tag and for submit button it didn't use <input type="submit"> like normal form. It use <a href="#"> to submit the data.
The question is how do you send the data using the <a href="#">???
In addition, when i click the SEARCH button both <% String dest = request.getParameter("destination"); %> and <% String products[]= request.getParameterValues("product"); %> didn't received any data.
When using the standard form, both can display result. This code is written in same page(JSP).
--The first coding cannot be changed because it was the assignment and need to solve it.--
Add a form tag in your code like
<form name="form1" method="POST">
<table>
<!-- Your code ->
<a id="search" name="search" class="button orange small" style="cursor:pointer" href="javascript:void(0);" onclick="document.form1.submit();">SEARCH</a>
</table>
</form>
Things to remember
Form tag: method(POST), Name of form(form1)
Method called on onclick of a tag

Displaying List elements in JSTL <c:/forEach> with counter variable

I have a scriptlet for-loop that displays movie schedules from a List<Schedule>.
<%
#SuppressWarnings("unchecked")
List<Schedule> scheduleList =
(ArrayList<Schedule>) session.getAttribute("scheduleList");
for (int ctr = 0; ctr < scheduleList.size(); ctr++) {
%>
<tr>
<td class="center">
<input type="radio" name="selection" value="<%=ctr%>" required title="Please select a schedule." />
</td>
<td><%=scheduleList.get(ctr).getMallId()%></td>
<td class="center"><%=scheduleList.get(ctr).getScheduleCinema()%></td>
<td>PHP <%=scheduleList.get(ctr).getSchedulePrice()%></td>
<td><%=scheduleList.get(ctr).getScheduleDate()%></td>
<td><%=scheduleList.get(ctr).getScheduleTime()%></td>
<td class="center"><%=scheduleList.get(ctr).getScheduleSeats()%></td>
</tr>
<%
}
%>
Here is the Schedule bean object for reference:
public class Schedule {
private int scheduleId;
private int movieId;
private String mallId;
private int scheduleCinema;
private BigDecimal schedulePrice;
private Date scheduleDate;
private Time scheduleTime;
private int scheduleSeats;
// getters and setters
}
I have managed to convert it to JSTL with my desired number of iterations (related thread).
<c:forEach var="ctr" begin="0" end="${scheduleList.size()-1}">
<tr>
<td class="center">
<input type="radio" name="selection" value="${ctr}" required title="Please select a schedule." />
</td>
<td>${schedule.mallId}</td>
<td>${schedule.scheduleCinema}</td>
<td>${schedule.schedulePrice}</td>
<td>${schedule.scheduleDate}</td>
<td>${schedule.scheduleTime}</td>
<td>${schedule.scheduleSeats}</td>
</tr>
</c:forEach>
Output:
Firefox Inspector:
<tr>
<td class="center">
<input type="radio" title="Please select a schedule." required="" value="0" name="selection"></input>
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="center">
<input type="radio" title="Please select a schedule." required="" value="1" name="selection"></input>
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
Unfortunately, the schedule details are not displaying.
How do I display the data of each Schedule element from the List<Schedule> in JSTL?
(The ctr is important by the way, its value will be needed in the next action.)
Instead of
<c:forEach var="ctr" begin="0" end="${scheduleList.size()-1}">
Use:
<c:forEach var="schedule" items="${scheduleList}" varStatus="ctr">
<tr>
<td class="center">
<input type="radio" name="selection" value="${ctr.index}" required title="Please select a schedule." />
</td>
For ctr you could maintain separate counter variable and increment within your forEach.
You can use varStatus as well as var variable in c:forEach
varStatus="loop"
and use it as ${loop.index}
e.g. see the example

how do I dynamically print out a page that is loaded into text boxes?

I have a blur function that writes to the data base
$(".thoughts_box").blur(function(){
var box_id= $(this).attr("id").split("_")[$(this).attr("id").split("_").length-1];
writeListValue(1,box_id,$(this).val());
});
Here is my table
<td><input class="printArea_1 thoughts_box" name="activities_thoughts_1" id="activities_thoughts_1" type="text" style="width:345px;" placeholder="Type or click list button >" /></td>
<td><input type="button" id="thoughts_1" class="list_btn ext_thoughts" value="List >" name="_1" /></td>
</tr>
here is how I am printing it out
function print_list() {
$('input[id=printLoad_1]').val($('input[class=printArea_1]').val());
$('input[id=printLoad_2]').val($('input[class=printArea_2]').val());
$('input[id=printLoad_3]').val($('input[class=printArea_3]').val());
$('input[id=printLoad_4]').val($('input[class=printArea_4]').val());
$('input[id=printLoad_5]').val($('input[class=printArea_5]').val());
for( var i=1; i<=5; i++ ) {
if( document.getElementById('printLoad_'+i).value === '' ) {
document.getElementById('num_'+i).style.display = 'none';
}
}
$(".printing_list").printElement(
{
overrideElementCSS:[
'/css/print_fixer.css',
{ href:'/css/print_fixer.css',media:'print'}],
//leaveOpen:true,
//printMode:'popup',
});
}
here is the page that prints out I need to find a way to dynamically print out what the user puts into the text fields. can anybody please help me.
<div class="printing_list" id="printList" >
<img id="print_logo" src="/images/print_header_med.png">
<div align="left" id="printHead_text"></div>
<br />
<div align="left" class="listPrint_info" style="width:700px;"></div>
<br /><br />
<table width="100%" style="line-height:0px; margin-left:20px;" >
<tr id="num_1">
<td><input id="printLoad_1" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_2">
<td><input id="printLoad_2" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_3">
<td><input id="printLoad_3" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_4">
<td><input id="printLoad_4" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_5">
<td><input id="printLoad_5" type="text" style="width:700px;" />
</td>
</table>
</div>
elclanrs has the right idea. The code you wrote isn't maintainable. Try something like this:
// My print_list function assumes that I have some textboxes that are class
// textboxClass
function print_list() {
Array.filter( document.getElementsByClassName('textboxClass'), function(elem){
$(".printing_list").printElement( elem );
});
}
If you assign class textboxClass to all of your text boxes, this will loop through each of them, and you can do whatever you want.

using <form> within <foreach> a few time

I'm pretty new to Spring and I'm trying to use Spring MVC + JSP + JSTL. My goal is to make JSP which containts list of Users and allow to edit each User separately from others. So I think I should use separate <form> tag and separate <sumbit> button for every User in the list and my JSP looks like:
<c:forEach items="${userList}" var="currentUser" varStatus="index">
<form:form method="post" action = "edit" commandName="userList[${index}]">
<tr>
<td><form:input path = "userList[${index}].login" value = "${currentUser.login}" /></td>
<td><form:input path = "userList[${index}].password" value = "${currentUser.password}" /></td>
<td><form:input path = "userList[${index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
<td><form:input path = "userList[${index}].popServer" value = "${currentUser.popServer}" /></td>
<form:hidden path="userList[${index}].id" value=""/>
<td>
<spring:message code="label.delete" />
</td>
</tr>
<input type="submit" value = "edit">
</form:form>
</c:forEach>
the idea is to have an opportunity to edit each user separately by pressing the button "edit". Ofcourse this code doesn't work. It gives me an exception:
java.lang.IllegalStateException: Neither BindingResult nor plain
target object for bean name 'userList[javax' available as request
attribute
I'm really totally noob at Spring and at web-programming too. I'll appreciate any help.
Your use of ${index} isn't what you think. To get the index of the current item you must append ".index" to your varStatus variable... in your case it would be ${index.index}. For clarity, consider naming your varStatus something other than index...
<c:forEach items="${userList}" var="currentUser" varStatus="uStatus">
<form:form method="post" action = "edit" commandName="userList">
<tr>
<td><form:input path = "userList[${uStatus.index}].login" value = "${currentUser.login}" /></td>
<td><form:input path = "userList[${uStatus.index}].password" value = "${currentUser.password}" /></td>
<td><form:input path = "userList[${uStatus.index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
<td><form:input path = "userList[${uStatus.index}].popServer" value = "${currentUser.popServer}" /></td>
<form:hidden path="userList[${uStatus.index}].id" value=""/>
<td>
<spring:message code="label.delete" />
</td>
</tr>
<input type="submit" value = "edit">
</form:form>
</c:forEach>
Keep in mind, this doesn't make sense using a different form for each user... but say you wanted to edit any/all of your users at once....
<form:form method="post" action = "edit" commandName="userList">
<c:forEach items="${userList}" var="currentUser" varStatus="uStatus">
<tr>
<td><form:input path = "userList[${uStatus.index}].login" value = "${currentUser.login}" /></td>
<td><form:input path = "userList[${uStatus.index}].password" value = "${currentUser.password}" /></td>
<td><form:input path = "userList[${uStatus.index}].smtpServer" value = "${currentUser.smtpServer}" /></td>
<td><form:input path = "userList[${uStatus.index}].popServer" value = "${currentUser.popServer}" /></td>
<form:hidden path="userList[${uStatus.index}].id" value=""/>
<td>
<spring:message code="label.delete" />
</td>
</tr>
</c:forEach>
<input type="submit" value = "edit">
</form:form>
I dont understant why you are using userList[${index}] since you have a different form for each user. Anyway your code isnt right at this line commandName="userList[${index}]"
Here is what I suggest :
<c:forEach items="${userList}" var="currentUser" varStatus="index">
<form:form method="post" action = "edit" commandName="user">
<tr>
<td><form:input path = "login" value = "${currentUser.login}" /></td>
<td><form:input path = "password" value = "${currentUser.password}" /></td>
<td><form:input path = "smtpServer" value = "${currentUser.smtpServer}" /></td>
<td><form:input path = "popServer" value = "${currentUser.popServer}" /></td>
<form:hidden path="id" value=""/>
<td>
<spring:message code="label.delete" />
</td>
</tr>
<input type="submit" value = "edit">
</form:form>
</c:forEach>
According that your user class is user.

Categories

Resources