JSP problem

Hi,

I have a requirement like I have a drop down which contains 1 to 20 numbers. I need to display number of rows in table grid on selection of the drop down. If i select 3 then it should show 3 rows , on selection of 10 should show 10 rows. Default should be 1 row as the drop down always show 1 by default. I have below code to get the data. But couldn't able to increment the row. I can always get a single record and i need to repeat the same row multiple times on selection of the drop down.

<%

Vector TransAmt = (Vector)request.getAttribute("TransAmt");

Vector CustRef = (Vector)request.getAttribute("CustRef");

out.println("

");

out.println("<table width=\"100%\" border=\"1\" bordercolor=\"Black\" cellspacing=\"0\" cellpadding=\"0\">");

out.println("<tr>");

out.println("<td class=\"bodyTableDataCol\" nowrap width=\"6%\" height=\"20\">Transfer Amount</td>");

out.println("<td class=\"bodyTableDataCol\" nowrap width=\"15%\" height=\"20\">Value Date</td>");

out.println("<td class=\"bodyTableDataCol\" nowrap width=\"15%\" height=\"20\">Customer Reference</td>");

out.println("<td class=\"bodyTableDataCol\" nowrap width=\"15%\" height=\"20\">Detail</td></tr>");

//for ( int row = 1; row >= intPer_page; row++ ){

out.println("<tr>");

out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">" + TransAmt + "</td>");

//out.println("<input type=\"text\" class=\"formItemMessageLabel\" name=\"FromValueDate\" width=\"12%\" height=\"20\">" value='" + FromValueDate + "' onChange=\"upperMe(this)\">");

//out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">" + FromValueDate + "</td>");

out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">");

out.println("<input type=\"text\" name=\"ValueDate\" size=\"11\" maxlength=\"11\" value='" + FromValueDate + "' onChange=\"upperMe(this)\">");

out.println("<A HREF=\"javascript:doNothing()\" onMouseOver=\"showtip(this,event,'Select from Calendar')\" onMouseOut=\"hidetip()\" onClick=\"setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('/mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')\">");

out.println("<IMG SRC=\"/mwintl11/images/calendar.gif\" BORDER=0></A></td>");

out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">" + CustRef + "</td>");

out.println("<td class=\"bodyTableDataAS\" nowrap width=\"12%\" align=\"center\" height=\"20\">");

out.println("<input value=\"Edit\" name=\"Edit\" tabindex=\"25\" type=\"button\" onClick=\"openNew()\" class=\"formButton\">");

out.println("</td>");

out.println("</tr>");

out.println("</table>");

%>

The drop down name is "per_page".

Can anyone please help on this?

Thanks.

[3211 byte] By [nsahooa] at [2007-11-27 7:04:33]
# 1

First, use JSP. You might as well be writing a servlet if you're gonig to do it like you have it below.

If I understand your problem correctly, you want to change a table's contents based on the selection of a dropdown. Do you want to do this w/o reloading the page? If so then you will need to create something lile a <span> or a <div> and change its text value using JavaScript.

I think its something like span.innerHTML="new data"

Here, the "new data" is the table.

If you are allowed to reload the page then your selection in the drop down should create a request with the selected value present.

Ex: if you use GET to load the page then GET the same url with &dropdown=<selected value>

awyorka at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 2
> First, use JSP. You might as well be writing a> servlet if you're gonig to do it like you have it> below.That IS a JSP, amazingly enough. Note that all of that Java code is actually a JSP scriptlet!
DrClapa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 3

Yes,that is a JSP and the java code written within scriplet. I need to load the page according to the number selection from drop down. I could get the value of the drop down through request object. My problem is to get the same row the number of times on selection of the drop down. I tried to put the for loop but it gave me some problem. Can you suggest something?

Thanks.

nsahooa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 4

> Yes,that is a JSP and the java code written within scriplet.

I can see that. Doing it that way makes it even more unreadable than JSP is normally. And what you did there is a WTF. Why not simplify it like this:<%

Vector TransAmt = (Vector)request.getAttribute("TransAmt");

Vector CustRef = (Vector)request.getAttribute("CustRef");

%>

<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">

<tr>

That at least would make it readable, so we might be able to understand what it had to do with your question.

DrClapa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 5

Hi,

I have modified the code to jsp and repeating the query again.

I have a requirement like I have a drop down which contains 1 to 20 numbers. I need to display number of rows in table grid on selection of the drop down. If i select 3 then it should show 3 rows , on selection of 10 should show 10 rows. Default should be 1 row as the drop down always show 1 by default. I have below code to get the data. But couldn't able to increment the row. I can always get a single record and i need to repeat the same row multiple times on selection of the drop down.

<%

Vector TransAmt = (Vector)request.getAttribute("TransAmt");

Vector CustRef = (Vector)request.getAttribute("CustRef");

%>

<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">

<tr>

<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td></tr>

<tr>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">

<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">

