Class 2 Intermediate Java 30-IT-397  

Repeat.java

package utils;

// import the necessary libraries.
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.*;

/**
 * Simple custom JSP tag that prints a copyright where inserted.
 *
 * 12/18/2001 jonesbr@email.uc.edu
 **/

public class Repeat extends BodyTagSupport {

    int repeat = 0;
 
    /**
     * method doAfterBody
     *
     * Gets the body between the tags, repeats it a said number of times.
     *
     * @author jonesbr@email.uc.edu
     * date December 30, 2001
     */
 
    public int doAfterBody() {
        if (repeat-- > 0) {
            try {
                // Get the BodyContent object to manipulate the body.
                BodyContent body = getBodyContent();

                // Get the output stream so that we can write the new body out.
                JspWriter out = body.getEnclosingWriter();
 
                // Write out the body.
                out.println(body.getString());
 
                // If you don't do this, it will repeat what it has already written.
                body.clearBody();
            } catch (IOException e) {
                System.out.println("IOException in Replace: " + e);
            } catch (Exception e) {
                System.out.println("Exception in Replace " + e);
            }
            return(EVAL_BODY_TAG);
        } else {
            // Let them know I'm finished.
            return(SKIP_BODY);
        }
    }
 
    // setter method for repititions.
    public void setReps(String repeat) {
        this.repeat = Integer.parseInt(repeat);
    }
 
// End the class.
}

 Back

Created by:  Brandan Jones December 17, 2001