Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to recall if() in JSP 1

Status
Not open for further replies.

alimirmd

Programmer
May 22, 2007
4
SA
Hi,
My program is something like this:

java.util.Vector v1991=new java.util.Vector(30);
int f1=1;
int f2=10;
v1991=mb1.Sel(f1,f2);
// Here,mb1 is the bean and sel is the function where I select the
// records between employee number f1 and f2

if(request.getParameter("submitxyz")!=null)
{
f1=f1+10;
f2=f2+10;
v1991=mb1.Sel(f1,f2);
}
// submit xyz is my submit button.I get records 11 to 20 on submit
// but I need to get back here and if I click this again, I should get
// records 21 to 30.Again on click,I should get 31 to 40 and so on.

for(int l=l1;l<=(v1991.size()/6);l++)
{
<tr>
<td width="8%">
<form method="get" name="form1" action="content.jsp">
<fieldset>
<input type="radio" name="radio" value="<%=v1991.get(i)%>">
</fieldset>
</td>
<td width="7%">
<%
out.println(v1991.get(i).toString());
i++;
%>
</td>
<td width="17%">
<input type="text" name="text222" value="<%=v1991.get(i).toString()
%>">
<%
i++;
%>
</td>
<td width="17%">
<input type="text" name="text333" value="<%=v1991.get(i).toString()
%>" >
<%
i++;
%>
</td>
<td width="17%">
<input type="text" name="text444" value="<%=v1991.get(i).toString()
%>">
<%
i++;
%>
</td>
<td width="17%">
<input type="text" name="text555" value="<%=v1991.get(i).toString()
%>">
<%
i++;
%>
</td>
<td width="17%">
<input type="text" name="text666" value="<%=v1991.get(i).toString
().toString().substring(0,10)%>">
<%
i++;
%>
</td>
</tr>
<%
}
%>


Now,I want to go back to if() statement as soon as I finish this loop.This program would give me 1 to 10 records initially.When I click submitxyz (next) button,I would get 11 to 20.As soon as I get 11 to 20,if I click the button,I want to get 21 to 30.But it doesn't work...

my select query would be select * from emp where empno between f1 and f2;
(There is no problem with the syntax or query.The only problem is how to return to if() statement);

Plz help me ...........Pleeeeeeeeease....I shall be very thankful.
 
(You could search for display records with paging in yahoo)
I have to clarify something for you. It looks all your statements are written on one jsp and you want to do something
interactively. The web page shows first 10 records, you click one button and the web page shows next 10 record.
As the position of your for loop block is lower than the position of if block, there is no way to reuse the if block.
1)Moreover, people will use function to reuse code and you need to pass all parameters needed.
2)You can write if block one more time, you want to do this.
3)The approach for display 10 records is a bit wrong, I would suggest programming like this:
The jsp will read parameter firstRecordNum. If the jsp cannot get the value of firstRecordNum, it may show the record from
1 to 10.
If the jsp get 11 as firstRecordNum, the jsp will display 11th to 20th records.
You need to simplify your logic in this jsp.
 
Hi,
Thank you very much prosper for the reply.Actually,I am new to JSP.I don't have much programming skills,since I am at the begining level.But,I need to do this at any cost. Do u have any idea of how am I supposed to break my code into pages(like in case of forums,,,we get the page numbers hyperlink at the top) or in case of mails,where 1-10 mails are displayed on 1 page.when we click on next button,we get mails from 11-20.Do u have any idea about how to create new pages for every 10 records???
I can call a function.But,I cannot use some servlet keywords like request in a function written in JSP scriptlet. Since I have already posted the code,can u please tell me where to use the function???
Please help me,as I am new to these things.Even a small hint at this point would be a great help for me.
Thanks u very much....
 
Code:
<%@ page import="java.util.Vector"%>
<%
int defaultFirstRec = 1;
int defaultpageSize = 10;
int firstRecordNumVal = 0;
int pageSizeVal = 0;
String firstRecordNum = request.getParameter("firstRecordNum");
String pageSize = request.getParameter("page");
//mb1.Sel(f1,f2);
try{
System.out.println("start");
   firstRecordNumVal = Integer.parseInt(firstRecordNum);
   pageSizeVal = Integer.parseInt(pageSize); 
}
catch (Exception e){
   // There will be exception in the first time because there are no parameter got in this page
   firstRecordNumVal = defaultFirstRec;
   pageSizeVal = defaultpageSize;
}
Vector v = new Vector();
// building demo data
for (int m=firstRecordNumVal; m<firstRecordNumVal+pageSizeVal; m++)
    {
     v.add(String.valueOf(m));
    }
// end of building demo data
%>
<html>
<head>
<script lang="JavaScript">
function myset(firstNum, page)
         {
          document.myform1.firstRecordNum.value=firstNum;
          document.myform1.page.value=page;
          document.myform1.submit();
         }
</script>
</head>
<body>
<form name="myform1" action="mydisplay.jsp" method="post">
<input type="hidden" name="firstRecordNum">
<input type="hidden" name="page">
<table border="1" width="400">
<% for (int i=0; i<pageSizeVal; i++)
       {
        if (i>=v.size())
            {continue;
            }
        else{
             out.println("<tr><td colspan=\"2\">"+v.get(i)+"</td></tr>");
            }

     }
%>
<tr><td width="200">
<%if (firstRecordNumVal>pageSizeVal) {
int leftFirstNum = firstRecordNumVal-pageSizeVal;
%>
<input type="button" value="Prev" onClick="myset('<%=leftFirstNum%>','<%=pageSizeVal%>')"><%} else{%>&nbsp;<%}%></td>
<td width="200">
<%// please check the max number of record
  // if max number>(firstRecordNumVal+pageSizeVal), display next button
  if (true) {
 %>
<input type="button" value="Next" onClick="myset('<%=(firstRecordNumVal+pageSizeVal)%>','<%=pageSizeVal%>')"><%} else{%>&nbsp;<%}%></td>
</tr>
</table>
</form>
</body>
</html>
 
Thank u very much.You r the best,,,,Simply the best prosper.
U r code has provided me a skeleton to work.I was running after that submit button by taking it's value.U have given me another option of how to display next and previous records.....Thank u very very much.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top