This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

controlling jsp:forward

 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

Sorry if this question sounds illogical.

I am working on a big jsp.
In the end of the jsp i have to iterate a collection & then redirect or forward to a different page(struts action) only if the collection consists of a specific value.

As we all know that the jsp:forward or sendRedirect has to be the first command in the jsp & i cannot have the same in the middle of a jsp page.

I would want to know if there is a workaround for this.

Because of this I have to check the collection twice. Initially in the start of the page i.e. to check if the collection comprises of the specific value.
& then as I mentioned earlier at the end of the jsp page where anyways I have to render the results of the collection.

Regards
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by manish ahuja:

As we all know that the jsp:forward or sendRedirect has to be the first command in the jsp & i cannot have the same in the middle of a jsp page.


Well that's not really accurate. It doesn't have to be the first thing, it just has to occur before the response gets committed (usually by a buffer flush).

If you are performing processing in order to make a decsion about what to display, doing it in a JSP just makes no sense at all. Do any such processing in a controller servlet which can then forward to the appropriate JSP based upon the result of your decision.

Perhaps this article on web app structure might be helpful.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and if you don't want to change your design then set a flag if you find that specific value, at the start, and then at last faorward or don't forward depends on flag value...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rathi ji:
... and if you don't want to change your design then set a flag if you find that specific value, at the start, and then at last faorward or don't forward depends on flag value...


I don't see how that helps at all. Once the response is comitted, that's that! Flag or no flag.

Adding bells and whistles to a poor design doesn't make it any better.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


I don't see how that helps at all. Once the response is comitted, that's that! Flag or no flag.

Adding bells and whistles to a poor design doesn't make it any better.


I think, I misunderstood the question. I thought, he is iterating the collection first for some reason and then again reiterating at last to decide to forward or not...

Sorry.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The double-iteration issue isn't really the issue at all. If the iteration needs to occur in order to determine whether to show a page or not, there's really no way to avoid iterating over it again in order to display it. (And so what? Iteration over a collection is generally very low overhead.)

My point was concerning using a JSP page in order to determine if another JSP page should be displayed. That's just not a very good practice.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only if the collection consists of a specific value.


And I'm not too sure what that means. Do you mean "only if the collection contains of a specific value"?
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thank you all for your response.

Earlier I just gave a brief description.

Here is the complete description of what I am doing.

In the Jsp page I display a list of records based on a certain criteria (i.e. All enabled records).

We display 7 records per page. I am using the jsp tags pager taglib for pagination.
The collection of the enabled records is set in the session.

All works fine till here. Now every record displayed is a link. The user has the ability to edit each record. Now say the user is on page3 record5 & he edits the same. From that editRecord page after the user clicks Save or cancel I have to return to the same page i.e. page3.

To acheive this I pass the page offset (i.e. page3 in this case) to the Edit page & when the user clicks save I have to fetch a new collection, set it in the session & redirect the user to page3 that is where he arrived from.

I have to fetch a new collection because the user has the ability to change the enabled record displayed to Disabled status & in the updated display he should not be shown the record which he has changed to disabled as the base criterias was to show only enabled records.

I am handling all pagination inside the JSP using pager taglibs.

Here is the problem scenario I am facing. Say I have 8 records. This will be in 2 pages. Page1 will have the first 7 records & page2 will have the singel (8th record).
Now I go & edit the 8th record. Since i have to retain the paginated page (i.e. page2 in this case) I pass the offset (2) to the edit page. Here the user can edit the record as he wants. If he is not changing the status then its fine. But now if he changes the status of the 8th record to Disabled then the collection size gets reduced & fits in 1 page.
In this case because i retain the offset(2) & redirect it from struts action. But in the collection i have only seven records so in this case I arrive at the second page & there are no records to display. Its shows the results something like this Showing 8-7 of 7 records.

To overcome this i have created a check in the pagination logic. Whenever the firstRecord(8) is greater than totalCount(7) as shown above, forward the user to the offset which is less than one. so in this case i will forward the user to offset (2-1) which is 1.
The attributes like firstRecord & totalCount is only available in the pager taglib which I use in the JSP.
This works. But only thing is pagination is the last section in the JSP & i cant do the forward successfully from the JSP at that level.

So in the initial section of the page i have to again reintroduce this pagination logic just to check for teh above scenario

Do respnd if you have any suggestions to avoid the JSP from get cluttered

Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic