Java Server Page

JSP Projects

JSP Project

Write a program that display the Add and Subtract of two number ?
Previous Home Next
adplus-dvertising
Save as a mytaglib.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">
    <tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>math</shortname>
    <uri> </uri>
    <info>A simple tab library for the math examples</info>
    <tag>
        <name>add</name>
        <tagclass>r4r.co.in.Add</tagclass>
        <bodycontent>empty</bodycontent>
        <info>Tag is used for Addition of two number</info>
        <attribute>
            <name>num1</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>num2</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
    <tag>
        <name>sub</name>
        <tagclass>r4r.co.in.Subtract</tagclass>
        <bodycontent>empty</bodycontent>
        <info>Tag is used for Substraction of two number</info>
        <attribute>
            <name>num1</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
            <name>num2</name>
            <required>true</required>
            <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
</taglib>
Save as a Add.java
*/ Program is used for TagHandler */
package r4r.co.in;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class Add extends TagSupport {
    private int num1, num2;
    public void setNum1(int num1) {
        this.num1 = num1;
    }
    public void setNum2(int num2) {
        this.num2 = num2;
    }
    @Override
    public int doStartTag() throws JspException {
        try {
            int sum = (int) (num1 + num2);
            pageContext.getOut().print("The sum of two number: " + sum);
        } catch (IOException ioe) {
            throw new JspException("Error:" + ioe.getMessage());
        }
        return SKIP_BODY;
    }
}
Save as a Subtract.java
*/ Program is used for TagHandler */
package r4r.co.in;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
public class Subtract extends TagSupport {
    private int num1, num2;
    public void setNum1(int num1) {
        this.num1 = num1;
    }
    public void setNum2(int num2) {
        this.num2 = num2;
    }
    @Override
    public int doStartTag() throws JspException {
        try {
     	           int sub= (int) (num1 - num2);
              pageContext.getOut().print("If you subtract to number " + sub);
        } catch (java.io.IOException ex) {
            throw new JspException("Error in Subtract tag", ex);
        }
        return SKIP_BODY;
    }
}

Save as a math.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/mytaglib.tld" prefix="math" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>r4r.co.in</title>
    </head>
    <body>
        <B>Calling the first Tag: Under name of Add.java</B>
        <P>
            <math:add num1="25" num2="12" />
        <P>  <B>Calling the first Tag: Under name of Subtract.java</B>
            <P>
            <math:sub num1="25" num2="15" />
    </body>
</html>
Output:
Previous Home Next