Discussions

Web tier: servlets, JSP, Web frameworks: Problem in IterationTag in taglibs

  1. Problem in IterationTag in taglibs (2 messages)

    Hi All,
    My afterBodyBody method is not working can any one help,
    i am not getting the output..

    Actually my problem was when we input two number through tag libs i want
    to find the power....
    ie-- input 2 numbers num1 = 3; num2 = 3;

    i want the result is 3 power 3 = 27.

    Actuall there is no Problem in web.xml file or tadlibEx.tld file
    because my remaining taglibs are working fine only i am having problem in
    iteration body .........i want a loop

    -------------------------------------------------------
    my powerEx.java
    -----------------------------------------

    package tagEx;
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;

    public class powerEx implements IterationTag {
    private int number;
    private int power;
    private int result = 1;
    private int temp = 1;
    private PageContext p;
    private Tag t;

    public void setNumber (int number) {
    this.number = number;
    }
    public void setPower (int power) {
    this.power = power;
    }

    public powerEx () {
    super ();
    }

    public void setPageContext (PageContext p) {
    this.p = p;
    }

    public void setParent (Tag t) {
    this.t = t;
    }

    public Tag getParent () {
    return t;
    }

    public void release () { }

    public int doStartTag () {
    if (temp <= power) {

    return EVAL_BODY_INCLUDE;
    }
    else {
    return SKIP_BODY;
    }

    }

    public int doAfterBody () throws JspException {
    try {
    if (temp <= power) {
    result = result * number;
    temp++;
    return EVAL_BODY_AGAIN;
    }
    else {
    return SKIP_BODY;
    }
    }
    catch(Exception e) {
    throw new JspTagException("IO Error: " + e.getMessage());
    }

    }

    public int doEndTag () throws JspException {
    try {

    JspWriter out = p.getOut();
    out.write("Hi the Result of number#### " + number + " power#### " + power + " is " + result);
    }
    catch(java.io.IOException e) {
    throw new JspTagException("IO Error: " + e.getMessage());
    }
    return EVAL_PAGE;
    }
    }


    ------------------------------------
    my jsp where i am using taglibs

    tagEx.jsp
    --------------------------------------------

    <%@ taglib uri="myTagUri" prefix="my" %>
    <HTML>
    <HEAD>
    <TITLE>Custom Tag Example!</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF">
    <%! int number1;
    int number2;
    String name;
    %>
    <% number1 = Integer.parseInt (request.getParameter ("number1"));
    number2 = Integer.parseInt (request.getParameter ("number2"));
    name = request.getParameter ("name");
    %>
    <HR>Power is:<my:myTagPower number = '<%= number1 %>' power = '<%= number2 %>'/><HR>
    </BODY>
    </HTML>


    ------------------------------
    taglibEx.tld

    --------------------------

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <!DOCTYPE taglib
    PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
    "http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">

    <taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>simple</short-name>
    <uri>http://jakarta.apache.org/tomcat/example-taglib>
    <description>
    A simple tab library for the examples
    </description>
    <tag>
    <name>myTag</name>
    <tag-class>tagEx.helloTag</tag-class>
    <description> Display JSP sources </description>
    </tag>
    <tag>
    <name>myTagPower</name>
    <tag-class>tagEx.powerEx</tag-class>
    <description> Display JSP sources </description>

    <attribute>
    <name>number</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
    <name>power</name>
    <required>true</required>
    <rtexprvalue>true</rtexprvalue>
    </attribute>
    </tag>
    </taglib>

    -----------------------------
    web.xml file is also fine
    ----------------------------------------
    <taglib>
    <taglib-uri>
    myTagUri
    </taglib-uri>
    <taglib-location>
    /WEB-INF/sureshtaglibs/taglibEx.tld
    </taglib-location>
    </taglib>
  2. Problem in IterationTag in taglibs[ Go to top ]

    Why not simply use Math.pow(double, double) to calculate the power, instead of looping
  3. Yaaa but i want to use afterBody tag[ Go to top ]

    No Mr.kishore i want to use afterBody Tag to test so can u please help in this