Followers

JSP:Custom Tags (TagSupport3)

index.html
===========

<html>
    <head>
        <title>Custom JSP Demo </title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <form action="first.jsp" method="get">
            <p>Please enter your name : <input type="text" name="sName" ></p>
            <p>Please enter your address : <input type="text" name="saddress" ></p>
            <p>Please enter your Age : <input type="text" name="sage" ></p>
            <p><input type="submit" value="Submit" ></p>
           
        </form>
    </body>
</html>


first.jsp
=========
<%@ taglib uri="WEB-INF/mytags.tld" prefix="mytags" %>

 Welcome to this application
 <br ><br >
<%
String name=request.getParameter("sName");
String addr=request.getParameter("saddress");
String ag=request.getParameter("sage");
out.println("<br /><br />");
pageContext.setAttribute("sname", name);
pageContext.setAttribute("saddr", addr);
pageContext.setAttribute("sag", ag);
%>
<mytags:show>
Your details:
</mytags:show>
<br ><br >
BYE BYE
<br ><br >

ShowTag.java
============
package com.java9m.mohan;
import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
public class ShowTag extends TagSupport{
  int count = 0;
public int doStartTag() {
    count = 0;
    return (EVAL_BODY_INCLUDE);
}
public int doAfterBody(){
try{
JspWriter out = pageContext.getOut();
String name=(String)pageContext.getAttribute("sname");
out.print("Hello "+name);
out.println("<br /><br />");

String saddress=(String)pageContext.getAttribute("saddr");
out.print("address "+saddress);
out.println("<br /><br />");

String sAge=(String)pageContext.getAttribute("sag");
out.print("Age "+sAge);
out.println("<br /><br />");  

}catch(IOException ex){}
count++;
if(count < 2)
    return (EVAL_BODY_AGAIN);
else
    return (SKIP_BODY);
}
}

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

<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>simple</short-name>

  <tag>
    <name>show</name>
    <tag-class>com.java9m.mohan.ShowTag</tag-class>
  </tag>
</taglib>





No comments:

Post a Comment