<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">

<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=CustRef%></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">

<input value="Edit" name="Edit" tabindex="25" type="button" onClick="openNew()" class="formButton">

</td>

</tr>

</table>

The drop down name is "per_page".

Thanks.

nsahooa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 6

Sorry. I think all things meshed up.

I have modified the code to jsp and repeating the query again. I have a requirement like I have a drop down which contains 1 to 20 numbers. I need to display number of rows in table grid on selection of the drop down. If i select 3 then it should show 3 rows , on selection of 10 should show 10 rows. Default should be 1 row as the drop down always show 1 by default. I have below code to get the data. But couldn't able to increment the row. I can always get a single record and i need to repeat the same row multiple times on selection of the drop down.

<%

Vector TransAmt = (Vector)request.getAttribute("TransAmt");

Vector CustRef = (Vector)request.getAttribute("CustRef");

%>

<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">

<tr>

<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>

</tr>

<tr>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"> <input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">

<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">

<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=CustRef%></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">

<input value="Edit" name="Edit" tabindex="25" type="button" onClick="openNew()" class="formButton"> </td>

</tr>

</table>

The drop down name is "per_page".

Thanks.

nsahooa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 7
What you should look at are the JSTL tag libraries. These allow you to put most loop and conditionals you might need in a JSP without mucking about with scriptlets (which are considered something of a last resort, these days).
malcolmmca at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 8
Hi,I am not using JSTL. I need this in simple JSP. I have to display a grid of records according to the dropdown number selection. Like if i select 3 then i should show 3 rows with same values int he rows.Thanks.
nsahooa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 9

> Hi,

>

> I am not using JSTL.

Why not?

> I need this in simple JSP.

Well it can be done with scriplets, but don't use all those prints. If you want to implement a loop with scriplets, for example, you do it something like;

<select name="fred">

<% for(String value : values) { %>

<option><%= value %></option>

<% } %>

</select>

malcolmmca at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 10

Hi,

Just for understanding I did like this.

Script:

function updatePerPageCount(objname)

{

//alert('I am inside updatePerPageCount');

var perpageStr, perpageInt;

perpageInt = objname.options.selectedIndex;

perpageStr = objname.options[perpageInt].value;

if (perpageStr != null)

{

document.RepetitiveInitiation.per_page.value = perpageStr;

return (true)

}

}

Setting in Java and getting that in JSP:

<%

String per_page = (String)request.getAttribute("per_page");

if (per_page == " " || per_page == null)

{

per_page = "";

}

%>

JSP:

<select name="per_page" style="width:50px" class="searchLabelWhite" onChange="return updatePerPageCount(this);">

<option value="1" ><%= per_page.equals("1") ? "selected" : "" %>>1</option>

<option value="2" ><%= per_page.equals("2") ? "selected" : "" %>>2</option>

<option value="3" ><%= per_page.equals("3") ? "selected" : "" %>>3</option>

<option value="4" ><%= per_page.equals("4") ? "selected" : "" %>>4</option>

<option value="5" ><%= per_page.equals("5") ? "selected" : "" %>>5</option>

</select>

And the grid:

<%

Vector TransAmt = (Vector)request.getAttribute("TransAmt");

Vector CustRef = (Vector)request.getAttribute("CustRef");

%>

<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">

<tr>

<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>

</tr>

<tr>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">

<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">

<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">

<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=CustRef%></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">

<input value="Edit" name="Edit" tabindex="25" type="button" onClick="openNew()" class="formButton"></td>

</tr>

</table>

In this grid can't I use a for loop to get the rows? Here I need to get the number of rows selected from dropdown (per_page) and i need the same rows number of times as selected from drop down. I am getting the per_page value but couldn't able to loop it here. Can you help me on this?

Thanks.

nsahooa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 11
Nobody can possibly be expected to follow that, please use code tags and sane line breaks.
malcolmmca at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 12

Hi,

Just for understanding I did like this.

Script:

function updatePerPageCount(objname)

{

var perpageStr, perpageInt;

perpageInt = objname.options.selectedIndex;

perpageStr = objname.options[perpageInt].value;

if (perpageStr != null) {

document.RepetitiveInitiation.per_page.value = perpageStr;

return (true)

}

}

Setting in Java and getting that in JSP:

<%

String per_page = (String)request.getAttribute("per_page");

if (per_page == " " || per_page == null)

{ per_page = "";

}

%>

JSP:

<select name="per_page" style="width:50px" class="searchLabelWhite" onChange="return updatePerPageCount(this);">

<option value="1" ><%= per_page.equals("1") ? "selected" : "" %>>1</option>

<option value="2" ><%= per_page.equals("2") ? "selected" : "" %>>2</option>

<option value="3" ><%= per_page.equals("3") ? "selected" : "" %>>3</option>

<option value="4" ><%= per_page.equals("4") ? "selected" : "" %>>4</option>

<option value="5" ><%= per_page.equals("5") ? "selected" : "" %>>5</option>

</select>

And the grid:

<%

Vector TransAmt = (Vector)request.getAttribute("TransAmt");

Vector CustRef = (Vector)request.getAttribute("CustRef");

%>

<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">

<tr>

<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>

</tr>

<tr>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">

<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">

<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">

<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20"><%=CustRef%></td>

<td class="bodyTableDataAS" nowrap width="12%" align="center" height="20">

<input value="Edit" name="Edit" tabindex="25" type="button" onClick="openNew()" class="formButton"></td>

</tr>

</table>

In this grid can't I use a for loop to get the rows? Here I need to get the number of rows selected from dropdown (per_page) and i need the same rows number of times as selected from drop down. I am getting the per_page value but couldn't able to loop it here. Can you help me on this?

Thanks.

nsahooa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 13

> In this grid can't I use a for loop to get the rows?

Of course

> Here I need to get the number of rows selected from

> dropdown (per_page) and i need the same rows number

> of times as selected from drop down. I am getting

> the per_page value but couldn't able to loop it

> here. Can you help me on this?

>

I just did. Put your loop start and end code in separate scriptlets, then use <%= expression %> to insert values from variables into the text.

malcolmmca at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 14

Hi,

Thanks for your immediate response. But still have some problem. I tried to loop like this but couldn't get any rows.

<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">

<tr>

<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>

</tr>

<%

for (int j = 0; j > per_page.length(); j++)

{

%>

<tr>

<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>

<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20">

<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">

<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">

<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>

<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20"><%=CustRef%></td>

<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20">

<input value="Edit" name="Edit" tabindex="25" type="button" onClick="modifyPayment()" class="formButton"></td>

</tr>

<%

}

%>

Can you suggest?

Thanks.

nsahooa at 2007-7-12 18:55:51 > top of Java-index,Java Essentials,Java Programming...
# 15

Hi,

Thanks for your immediate response. But still have some problem. I tried to loop like this but couldn't get any rows.

<table width="100%" border="1" bordercolor="Black" cellspacing="0" cellpadding="0">

<tr>

<td class="bodyTableDataCol" nowrap width="6%" height="20">Transfer Amount</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Value Date</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Customer Reference</td>

<td class="bodyTableDataCol" nowrap width="15%" height="20">Detail</td>

</tr>

<% for (int j = 0; j > per_page.length(); j++)

{

%>

<tr>

<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20"><%=TransAmt%></td>

<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20">

<input type="text" name="ValueDate" size="11" maxlength="11" value=<%=FromValueDate%> onChange="upperMe(this)">

<A HREF="javascript:doNothing()" onMouseOver="showtip(this,event,'Select from Calendar')" onMouseOut="hidetip()" onClick="setDateField(document.RepetitiveInitiation.ValueDate);top.newWin = window.open('/mwintl11/calendar.html','cal','dependent=yes,width=210,height=230,left=400,top=300,titlebar=yes')">

<IMG SRC="/mwintl11/images/calendar.gif" BORDER=0></A></td>

<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20"><%=CustRef%></td>

<td class="<%=(j%2==0)?"bodyTableDataBS":"bodyTableDataAS"%>" nowrap width="12%" align="center" height="20">

<input value="Edit" name="Edit" tabindex="25" type="button" onClick="modifyPayment()" class="formButton"></td>

</tr>

<%

}

%>

Can you suggest?

Thanks.

nsahooa at 2007-7-21 22:12:10 > top of Java-index,Java Essentials,Java Programming...
# 16
I think you might have more luck with j < per_page.length()
malcolmmca at 2007-7-21 22:12:10 > top of Java-index,Java Essentials,Java Programming...
# 17
Just want to know that is this the correct way I am following?Thanks.
nsahooa at 2007-7-21 22:12:10 > top of Java-index,Java Essentials,Java Programming...
# 18
Lord almighty!!! He just told you what you did wrong!> I think you might have more luck with j <> per_page.length()It should say j < per_page.length() and not j > per_page.length()
maple_shafta at 2007-7-21 22:12:10 > top of Java-index,Java Essentials,Java Programming...
# 19
I am sorry I couldn't follow that. Thanks.
nsahooa at 2007-7-21 22:12:10 > top of Java-index,Java Essentials,Java Programming...
# 20

> Lord almighty!!! He just told you what you did

> wrong!

>

> > I think you might have more luck with j <

> > per_page.length()

>

> It should say j < per_page.length() and not j >

> per_page.length()

No my apologies... I shouldn't have blown up like that.

maple_shafta at 2007-7-21 22:12:10 > top of Java-index,Java Essentials,Java Programming...
# 21
I appreciate your greatness. Thanks all your help guys. Atlast my problem is solved. I am really thankfull to all of yours valuable inputs. Regards.
nsahooa at 2007-7-21 22:12:10 > top of Java-index,Java Essentials,Java Programming...