JavaServer Page

March 12, 2007

Iterating with tags 101 } protected void parseElements()

Filed under: JSP Tag Libraries — webmaster @ 2:22 am

CHAPTER 10 Iterating with tags E Breaks the string list into a Java list. F Writes the results of this iteration back to the user and clears the body buffer. G If we have more elements in the list, exports a new iterator value and repeats evaluat ing the body. The work in SimpleForeachTag takes place in three designated locations: The service phase initialization in doStartTag(). The tag initializes the set of objects on which we plan to iterate, and determines if we need to process the body. This is not necessary if the list of objects is empty. The loop initialization in doInitBody(). The tag exports the needed iterator object by calling pageContext.setAttribute() with the name of the object and the object itself. In doing so, we publish the iterator as a scripting variable, so that it ends up in the scope in the JSP (a practice we first came across with JavaBean tags in chapter 8) . By exporting the iterator object, other tags and scriptlets can take advantage of it. The loop termination/repeating in doAfterBody(). The tag writes the results of the last loop into the previous writer (usually the writer that goes to the user) and then clears the body content to prepare it for the next iteration. In the final step, if there are additional items to iterate, the tag exposes a new iterator value and signals the JSP environment to repeat the execution by returning EVAL_BODY_TAG. NOTE When implementing iterations using tags, you do not have to write the results of each loop separately. You may instead wait for the body execution to finish (no more elements on which to iterate) and then write the complete result. Doing so usually results in improved performance, but it may also cause a delay in the user s receipt of the results. For example, consider reading a substantial amount of data from a database and presenting it to the user with some iteration on the result set. Since we are working with a database, completing the iteration may take a while and writing the response only on completion may cause the user to leave the page. Writing the result of each loop incrementally would (depending on buffer size) cause the results to return to the user incrementally, instead of in a large chunk. SimpleForeachTag s TagExtraInfo Following the development of SimpleForeachTag we must now create its TagExtraInfo counterpart. You may recall from our discussions of the TagExtraInfo class in chapters 6 and 8, we need to create a subclass of TagExtraInfo whenever we have a tag that exports a scripting variable. Since SimpleForeachTag will need

Note: If you are looking for good and high quality web space to host and run your application check Lunarwebhost Java Web Hosting services

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Sorry, the comment form is closed at this time.

Powered by Java Web Hosting