• Post Reply Bookmark Topic Watch Topic
  • New Topic

best method for getting current page?

 
Bob Peterson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to get the current page for a custom tag to handle Next/Previous links for a group of data. I just need it to go to the same page with a different page number.

I've searched this forum and found 3 methods:

HttpServletRequest.getRequestURL()
HttpServletRequest.getRequestURI()
and another one that I'm now forgetting. (getServletPath()?)

What's the difference between them and is there a preferred or 'best practice' method?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best practice would be to not rely on secondary data like what the page URL is. If you want to keep track of page 'numbers' that should be done via request parameters or scoped variables. Could you explain your requirements a little deeper?
 
Bob Peterson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you explain what you mean by the URL being secondary data?

The page numbers _will_ be supplied with request parameters. But when the user clicks on a 'Next' or 'Previous' link, they're still going to be going to the same .jsp, but with different page parameters.

The requirements are basically a group of tags to handle 'paging' through a collection of data, where the user provides a Collection of data and the pageSize (# of items per page), which together determine the # of pages.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So your URL is something like xyz.jsp?page=32 and you don't want to hard-code the xyz.jsp part in your links?

I don't see any reason not to -- it's quite customary -- but if you want to abstract it out, I would say that doing self-discovery on-page is a bit messy. In that case, I'd create a URL-mapping in the web.xml to create your page reference abstraction.

[With regards to my 'secondary data' comment -- it almost sounded like you were trying to do the "I want to know what page I came from in order to make decision on this page" mistake].
 
Bob Peterson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's what I want to do. Only it won't be done on the page, it will be done in a Custom Tag that is a kind of 'LinkTag', which figures all of this out. Should I use one of the methods I mentioned in the original post?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I use one of the methods I mentioned in the original post?


I guess you could. See what each returns to you and determine which one you want to use to parse out the page part.

But I wouldn't go that route. It's too iffy, too messy, and prone to breakage (what if at some point the page is the result of a forward? the URL will be useless).

Firstly, I rarely submit/link to JSP pages directly. I almost always go through a servlet controller which performs all the processing/decisions and forwards to the JSP which is merely responsible for formatting the view. I recommend this. (And in which case the issue at hand becomes moot since you would use the servlet mapping in your link).

If you don't want to do that and continue to link directly to the JSP, I would use the web.xml mapping technique to make sure that the mapping is deterministic.
 
Bob Peterson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree it's iffy and potentially problematic, but the problem is I don't know how it will be used. I'm only working on the taglibs as a component and was given scant examples of how it might be used.

But, for more flexibility, the taglib has also been designed to be used by passing itself as a scripting variable (via TEI because the spec is JSP 1.1) back to the page and I think the user could then supply their own href.

I'll probably be posting another thread question about TEIs soon, although after searching through the archives on them it looks like they're not much used because of the new spec and not well understood.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!