• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to create page 1 2 3 in jsp?  RSS feed

 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have jsp file and inside i have list that i get from my servlet .
i want to create in the bottom of the page the option to move from one page to another like this " page: 1,2,3,4,5"
i try to use the tag <a href=.. and onclick() event ,and i understand that javascript will not work ,

someone know how can i create the " page: 1,2,3,4,5" ?
 
Mohnish Dukhande
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i understand correctly you want to create pagination.Check the following link where server side pagination is done.
http://mrbool.com/how-to-handle-pagination-using-jsp-pages/28345 and if you want to do client side pagination using following link
http://www.javaworkspace.com/paginationUsingJavaScript.do
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Asaf,
You need to know the number of records you want to display on each page. And divide the total number of records in the list by that number to get the page numbers. Note that you will need to add 1 to that result if the remainder of the division is not 0. You will then need to know the last record of each page that you will pass on to the next page. This last record should be a unique column value(number preferably). Note that the column should be sorted.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asaf shay wrote:,and i understand that javascript will not work

Why not?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use an existing tag library: http://www.servletsuite.com/servlets/pagertag.htm (that one is commercial, though).
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could not solve the problem and I'm at a loss

i have coupns list . i want to create in the bottom page number . in every page i define to have 3 coupon Maximum."every page" i mean it's the same page !!
i just want to change the index from where to start to print the coupons.


i try to create static variable
<%! static int indicateFromWhereToStartPrint = 0; %>
and the pages numbers i create like this
<a href="#" onclick="goToPage(2)">2</a>
i did this for every page .
in the function i increased if necessary the indicateFromWhereToStartPrint , the problem is the variable is incress even when i do reresh to the page
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not any have variables or any other code in the JSP page. Perform the data handling in the servlet controller. Keep track of what "page" you are on using a hidden variable in the form.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Asaf,
It is very difficult to guess the logic you use in your jsp page. When you have divided the number of records in your coupns list by 3, just go through the list and record the indexes at position 1, 4, 7... You will then pass these indexes like <a href="yourjsppage.jsp?index=<%= index[0]%>">1</a> <a href="yourjsppage.jsp?index=<%= index[2]%>">2</a>...

and you build your query as follows:

"select * from yourtable where index>="+(request.getParameter("index")==nul)?index[0]:request.getParameter("index")+" order by index asc limit 3"

Hope i will be understood.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After I tried a few things and I failed I decided to try a session, I guess it will work. Anyway thanks helpers.
 
Paul Ngom
Ranch Hand
Posts: 355
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

After I tried a few things and I failed I decided to try a session, I guess it will work. Anyway thanks helpers.

I think if you had followed Bear's advice or mine, together we could have come out with something that works. But good luck in your new endeavor. I hope you will get a right solution.
 
asaf shay
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You should not any have variables or any other code in the JSP page. Perform the data handling in the servlet controller. Keep track of what "page" you are on using a hidden variable in the form.


1)i have a list that come from the servlet ,i need to print this list how can i do that if not using inside the jsp ?

2)"Keep track of what "page" you are on using a hidden variable in the form" i do not know which way you go


my solution is to save in session the index and For example if the user click the page 2,i send to servlet the number 2 and he will change the value that i store in session that track the index . and the user
will be in the same page but now the page print the coupons from the new index .Is this idea right ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!