Skip to main content

Custom Tags

JSP Tag Library:-
-->JspTag library is a library that contains set of readily available jsp tags. It is a java package holding classes, interfaces
-->These are 4 types of tag libraries
a.)Built-in jsp tag libraries(All standard action tags)
b.)JSTL
c.)3rd party jsp tag libraries
d.)Custom tag libraries(User defined)
Custom tag libraries(User defined):-
-->Writing java code in jsp is bad practice. The reasons are...
a.)Against of JSP programming principles(Tag based programming)
b.)Kills the readability of the code
c.)Kills the reusability of java code that is placed in jsp and etc...
-->To overcome these problems we need to use the following tags like html tags, jsp built-in tags, jstl tags, 3rd party tags and custom tags...
Terminologies for Custom Tags:-
a.)Tag handler class
-->The java class that defines the functionality of jsp tags is called Tag handler class
-->This class extends from TagSupport(C) directly or indirectly and defines two call back methods
  1. public int doStartTag(){•••}
  2. ->Executes when open tag encounters
  3. public int doEndTag(){•••}
  4. ->Executes when closing tag encounters
b.)TLD file (Tag Library Descriptor)
-->It is xml file where all the jsp tags of tag library are configured
-->It contains jsp tag names, tag handler class names, attributes names and etc...
c.)Empty Tag
-->Tag without body
  1. E.g: <start:example>
d.)Empty tag optional attributes
  1. E.g: <start:prime n=20>
e.)Tag with body
  1. E.g: <start:display>teja</start:display>
f.)Tag with body having optional attributes and mandatory attributes
  1. E.g: <start:display size="30" font="verdana">teja</start:display>
Steps to develop Custom TagLibrary:-
a.)Design tag library
<ABC>
<XYZ>
b.)Develop tag handler classes(src folder packages)
  1.  public class ABCTag extends TagSupport{
  2.   public int doStartTag(){
  3.       •••
  4.  }
  5.  public int doEndTag(){
  6.      •••
  7.  }
  8. }
XYZTag.java
  1. public class XYZTag extends TagSupport{
  2.   public int doStartTag(){
  3.       •••(g)
  4.  }
  5.  public int doEndTag(){
  6.      •••(g)
  7.  }
  8. }
c.)Configure JSP tags in TLD file
first.tld(xml entries)
  1. <ABC>=(f)=>ABCTag.class
  2. <XYZ>==>XYZTag.class
d.)Configure jsp tag library in web.xml file
web.xml
  1. <jsp-config>
  2.   <taglib>
  3.     <taglib-uri>demo(d)</taglib-uri>
  4.     <taglib-location>/WEB-INF/first.tld(e)</taglib-location>
  5.  </taglib>
  6. </jsp-config>
-->Every jsp tag library is identified with its taglib uri
e.)Use the jsp tags of tag library in jsp pages
test.jsp
  1. <%@taglib uri="demo" prefix="st"(c)%>
  2. ---->(b)
  3. <st:ABC/>
  4. ------->(a)
  5. <st:XYZ/>
  6. (h)-->output

Note: If two tags of two different tag libraries are used in same jsp having same name then they can be differentiated based on prefix(prefixes are user defined)

Custom Taglibrary
-->doStartTag(), doEndTag() methods are callback methods. These methods return numeric value as int constant values to provide instructions to ServletContainer towards performing additional operations on the jsp page

EVAL_PAGE(6)->Gives instruction to evaluate the remaining page
EVAL_BODY_INCLUDE(1)->Evaluatethe tag by including the body of the tag
SKIP_BODY(0)->Evaluate the tag by ignoring the body
SKIP_PAGE(5)->Stop evaluating the remaining jsp page tags
EVAL_BODY_AGAIN(2)->Evaluate the tag body for multiple times

-->pageContext object is protected member variable of TagSupport class, so in every TagHandler class the pageContext object is visible as inherited property because every TagHandler class is sub class of TagSupport class
-->Using this inherited pageContext object we can get access to other implicit objects in TagHandler class by using pageContext.getXxx()methods
pageContext.getOut(), pageContext.getServletConfig(), pageContext.getServletContext(), pageContext.getRequest(),pageContext.getResponse() and etc...
 Example App:-
CustomTaglibApp
|-->java resources
     |-->src
           |-->org.tags
                |-->ExampleTag.java
                |-->PrimeTag.java
                |-->DisplayTag.java
|-->web content
     |-->test.jsp
     |-->WEB-INF
          |-->first.tld
          |-->web.xml
Jars:- servlet-api.jar, jsp-api.jar

-->Use span tag to apply styles on one line of text, similarly use <div> tag to apply styles on multiple lines of text

Comments

Popular posts from this blog

JSP Comments

Comments in JSP:- -->Compiler /Interpreter doesn't take commented code for compilation /Interpretation, so the commented code doesn't participate in execution JSP supports 3 types of comments:- a.)HTML comments /Template text comments/ Output comments Syn :- <!---text---> -->Recognized by html interpreter of browser b.)JSP comments /Hidden comments Syn :- <%--text--%> -->JSP page compiler recognize these comments c.)Java comments /Scripting comments Syn :- //-->For single line /*-- -----*/-->For multiple line -->Java compiler(java) recognize these comments -->In Eclipse IDE JES class for first.jsp comes in our(workspace)folder/.metadata/.plugins/.org eclipse Comments (1)JSP comment<%--%> (2)Java comment(// or /*--*/) (3)HTML comments(<!---> JES Source Code (1)No (2)Yes (3)Yes In JES compiled code (1)No (2)No (3)Yes In the code going to browser (1)No (2)No (3)Yes Output (1)No (2)No (3)No -->Jsp comments are not visible in any phase...

Scripting Tags

JSP Tags/ Elements (1)Scriptlet     Standard syn:-         <%.........%>     xml syn:-        <jsp:scriptlet>...</jsp:scriptlet> Note: All scripting tags allows us to place script code(java code) 1.)The code placed in scriptlet go to _jspService(-,-) of JES class 2.)In a jsp page we can have zero or more scriptlets 3.)We place request processing logics in scriptlets 4.)Variables declared in scriptlets becomes the local variables in _jspService(-,-) of JES class in first.jsp In first.jsp: <% int a=10;  out.println("square:"+(a*a));%> In first_jsp.java(JES): public class first_jsp extends..{     public void _jspService(-,-){        int t=30;        out.println(“Square:”+(t*t));     }  } The code placed in scriptlet can use implicit objects of jsp because implicit objects and the code placed scriptlet goes to _jspService(-...

Project Architecture's

1.) Functional flow/ Architecture a.) Only for 6+/7+years b.) Explain the flow of process/business 2.) Technical Flow/Architecture a.) Upto 5+ years b.) Explain technologies /components that are involved in the project Servlet & JSP Project Architecture:- 1.)What is need of Business Deligate to convert VO class object to DTO class object ? a.) If Business Deligate is not there servlet component directly prepares DTO class object by receiving and translating form data and passes to service class, if service class excepts certain input in other format like numeric employee number as string employee number then we need to modify servlet component code i.e For the changes that happened in business tier service component we have to modify servlet component of presentation tier b.) If Business Deligate is taken then it gets original form data as VO class object having string inputs and converts VO class object to DTO class object to pass to service class c.) If service class excepts ...