Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How does servlet know which url to open?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to display one table contents on a webpage using pagination. I want to show 20 entries (20 columns) per page.
My web.xml file content (just the servlet part)



I want to use just one servlet to display all pages of pagination. How do i specify in servlet which 20 results it should show for which url? I mean for page/1 i want to show first 20 results, for page/2 next 20 results and so on. How do i specify that which result should be shown for which url using single servlet.
 
Ranch Hand
Posts: 81
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to add some attribute to the Request object, to determine which page you are in right now before invoking the servlet. On the servlet code you need to use the attribute to send the details to the UI accordingly.
 
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe, you should be using cached resultset etc... to mimic paging behaviour and let the servlet pass the start and end indices of the resultset to be displayed.
 
Tarun Kumar Agrawal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose page numbers are like:

1 2 3 4 5

and user clicks on 2. Then, it will execute CloudServlet servlet. How will i know the page number on which user clicked?
 
Madhan Sundararajan Devaki
Ranch Hand
Posts: 312
MS IE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe, you need to pass page number(s) information to the Servlet (either as query string parameters or hidden values or session attributes).
 
Tarun Kumar Agrawal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But in case of page/* how will i come to know what was the page no that was requested. because for all pages (since * is there), same servlet will be requested. So, how can init-param help me? What parameter should i pass in case of *?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can send the page number as a GET Parameter!

<a href="pageActionServlet?pageId=1">1</a>
<a href="pageActionServlet?pageId=2">2</a>

and/or used a session var to keep it
 
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming your search is normal database result set recieved from some SQL query ( search is not Lucene/Sor based)

After getting result set in your java code, find the size of result set (for example - you get 88 search result items).
Now your page can only show 20 records per page.
Total number of page becomes 88/20 = 5 (with 18 search items on 5th page)

in your jsp, you can set page numbers as shown below

Page numbers
href="/baadal/list/cloud/page/CloudServlet?pageNumber=1"> 1<
href="/baadal/list/cloud/page/CloudServlet?pageNumber=2"> 2<
href="/baadal/list/cloud/page/CloudServlet?pageNumber=3"> 3<
href="/baadal/list/cloud/page/CloudServlet?pageNumber=4"> 4<
href="/baadal/list/cloud/page/CloudServlet?pageNumber=5"> 5<

for example - when user clicks on page number 2 then you need to pick up the search items from 21 to 40 and display it.
To achieve this, you can retrieve value of pageNumber in your Cloud Servlet. And simply calculate lower and upper limit of search items.
do [(pageNumber-1)*20 +1] to get lower limit (which will be (2-1)*20 +1 = 21).
do [(pageNumber)*20] to get upper limit (which will be (2)*20 = 40). If upper limit exceeds total number of search items (88 in this example) then assign upper limit as total number of search items (88).

based on upper and lower limit, you can now show search resut from 21 to 40 when user clicks on page number 2.
Similiar approach can be applied for rest of pages.

~ abhay
 
Tarun Kumar Agrawal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if number of entries in table are quite high. Doing manually for each page doesn't seem such a good idea.
 
Tarun Kumar Agrawal
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also i want url to be displayed as /baadal/list/cloud/page/1, /baadal/list/cloud/page/2 and so on. So, GET doesn't seem good option..
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What if number of entries in table are quite high. Doing manually for each page doesn't seem such a good idea.



I assume by table you meant search result count. If your search result return 1000 records then using pagination size as 20 will results in 50 pages (and its corresponding hyperlinks)

In your jsp you can generate page numbers at runtime. before forwarding request to JSP, you would know the total number of pages to displayed.
Just propgate this total page count to JSP in request/session. Then in JSP, Just loop around with total page count (as shown below) and you will then get all page number hyperlink at run time (no manual hardcoding required)



I have used scriplets just for this sample , in actual JSP, prefer to use JSP tags for this task.


Also i want url to be displayed as /baadal/list/cloud/page/1, /baadal/list/cloud/page/2 and so on. So, GET doesn't seem good option..



if you want URL to be like /baadal/list/cloud/page/1 then modify href url (as shown below)



For example - if user has clicked on page 2 then URL will be /baadal/list/cloud/page/2. So in your Cloud Servlet , you just need to parse request URL to extract page number (2 in this case) and then continue to perform your business logic.


~ abhay
 
reply
    Bookmark Topic Watch Topic
  • New Topic