Wednesday, 28 November 2012

J2ee Tutorial 15 :- Creation of custom tag using BodyTagSupport

//  Creation of custom tag using BodyTagSupport example in this example we will take user gross salary,tax allowance and tax percentage and caliculate the net salary using tag lib

Step 1 :- Create a dynamic web project

Step 2 :- Create a html page were we can take the inputs from user as given bellow
//income.html
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.w3.org/1999/xhtml
                          http://www.w3.org/2002/08/xhtml/xhtml1-strict.xsd">
  <head><title>Input Income &amp; Tax Details</title></head>
  <body>
    <h1>Enter Income &amp; Tax Figures</h1>
    <form action="taxation.jspx">//transferring control to taxation.jspx
      <table>
        <tr>//user enterring details
          <td>Enter gross income:</td>
          <td><input type="text" name="gross" /></td>
        </tr>
        <tr>
          <td>Enter tax allowance:</td>
          <td><input type="text" name="allowance" /></td>
        </tr>
        <tr>
          <td>Enter tax rate (percentage):</td>
          <td><input type="text" name="rate" /></td>
        </tr>
        <tr>
          <td colspan="2"><input type="submit" value="Calculate Net Income" /></td>
        </tr>
      </table>
    </form>
  </body>
</html>

Step 3 :- Now create the taxation.jspx were we will be having our tag used to caliculate the result salary

<html xmlns:mytags="http://www.xyz.com/taglibs/mytags"
      xmlns:jsp="http://java.sun.com/JSP/Page">//declaring our tag in xhtml format if you want you can follow jsp format
  <jsp:output omit-xml-declaration="true" />
  <jsp:directive.page contentType="text/html" />
  <head><title>Taxation, Taxation, Taxation</title></head>
  <body>
    <h1>Calculated Net Income</h1>
    <p>My net income is:
    <mytags:netincome grossIncome="${param.gross}"
                      allowance="${param.allowance}"
                      taxRate="${param.rate}"
                      currency="GBP" />//our custom tag
    </p>
  </body>
</html>

Step 4 :- Now create the myTags.tld wnder the webcontent by creating a folder name tags and place in  it

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
  version="2.0">

  <tlib-version>1.0</tlib-version>
  <short-name>MyTagLib</short-name>
  <uri>http://www.osborne.com/mytags.tld</uri>
  <tag>
    <name>netincome</name>//our tagname implementation
    <tag-class>web.prac.TaxationTag</tag-class>
    <body-content>empty</body-content>
    <attribute>
      <name>grossIncome</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>allowance</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>taxRate</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>currency</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>

</taglib>

Step 5 :-  Now create the tag implementation class

package web.prac;

import java.io.*;
import java.text.*;
import java.util.*;

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

public class TaxationTag extends BodyTagSupport {

    private String grossIncome;

    private String taxRate;

    private String allowance;
   
    private String currency;
   
    public void setAllowance(String allowance) {
        this.allowance = allowance;
    }

    public void setGrossIncome(String grossIncome) {
        this.grossIncome = grossIncome;
    }

    public void setTaxRate(String taxRate) {
        this.taxRate = taxRate;
    }
    
    public void setCurrency(String currency) {
        this.currency = currency;
    }

    public int doStartTag() throws JspException {

        JspWriter out = pageContext.getOut();

        double gross;
        double rate;
        double allow;

        try {
            /* Convert string input to numerics */
            gross = Double.parseDouble(grossIncome);
            rate = Double.parseDouble(taxRate);
            allow = Double.parseDouble(allowance);
        } catch (NumberFormatException nfe) {
            try {
                out.write("Bad Input Figures");
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            return super.doStartTag();
        }
       
        double taxToPay = ((gross - allow) * rate / 100);
        double net = (gross - taxToPay);
        NumberFormat nf = NumberFormat.getInstance();
        Currency c = Currency.getInstance(currency);

        nf.setCurrency(c);
        nf.setMinimumFractionDigits(c.getDefaultFractionDigits());
        nf.setMaximumFractionDigits(c.getDefaultFractionDigits());
       
        try {            
            out.print(c.getSymbol() + nf.format(net));
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        return super.doStartTag();
    }
}


Step 5 :- Now map it in web.xml
<?xml version="1.0" ?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <jsp-config>
        <taglib>//declare our tag lib
            <taglib-uri>http://www.x.y.z.com/taglibs/mytags</taglib-uri>
            <taglib-location>/WEB-INF/tags/mytags.tld</taglib-location>
        </taglib>
    </jsp-config>
</web-app>

Step 6 :- Clean build and run the project

output :-Enter values an submit it



























No comments:

Post a Comment

Custom single threaded java server

 package com.diffengine.csv; import java.io.*; import java.net.*; import java.util.Date; public class Server { public static void main(Str...