Java Server Page

JSP Projects

JSP Project

Write a program that display The circle Area and parameter ?
Previous Home Next
adplus-dvertising
Save as a circle.tld
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/
javaee/web-jsptaglibrary_2_1.xsd">
    <tlib-version>1.0</tlib-version>
    <short-name>circle</short-name>
    <uri>/WEB-INF/tlds/circle</uri>
    <tag>
        <name>area</name>
        <tag-class>r4r.co.in.Area</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>radius</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>parameter</name>
        <tag-class>r4r.co.in.Parameter</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <name>radius</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>        
</taglib>
save as a Area.java
/* Program is used for TagHandler */

package r4r.co.in;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class Area extends TagSupport {

    private int radius;

    public void setRadius(int radius) {
        this.radius = radius;
    }

    @Override
    public int doStartTag() throws JspException {

        try {

            int area = (int) (3.14 * radius * radius);
            pageContext.getOut().print("Area of Circle:" + area);

        } catch (java.io.IOException ex) {
            throw new JspException("Error in Radius tag", ex);
        }
        return SKIP_BODY;
    }
}
save as a Parameter.java
Program is used for TagHandler

package r4r.co.in;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class Parameter extends TagSupport {

    private int radius;

    public void setRadius(int radius) {
        this.radius = radius;
    }

    @Override
    public int doStartTag() throws JspException {

        try {

            int para = (int) ( 2 * 3.14 * radius );
            pageContext.getOut().print("Parameter of Circle:" + para);

        } catch (java.io.IOException ex) {
            throw new JspException("Error in Radius tag", ex);
        }
        return SKIP_BODY;
    }
}
<---Save as a circle.jsp-->
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/WEB-INF/tlds/circle.tld" prefix="circle" %>
<html>
    <head>
        <meta http-equiv="Content-Type"
		content="text/html;
					charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <B>Calling first Tag:</B>
        <P>           
           <circle:area radius="10"/>
        <P>
            <circle:parameter radius="10" />
    </body>
</html>
Output:
Previous Home Next