• Post Reply Bookmark Topic Watch Topic
  • New Topic

dynamic paging

 
Michael Waserman
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am trying to create a jsp to be able to call page by page data from the database. Something similiar to what all the search engines have:

<< Previous 1 2 3 4 5 6 7 8 9 10 11 Next >>

Statically it looks and works good, but I need to be able to call a servlet by clicking on the page number and pass as a parameter the page number that I am quering as weel as some other key information. Code example or a link to some tutorial/book is greatly appreciated.

Thanks. Mike.
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could set up your page links like so:

<a href="/myApp/myServlet?myParam1=value1&myParam2=value2">1</a>

In your servlet, you can get the parameters sent to it with request.getParam().

It is better still to not hard code the name of your application in a link. To avoid such hard coding, you can use JSTL or Struts custom tags.
Here are two examples (without parameters):

<a href="<c:url value="/myServlet" />">1</a>
<a href="<html:rewrite page="/myServlet" />">1</a>
 
Michael Waserman
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony, thanks for your reply.

I have not dealt with the JSTL or Struts custom tags before and looking up info on them on the net I found that some web server setup would be nessesary to use them. Is that true? I am an application programmer and do not have direct access to Iplanet instance. Could you advise what to read to gain more understanding about the custom tags?

Also, if I go with your first example - could you please give me some more details (sorry for my luck of knowlege)
<a href="/myApp/myServlet?myParam1=value1&myParam2=value2">1</a>
What does the "?" mean? What's "value1&myParam2=value2"?
Can I write this href dynamicaly using "out.println" inside while loop?
Do you think I have to create as many forms as pages or one form is fine as I pass the page number as a parameter? How to pass the key(for database qyery) parameter from the other form of the same jsp along with the page parameter from this form?

Thank you.
 
rahul V kumar
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you are trying to implement paging

<prev> 1 2 3 4 5 <next>

you might need to know the start and end points for the page selected.
These start and end points can be passed on to your application as suggested
<a href="/myApp/myServlet?myParam1=value1&myParam2=value2">1</a>

And yes the above URL must be constructed before the page is displayed to the user.

Here myParam1 could contain be the start point and myParam2 could contain the end point.

For example: if your search resulted in 100 results and you are displaying 15 results per page. Then when the user tries to access the third page you could pass

<a href="/myApp/myServlet?myParam1=30&myParam2=45">3</a>

In otherwords myParam1 & myParam2 are nothing but your query string parameters.

In your myServlet you could retrieve the values by
request.getParameter("myParam1")
request.getParameter("myParam2")

However if you wish to sort the results by any order then all those would become part of your query string. Something like

<a href="/myApp/myServlet?start=30&end=45&sortBy=EmpId&Order=Asc">3</a>

Hope I am not confusing you.

Coming to what Anthony was mentioning regarding not hardcoding the application name, you could however you <%=request.getContextPath()%>
This would give you the context root.

You could always clean up your code later once you have the functionality built in.

Do a search on Javaranch to get more help on how to implement this.
Also I found this link which has some sample code. I am no clue whether it is good or bad. You can evalulate it yourself.

http://www.devx.com/Java/Article/21383/1763/page/1

Hope this helps.
[ November 03, 2004: Message edited by: rahul V kumar ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!