Nearby lessons

29 of 30

JSP Examples - Custom Tags

📌 What You Will Learn
  • Build classic and simple tag handlers
  • Understand the tag life-cycle methods
  • Write tag files under /WEB-INF/tags

Custom tag development from scratch: the classic Tag/TagSupport/IterationTag/BodyTag models, SimpleTag and SimpleTagSupport, tag files, dynamic attributes, and accessing implicit objects from tag handlers — each with a TLD and handler source.

Unit 5: Custom Tags

Custom Tag

Classic Tag Simple Tag Tag Files

(JSP 1.1)(JSP 2.0) (JSP 2.0)

Components of Custom Tag Application:

Custom tag application contains the following 3 components

1.Tag Handler class:

It is a simple java class which defines entire required functionality

Every tag handler class should compulsory implements Tag interface either directly or indirectly.

Web container is responsible for creation of Tag Handler object. For this it always invokes public

no-arg constructor. Hence every tag handler class should compulsory contains public no-arg

constructor.

2.TLD file(Tag Library Descriptor):

It is an xml file which provides mapping b/w JSP(where custom tag functionality is required) and

tag handler class(where custom tag functionality is available).

3.taglib directive:

It makes custom tag functionality available to the jsp. It defines the location of the tld file.

Execution Flow of Custom tag application

Custom Tag

Invocation

taglib

Directive

tld File

TagHandler

Class

1.Whenever jsp engine encounters a custom tag, it identifies prefix and checks for the

corresponding taglib directive with matched prefix.

  • From the taglib directive jsp engine identifies the location of the tld file
  • From the tld file JSP engine identifies the corresponding Tag handler class.
  • JSP engine executes tag handler class and provides required functionality to the JSP.
Example01
JCode Cell
1 
2Classic Tag Model(JSP 1.1V)
3Simple Tag Model(JSP 2.0V)
4Tag Files(JSP 2.0V)
5

Tag Extension API

It is the base interface for all custom tag handlers.

Every tag handler class should compulsory implement this interface either directly or indirectly.

This interface defines 6 methods which are applicable for any Tag Handler object.

We should go for this interface if we are not manipulating Tag body and iteration is not required.

Example02
JCode Cell
1 
2Tag(I):
3

Tag Extension API

It is the child interface of Tag. We should go for this interface ,if we want to consider tag body

multiple times without any manipulation.

It contains only one extra method: doAfterBody()

Example03
JCode Cell
1 
2IterationTag(I):
3

Tag Extension API

It is the child interface of IterationTag. If we want to manipulate Tag body then we should go for

BodyTag.

This interface defines 2 extra methods

setBodyContent() and doInitBody()

Example04
JCode Cell
1 
2BodyTag(I):
3

Tag Extension API

It implements IterationTag interface and provides default implementation for all its methods.

It acts as base class to develop simple and Iteration tags.

More or less this class acts as adapter class for Tag and IterationTag interfaces.

Example05
JCode Cell
1 
2TagSupport(C):
3

Tag Extension API

This class implements BodyTag interface and provides default implementation for all its methods.

It is the child class of TagSupport.

We can use this class as Base class for implementing Customtags that processes tag body.

6.BodyContent(C):

BodyContent object acts as a buffer to hold tag body.

It extends JspWriter.

We have to use BodyContent class only in BodyTag interface and BodyTagSupport class.

JspTag (I)

Tag (I)SimpleTag (I)
TagSupport (AC) IterationTag (I)SimpleTagSupport (AC)

BodyTagSupport (AC) BodyTag (I)

Classic Tag ModelSimple Tag Model
(JSP 1.1)(JSP 2.0)

JspTag Interface is just for Polymorphism purpose and doesn't contain any Methods.

Example06
JCode Cell
1 
2BodyTagSupport(C):
3

Implementing Tag(I)

Tag interface defines the following 4 constants

EVAL_BODY_INCLUDE

SKIP_BODY

EVAL_PAGE

SKIP_PAGE

Life Cycle of Tag Handler that implements Tag Interface

1.Whenever JSP engine encounters a custom tag in the JSP, it will identify the corresponding Tag

Handler class through tag lib directive and tld file.

2.Web container creates an instance of TagHandler by executing public no-arg constructor if it is

not already available.

3.JSP Engine calls setPageContext() method to make pageContext object available to Tag Handler

class.

public void setPageContext(PageContext p)

By using pageContext implicit object, Tag Handler class can get all other implicit objects and can

get attributes from various scopes.

4.JSP Engine calls setParent() method to make Parent Tag object available to Tag Handler. This is

helpful in nested tags.

public void setParent(Tag t)

5.Setting Attributes

attributes in custom tags are exactly similar to properties of Java beans. If a custom tag has an

attribute then compulsory Tag Handler class should contain instance variable and the

corresponding setter method.

JSP engine calls setter methods for each attribute.

Example07
JCode Cell
1 
2setPageContext()
3setParent()
4doStartTag()
5doEndTag()
6release()
7getParent()
8

Implementing Tag(I)

public int doStartTag() throws JspException

We can define entire tag functionality in this method only.

doStartTag() method can return either EVAL_BODY_INCLUDE or SKIP_BODY. If it returns

EVAL_BODY_INCLUDE then tag body will be included in the result.

If it returns SKIP_BODY then JSP Engine won't consider tag body.

7.JSP Engine calls doEndTag()

public int doEndTag()throws JspException

doEndTag() can return either EVAL_PAGE or SKIP_PAGE.

If it returns EVAL_PAGE then rest of the JSP will be executed normally.

If it returns SKIP_PAGE then JSP page will be returned without executing rest of the JSP.

  • Finally JSP Engine calls release() method to perform cleanup activities whenever tag handler

object no longer required.

public void release()

Flowchart for Tag Handler Life Cycle

JSP Engine encounters

a Custom Tag in JSP

JSP Engine will Identify

the corresponding

TagHandler Class by using

taglib Directive and tld File

JSP Engine will create

TagHandler Object if it is

not available

setPageContext()

setParent()

Setting Attributes

EVAL_BODY_INCLUDE

doStartTag()

Tag Body will be

included

SKIP_BODY

EVAL_PAGE doEndTag()SKIP_PAGE
Rest of JSP will beJSP will be returned without
executed normallyexecuting rest of JSP

Demo Program for Custom Tags(cust1)

test.jsp:

Example08
JCode Cell
1 
2JSP engine calls doStartTag()
3

Implementing Tag(I)

Example09
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<h1>Hello this is Demo JSP</h1>
4 
5<mine:mytag>
6<H1>This is body of the custom tag</H1>
7</mine:mytag>
8 
9<h1>This is after the custom tag invocation</h1>
10

MyTld.tld

Example10
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4 
5<tag>
6<name>mytag</name>
7<tag-class>tags.MyCustomTag</tag-class>
8</tag>
9 
10</taglib>
11

MyCustomTag.java

cust1

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

Output:

Hello this is Demo JSP

Hello this is from tag handler

This is body of the custom tag

This is after the custom tag invocation

If doStartTag() method resturns SKIP_BODY and doEndTag() method returns SKIP_PAGE then the

output is:

Hello this is Demo JSP

Hello this is from tag handler

How to map taglib directive with TLD file

  • By hard coding the location of tld file in the taglib directive

<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>

  • Instead of hard coding the location and name of tld file in jsp,we can specify through web.xml

also.

Example11
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import java.io.*;
6public class MyCustomTag implements Tag
7{
8private PageContext pageContext;
9public void setPageContext(PageContext pageContext)
10{
11this.pageContext = pageContext;
12}
13public void setParent(Tag t){ }
14public int doStartTag() throws JspException
15{
16try{
17JspWriter out = pageContext.getOut();
18out.println("<h1>Hello this is from tag handler</h1>");
19}catch(IOException e){}
20return EVAL_BODY_INCLUDE;
21}
22public int doEndTag() throws JspException
23{
24return EVAL_PAGE;
25}
26public void release(){}
27public Tag getParent()
28{
29return null;
30}
31}
32
Output

<h1>Hello this is from tag handler</h1>
      

MyCustomTag.java

  • We can map taglib directly to the tld file by using uri attribute.
Example12
JCode Cell
1 
2<web-app>
3<jsp-config>
4<taglib>
5<taglib-location>/WEB-INF/MyTld.tld</taglib-location>
6</taglib>
7</jsp-config>
8</web-app>
9

MyCustomTag.java

Q. In how many ways we can map taglib directive to tld file?

3 ways

Structure of TLD file:

Example13
JCode Cell
1 
2<%@ taglib prefix="mine" uri="www.durgajobs.com" %>
3 
4<taglib version="2.1" >
5<tlib-version>1.2</tlib-version>
6<uri>www.durgajobs.com</uri>
7<tag>
8<name>mytag</name>
9<tag-class>tags.MyCustomTag</tag-class>
10</tag>
11</taglib>
12

MyCustomTag.java

It describes the type of content allowed in tag body.

The allowed values are:

1.empty:

The body of the tag should be empty. We cannot take any tag body. In this case we can invoke

custom tag as follows..

<mine:mytag>OR <mine:mytag/>

</mine:mytag>

2.tagdependent:

Total tag body will be treated as plain text.

JSP engine sends tag body to the tag handler class without any processing.

3.scriptlet:

Tag body should not contain any scripting elements(i.e. scriptlet, expressions etc),But standard

actions and EL expressions are allowed.

4.jsp:

No restrictions on tag body. whatever allowed in JSP is by default allowed in tag body also.

The default value is jsp

Attributes:

A tag can contain any number of attributes. we can declare these attributes by using <attribute>

tag in tld.

<attribute> tag contains the following child tags.

1.<name> - Name of the attribute

2.<required>

true means attribute is mandatory

false means attribute is optional

default value is false

3.<rtexprvalue>

runtime expression value

true means runtime expressions are allowed

Eg: <mine:mytag color="${param.color}" />

false Runtime expressions are not allowed and we have to provide only literals.

Eg: <mine:mytag color="red" />

default value is false.

Things to remember about tag attributes:

A tag can contain attributes also. For each attribute we have to do the following things

1.We have to declare that attribute in tld by using <attribute> tag.

2.For each attribute in TagHandler class, we have to define one instance variable and

corresponding setter method.

3.In the case of optional attributes there may be a chance of NullPointerException. We have to

handle carefully.

Example14
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4 
5<uri>www.durgajobs.com</uri>
6 
7<tag>
8<description>This custom tag for weather report</description>
9<name>mytag</name>
10<tag-class>tags.MyCustomTag</tag-class>
11<body-content>xxx</body-content>
12<attribute>
13<name>
14<required>
15<rtexprevalue>
16</attribute>
17</tag>
18 
19</taglib><body-content>:
20

Demo Program for empty Custom Tag with mandatory attribute

Example15
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<mine:mytag number="5"/>
4<mine:mytag number="${param.num}"/>
5

MyTld.tld

Example16
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>mytag</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<body-content>empty</body-content>
8<attribute>
9<name>number</name>
10<required>true</required>
11<rtexprvalue>true</rtexprvalue>
12</attribute>
13</tag>
14</taglib>
15

MyCustomTag.java

cust2

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

Example17
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5public class MyCustomTag implements Tag
6{
7private int number;
8private PageContext pageContext;
9public void setPageContext(PageContext pageContext)
10{
11this.pageContext = pageContext;
12}
13public void setParent(Tag t)
14{
15}
16public void setNumber(int number)
17{
18this.number=number;
19}
20public int doStartTag() throws JspException
21{
22try{
23JspWriter out = pageContext.getOut();
24out.println("<h1>Double of "+number+" is :"+ (2*number)+"</h1>");
25}
26catch(java.io.IOException e){}
27return SKIP_BODY;
28}
29public int doEndTag() throws JspException
30{
31return EVAL_PAGE;
32}
33public void release()
34{
35}
36public Tag getParent()
37{
38return null;
39}
40}
41

Demo Program for empty Custom Tag with optional attribute

Example18
JCode Cell
1 
2<%@taglib prefix="mine1" uri="/WEB-INF/MyTld.tld" %>
3<mine1:greeting name="Durga" />
4<mine1:greeting/>
5

MyTld.tld

Example19
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>greeting</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<body-content>empty</body-content>
8<attribute>
9<name>name</name>
10<required>false</required>
11</attribute>
12</tag>
13</taglib>
14

MyCustomTag.java

cust3

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

IterationTag(I)

IterationTag is the child interface of Tag.

If we want to include tag body multiple times then we should go for IterationTag.

It contains only one extra method doAfterBody() and one extra constant EVAL_BODY_AGAIN.

Example20
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5public class MyCustomTag implements Tag
6{
7private PageContext pageContext;
8private String name;
9public void setPageContext(PageContext pageContext)
10{
11this.pageContext = pageContext;
12}
13public void setParent(Tag t)
14{
15}
16public void setName(String name)
17{
18this.name=name;
19}
20public int doStartTag() throws JspException
21{
22try
23{
24JspWriter out = pcontext.getOut();
25if(name == null)
26{
27out.println("<h1>Hello Guest..Good Morning...</h1>");
28}
29else
30{
31out.println("<h1>Good Morning..."+name+"</h1>");
32}
33}
34catch(java.io.IOException e){}
35return SKIP_BODY;
36}
37public int doEndTag() throws JspException
38{
39return EVAL_PAGE;
40}
41public void release()
42{
43}
44public Tag getParent()
45{
46return null;
47}
48}
49

Demo Program for IterationTag

Example21
JCode Cell
1 
2<%@taglib prefix="mine1" uri="/WEB-INF/MyTld.tld" %>
3<mine1:loop count="6">
4<h1>Learning custom tags is very easy...</h1>
5</mine1:loop>
6

MyTld.tld

Example22
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>loop</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<attribute>
8<name>count</name>
9<required>true</required>
10</attribute>
11</tag>
12</taglib>
13

MyCustomTag.java

cust4

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

TagSupport Class

We can develop Tag Handler class by implementing Tag and IterationTag interfaces directly. But

the problem in this approach is we have to provide implementation for all methods even though

most of the times our requirement is only doStartTag(), doAfterBody() and doEndTag() methods.

To overcome this problem we should go for TagSupport class. TagSupport class implements

IterationTag interface and provides default implementation for all its methods.

The main advantage of extending TagSupport class is we have to override only required methods

instead implementing all methods.

Internal implementation of TagSupport class:

Example23
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5public class MyCustomTag implements IterationTag
6{
7private int count;
8public void setCount(int count)
9{
10this.count = count;
11}
12public void setPageContext(PageContext p)
13{
14}
15public void setParent(Tag t)
16{
17}
18public int doStartTag() throws JspException
19{
20if(count >0)
21return EVAL_BODY_INCLUDE;
22else return SKIP_BODY;
23}
24public int doAfterBody() throws JspException
25{
26if(--count >0)
27return EVAL_BODY_AGAIN;
28else return SKIP_BODY;
29}
30 
31public int doEndTag() throws JspException
32{
33return EVAL_PAGE;
34}
35public void release()
36{
37}
38public Tag getParent()
39{
40return null;
41}
42}
43

MyCustomTag.java

Note:

  • The default return type of doStartTag() and doAfterBody() methods is SKIP_BODY.

2.The default return type of doEndTag() is EVAL_PAGE

  • pageContext variable is by default available to the child class. Hence we can use this variable

directly in our Tag Handler class.

Demo Program-1 by using TagSupport class:

test.jsp:

Example24
JCode Cell
1 
2public class TagSupport
3implements IterationTag, Serializable
4{
5 
6private Tag parent;
7protected transient PageContext pageContext;
8 
9public void setPageContext(PageContext pageContext)
10{
11this.pageContext = pageContext;
12}
13public void setParent(Tag t)
14{
15parent = t;
16}
17public int doStartTag()throws JspException
18{
19return SKIP_BODY;
20}
21public int doAfterBody() throws JspException
22{
23return SKIP_BODY;
24}
25public int doEndTag() throws JspException
26{
27return EVAL_PAGE;
28}
29public Tag getParent()
30{
31return parent;
32}
33public void release()
34{
35pageContext=null;
36parent=null;
37}
38..
39}
40

MyCustomTag.java

Example25
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<mine:loop count="7">
4<h1>Learning custom tags is very easy...</h1>
5</mine:loop>
6

MyTld.tld

Example26
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2.3.4</tlib-version>
4<tag>
5<name>loop</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<attribute>
8<name>count</name>
9<required>true</required>
10</attribute>
11</tag>
12</taglib>
13

MyCustomTag.java

cust5

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

Demo Program-2 by using TagSupport class:

test.jsp:

Example27
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5public class MyCustomTag extends TagSupport
6{
7private int count;
8public void setCount(int count)
9{
10this.count = count;
11}
12public int doStartTag() throws JspException
13{
14if(count >0)
15return EVAL_BODY_INCLUDE;
16else return SKIP_BODY;
17}
18public int doAfterBody() throws JspException
19{
20if(--count >0)
21return EVAL_BODY_AGAIN;
22else return SKIP_BODY;
23}
24}
25

MyCustomTag.java

Example28
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<mine:mytag/>
4<mine:mytag/>
5<mine:mytag/>
6<mine:mytag/>
7

MyTld.tld

Example29
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>mytag</name>
6<tag-class>tags.MyCustomTag</tag-class>
7</tag>
8</taglib>
9

MyCustomTag.java

cust5A

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

BodyTag(I)

It is the child interface of IterationTag.

If we want to manipulate Tag Body then we should go for BodyTag interface.

BodyTag interface defines the following 2 extra methods.

Example30
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import java.io.*;
6public class MyCustomTag extends TagSupport
7{
8public int doStartTag() throws JspException
9{
10try{
11JspWriter out = pageContext.getOut();
12out.println("<h1>Hello this is from tag handler</h1>");
13}catch(IOException e){}
14return EVAL_BODY_INCLUDE;
15}
16}
17
Output

<h1>Hello this is from tag handler</h1>
      

MyCustomTag.java

BodyTag interface defines the following 2 extra constants

1.EVAL_BODY_BUFFERED

2.EVAL_BODY_TAG==>Deprecated in JSP 1.2V

Example31
JCode Cell
1 
2public void setBodyContent(BodyContent b)
3public void doInitBody() throws JspException
4

BodyContent

returns body content as String

Example32
JCode Cell
1 
2public String getString()
3

BodyContent

returns Reader object to read tag body

Example33
JCode Cell
1 
2public Reader getReader()
3

BodyContent

Returns JspWriter object of Parent class

If there is no parent class then it returns current jsp out object.

Example34
JCode Cell
1 
2public JspWriter getEnclosingWriter()
3

BodyContent

It clears bodyContent object. i.e data present in bodyContent object will be removed.

Life cycle of Tag Handler that implements BodyTag:

The life cycle of BodyTag Handler is exactly similar to IterationTag Hanlder. The difference is

doStartTag() method can return EVAL_BODY_BUFFERED in addition to EVAL_BODY_INCLUDE and

SKIP_BODY.

If the method returns EVAL_BODY_INCLUDE then body will be evaluated and included exactly

similar to IterationTag.

If the method returns EVAL_BODY_BUFFERED and tag contains body then JSP engine creates

BodyContent object and call setBodyContent() method followed by doInitBody() method.

Note:

setBodyContent() and doInitBody() method won't be executed in the following case:

If doStartTag() returns either EVAL_BODY_INCLUDE or SKIP_BODY or if the tag does not contain

body.

Flow Chart for BodyTag Life Cycle

JSP Engine encounters

a Custom Tag in JSP

JSP Engine will Identify the

corresponding TagHandler

Class by using taglib Directive

and tld File

JSP Engine will create

TagHandler Object if it is

not available

setPageContext()

setParent()

Setting Attributes

EVAL_BODY_INCLUDEdoStartTag() EVAL_BODY_BUFFERED

Evaluate Body SKIP_BODY No If Tag

SKIP_BODYSKIP_BODYhas Body

doAfterTag()

EVAL_BODY_Yes
AGAINsetBodyContent()
EVAL_PAGEdoInitBody()

doEndTag()

Manipulate Tag

Rest of JSP will beSKIP_PAGEBody and include in
executed normallyrest of JSP
JSP will be returneddoAfterBody()
without executing restEVAL_BODY_

of JSP

AGAIN

BodyTagSupport Class

This class extends TagSupport class and implements BodyTag interface.

This class provides default implementation for all 9 methods available in BodyTag interface.

It is very easy to develop tag handler by extending BodyTagSupport class instead of implementing

BodyTag interface directly. In this case we have to provide implementation only for required

methods instead of implementing all 9 methods.

Internal implementation of BodyTagSupport class:

Example35
JCode Cell
1 
2public void clearBody()
3

BodyContent

Note:

1.pageContext and bodyContent variables are by default available to our Tag handler class and

hence we can use these directly.

  • The default return type of

doStartTag() is EVAL_BODY_BUFFERED,

doAfterBody() is SKIP_BODY ,

doEndTag() is EVAL_PAGE.

Example36
JCode Cell
1 
2public class BodyTagSupport extends TagSupport implements BodyTag
3{
4protected transient BodyContent bodyContent;
5protected transient PageContext pageContext;
6private Tag parent;
7public void setPageContext(PageContext pageContext)
8{
9this.pageContext = pageContext;
10}
11public void setParent(Tag t)
12{
13parent = t;
14}
15public int doStartTag() throws JspException
16{
17return EVAL_BODY_BUFFERED;
18}
19public void setBodyContent(BodyContent b)
20{
21bodyContent = b;
22}
23public void doInitBody() throws JspException
24{
25}
26public int doAfterBody() throws JspException
27{
28return SKIP_BODY;
29}
30public int doEndTag() throws JspException
31{
32return EVAL_PAGE;
33}
34public BodyContent getBodyContent()
35{
36return bodyContent;
37}
38....
39}
40

Demo Program for BodyTag and BodyTagSupport

Example37
JCode Cell
1 
2<%@taglib prefix="mine1" uri="/WEB-INF/MyTld.tld" %>
3<mine1:body >
4<h1>Learning custom tags is very easy...</h1>
5</mine1:body>
6<mine1:body >
7<h1>Durga Software Solutions....</h1>
8</mine1:body>
9<mine1:body >
10<h1>Ameerpet, Hyderabad</h1>
11</mine1:body>
12

MyTld.tld

Example38
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>body</name>
6<tag-class>tags.MyCustomTag</tag-class>
7</tag>
8</taglib>
9

MyCustomTag.java

cust6

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

Comparison b/w TagSupport and BodyTagSupport Classes

MethodTag SupportBodyTagSupport
1) doStartTag()EVAL_BODY_INCLUDE, EVAL_BODY_INCLUDE,
1. Possible Return Values SKIP_BODYSKIP_BODY,

EVAL_BODY_BUFFERED

  • Default Value from SKIP_BODY EVAL_BODY_BUFFERED

Implementation Class

  • Number of times it can Only Once Only Once

be called per Tag

EVAL_BODY_AGAIN,

Example39
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5public class MyCustomTag extends BodyTagSupport
6{
7public int doAfterBody() throws JspException
8{
9try
10{
11String s = bodyContent.getString();
12s = s.toLowerCase();
13JspWriter out = bodyContent.getEnclosingWriter();
14out.println(s);
15}
16catch(Exception e){}
17return SKIP_BODY;
18}
19}
20

MyCustomTag.java

  • Possible Return Values SKIP_BODY SKIP_BODY
  • Default Value from SKIP_BODY

Implementation Class

  • Number of times it can Zero OR More Zero OR More

be called per Tag

Example40
JCode Cell
1 
2doAfterBody() EVAL_BODY_AGAIN, SKIP_BODY
3

MyCustomTag.java

  • Possible Return Values EVAL_PAGE, SKIP_PAGE EVAL_PAGE, SKIP_PAGE
  • Default Value from EVAL_PAGE EVAL_PAGE

Implementation Class

  • Number of times it can Only Once Only Once

be called per Tag

Executed only once iff

Example41
JCode Cell
1 
2doEndTag()
3

MyCustomTag.java

doInitBody()EVAL_BODY_BUFFERED

and Tag contains Body

Circumstances under which

these Methods can be called

and Number of times per Tag

Invocation

Co-operative OR Nested tags:

Sometimes a group of tags work together to perform required functionality. Such type of tags are

called co-operative or nested tags.

Eg:

In JSTL <c:choose>,<c:when> and <c:otherwise> tags work together to implement java's switch

statement.

These tags are called co-operative tags.

Eg:

<mine:outerTag>

<mine:innerTag/>

</mine:outerTag>

From Inner Tag(Child Tag) we can get Parent Tag Referece by using getParent() method.

Tag parent=getParent();

Example42
JCode Cell
1 
2setBodyContent() Not Applicable doStartTag() returns
3

Demo Program1 for Nested Tags

Example43
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<mine:mytag>
4<mine:mytag>
5<mine:mytag>
6<mine:mytag/>
7</mine:mytag>
8</mine:mytag>
9</mine:mytag>
10

MyTld.tld

Example44
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>mytag</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<body-content>JSP</body-content>
8</tag>
9</taglib>
10

MyCustomTag.java

cust7

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

Example45
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5public class MyCustomTag extends TagSupport
6{
7public int doStartTag() throws JspException
8{
9int level = 0;
10Tag t = getParent();
11while( t != null)
12{
13level++;
14t = t.getParent();
15}
16try{
17JspWriter out = pageContext.getOut();
18out.println("<h1>Nested level is :"+level+"</h1>");
19}
20catch(java.io.IOException e){}
21return EVAL_BODY_INCLUDE;
22}
23}
24

Demo Program2 for Nested Tags

Example46
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<mine:menu>
4<mine:menuitem item="chicken65"/>
5<mine:menuitem item="Mutton"/>
6<mine:menuitem item="Fish"/>
7</mine:menu >
8 
9<mine:menu>
10<mine:menuitem item="CoreJava"/>
11<mine:menuitem item="AdvJava"/>
12<mine:menuitem item="Oracle"/>
13<mine:menuitem item="Spring"/>
14<mine:menuitem item="Hibernate"/>
15<mine:menuitem item="WebServices"/>
16<mine:menuitem item="DP"/>
17<mine:menuitem item="RT"/>
18</mine:menu >
19

MyTld.tld

Example47
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4 
5<tag>
6<name>menu</name>
7<tag-class>tags.MenuTag</tag-class>
8<body-content>JSP</body-content>
9</tag>
10 
11<tag>
12<name>menuitem</name>
13<tag-class>tags.MenuItemTag</tag-class>
14<attribute>
15<name>item</name>
16<required>true</required>
17</attribute>
18</tag>
19 
20</taglib>
21

MenuTag.java

Example48
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import java.util.*;
6public class MenuTag extends TagSupport
7{
8private ArrayList l = null;
9public int doStartTag() throws JspException
10{
11l = new ArrayList();
12return EVAL_BODY_INCLUDE;
13}
14public void addMenuItem(String s)
15{
16l.add(s);
17}
18public int doEndTag() throws JspException
19{
20try
21{
22JspWriter out = pageContext.getOut();
23out.println("<h1><br>Menu Items are :" +l+"</h1>");
24}
25catch(Exception e ) {}
26 
27return EVAL_PAGE;
28}
29}
30

MenuItemTag.java

cust8

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MenuTag.class

|-MenuItemTag.class

Getting an arbitrary Ancestor class:

We can get immediate parent by using getParent() method.

TagSupport class contains the following method to get an arbitrary ancestor class.

public static Tag findAncestorWithClass(Tag t,Class c)

Example49
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import java.util.*;
6public class MenuItemTag extends TagSupport
7{
8private String item;
9public void setItem(String item)
10{
11this.item = item;
12}
13public int doStartTag() throws JspException
14{
15MenuTag parent = (MenuTag)getParent();
16parent.addMenuItem(item);
17return SKIP_BODY;
18}
19}
20

Accessing JSP Implicit Objects in Tag Handler Class

Note:

Exception implicit object is available only in error pages. If the enclosing JSP is not error page then

getException() returns null

Example50
JCode Cell
1 
2request getRequest()
3response getResponse()
4config getServletConfig()
5application getServletContext()
6session() getSession()
7out getOut()
8page getPage()
9exception getException()
10

Demo Program for JSP Implicit objects

Example51
JCode Cell
1 
2<%@taglib prefix="mine1" uri="/WEB-INF/MyTld.tld" %>
3<%@ page isErrorPage="true" %>
4<mine1:myTag/>test1.jsp:
5<%@ page errorPage="test.jsp" %>
6<% System.out.println(10/0); %>
7

MyTld.tld

Example52
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4 
5<tag>
6<name>myTag</name>
7<tag-class>tags.MyCustomTag</tag-class>
8<body-content>JSP</body-content>
9</tag>
10</taglib>
11

MyCustomTag.java

cust9

|-test.jsp

|-test1.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

http://localhost:7777/cust9/test1.jsp

http://localhost:7777/cust9/test.jsp

Getting and Setting Attributes by using PageContext API

PageContext class defines the following methods to perform attribute management.

Example53
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import javax.servlet.*;
6import javax.servlet.http.*;
7public class MyCustomTag extends TagSupport
8{
9public int doStartTag() throws JspException
10{
11ServletRequest req = pageContext.getRequest();
12String s1 = req.getServerName()+":"+req.getServerPort();
13ServletResponse resp = pageContext.getResponse();
14String s2 = resp.getContentType();
15HttpSession session = pageContext.getSession();
16String s3 = session.getId();
17Throwable e = pageContext.getException();
18try{
19JspWriter out = pageContext.getOut();
20out.println("<h1>"+s1);
21out.println(s2);
22out.println(s3);
23out.println(e+"</h1>");
24}
25catch(Exception e1){}
26return EVAL_BODY_INCLUDE;
27}
28}
29

MyCustomTag.java

3.public Object getAttribute(String name)

4.public Object getAttribute(String name,int scope)

Example54
JCode Cell
1 
2public void setAttribute(String name,Object value)
3public void setAttribute(String name,Object value,int scope)
4

MyCustomTag.java

SimpleTag Model

Implementing custom tags by using classic tag model (Tag, IterationTag, BodyTag, TagSupport,

BodyTagSupport)is very complex because each tag has its own life cycle and different possible

return types for each method.

To resolve this complexity, Sun people introduced Simple Tag Model in JSP 2.0V

In this model, we can build custom tags by using SimpleTag interface and its implementation class

SimpleTagSupport.

JspTag (I)

Tag (I) SimpleTag (I)

TagSupport (AC) IterationTag (I) SimpleTagSupport (AC)

BodyTagSupport (AC) BodyTag (I)Simple Tag Model
Classic Tag Model(JSP 2.0)

(JSP 1.1)

Example55
JCode Cell
1 
2public void removeAttribute(String name)
3public void removeAttribute(String name,int scope)
4public Object findAttribute(String name)
5public Enumeration getAttributeNamesInScope(int scope)
6

SimpleTag(I)

Example56
JCode Cell
1 
2public void setJspContext(JspContext c)
3public void setParent(JspTag t)
4public void setJspBody(JspFragment f)
5public void doTag()throws JspException,IOException
6public JspTag getParent()
7

Life Cycle of SimpleTag Handler

Example57
JCode Cell
1 
2public JspContext getJspContext()
3public JspFragment getJspBody()
4public JspTag findAncestorWithClass(JspTag t,Class c)
5

Demo Program for SimpleTagSupport

Example58
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<h1>This is Simple Tag Demo<br>
4<mine:mytag/>
5<h1>This is rest of the JSP</h1>
6

MyTld.tld

Example59
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>mytag</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<body-content>tagdependent</body-content>
8</tag>
9</taglib>
10

MyCustomTag.java

cust10

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

If doTag() method throws SkipPageException then the output is:

This is Simple Tag Demo

Hello this is from simple tag handler

If doTag() method does not throw SkipPageException then the output is:

This is Simple Tag Demo

Hello this is from simple tag handler

This is rest of the JSP

Q. What is the difference b/w classic and simple tags wrt tag body?

In classic tag model we are allowed to take scripting elements in tag body. Hence the possible

values for <body-content> tag are : empty, tagdependent, scriptless, jsp.

default value is JSP.

But in Simple Tag Model Scripting elements are not allowed in tag body. Hence allowed values for

<body-content> are empty, tagdepdendent, scriptless but not jsp.

Default value is scriptless

Example60
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import java.io.*;
6public class MyCustomTag extends SimpleTagSupport
7{
8public void doTag() throws JspException,IOException
9{
10JspWriter out = getJspContext().getOut();
11out.println("<h1>Hello this is from simple tag handler</h1>");
12//throw new SkipPageException();
13}
14}
15
Output

<h1>Hello this is from simple tag handler</h1>
      

Processing Body Content in Simple Tags

It causes evaluation of TagBody and return to supplied writer.

use "null" argument to write directly to the JspOutputStream.

Example61
JCode Cell
1 
2public JspContext getJspContext()
3public void invoke(Writer w);
4

Demo Program for SimpleTag to process Tag Body

Example62
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<h1> This is Before tag invocation<br>
4 
5<mine:mytag>
6<h1>This is Tag Body<br>
7</mine:mytag>
8 
9<h1>This is After tag invocation</h1>
10

MyTld.tld

Example63
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>mytag</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<body-content>tagdependent</body-content>
8</tag>
9</taglib>
10

MyCustomTag.java

cust12

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

Key Differences b/w Simple and Classic Tags

PropertySimple TagsClassic Tags
1) Tag InterfacesTag
Simple TagIteration Tag

Body Tag

Example64
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import java.io.*;
6public class MyCustomTag extends SimpleTagSupport
7{
8public void doTag() throws JspException,IOException
9{
10JspWriter out = getJspContext().getOut();
11out.println("<h1>Hello this is from simple tag handler</h1>");
12getJspBody().invoke(null);
13//throw new SkipPageException();
14}
15}
16
Output

<h1>Hello this is from simple tag handler</h1>
      

MyCustomTag.java

Imlementation ClassesBodyTagSupport
3) Key Life Cycle MethodsdoTag()doStartTag()
that we have to implementdoEndTag()

doAfterBody()

Example65
JCode Cell
1 
2Supporting SimpleTagSupport TagSupport
3

MyCustomTag.java

JspOutputStreamNo need to use try - catch forCompulsory we should enclose
IoExceptionby using try - catch for

IoException

Example66
JCode Cell
1 
2How to write Response to getJspContext().getOut().println(); pageContext.getOut().println();
3

MyCustomTag.java

Objects and Attributes

Example67
JCode Cell
1 
2How to access JSP Implicit By using JspContext Object By using PageContext Object
3

MyCustomTag.java

be processedfrom doStartTag()

getJspBody().invoke(null) OR

EVAL_BODY_BUFFERED in

Body Tag Interface from

doStartTag()

Example68
JCode Cell
1 
2How to cause the Body to Returns EVAL_BODY_INCLUDE
3

MyCustomTag.java

doEndTag()

Page Evaluation to stopdoTag()

Dynamic Attributes

In general tags can contain attributes. We can declare these attributes in tld file by using

<attribute> tag. In the tag handler class we have to maintain instance variables and corresponding

setter methods. Such type of attributes are called static attributes.

But we can use attributes, even though TLD file does not contain any <attribute> tag declarations.

Such type of attributes are called Dynamic Attributes.

Dynamic attributes are applicable for both classic and simple tags and introduced in Jsp

2.0version.

Example69
JCode Cell
1 
2How to cause the Current throw SkipPageException from Return SKIP_PAGE from
3

To Support dynamic attributes we have to do the following arrangements

" party="JanaSena" address="hyd" />

Example70
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3 
4<mine:mytag name="pawan" age="45" wife1="Nandita" wife2="Renu" wife3="Anna Lezi
5

MyTld.tld

Example71
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>mytag</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<body-content>tagdependent</body-content>
8<dynamic-attributes>true</dynamic-attributes>
9</tag>
10</taglib>
11

MyCustomTag.java

cust11D

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

Demo Program-2 for Dynamic Attributes:

test.jsp:

Example72
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import java.io.*;
6import java.util.*;
7public class MyCustomTag extends SimpleTagSupport implements DynamicAttributes
8{
9HashMap h = new HashMap();
10public void setDynamicAttribute(String ns, String name, Object value)
11{
12h.put(name,value);
13}
14public void doTag() throws JspException,IOException
15{
16JspWriter out = getJspContext().getOut();
17out.println("<h1>"+h+"</h1>");
18}
19}
20

MyCustomTag.java

Example73
JCode Cell
1 
2<%@taglib prefix="mine" uri="/WEB-INF/MyTld.tld" %>
3<h1>Hello this is Sample JSP</h1>
4<mine:mytag num="2" min="10" max="20" pow="2" />
5

MyTld.tld

Example74
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4<tag>
5<name>mytag</name>
6<tag-class>tags.MyCustomTag</tag-class>
7<attribute>
8<name>num</name>
9<required>true</required>
10</attribute>
11<dynamic-attributes>true</dynamic-attributes>
12<body-content>empty</body-content>
13</tag>
14</taglib>
15

MyCustomTag.java

cust11

|-test.jsp

|-WEB-INF

|-MyTld.tld

|-classes

|-tags

|-MyCustomTag.class

Tag Files

Objective:

  • Describe the semantics of tag file
  • Describe application structure of Tag Files
  • Write a Tag File and Explain constraints on JspContent in body of tag

Tag Files concept introduced in JSP 2.0V.

Tag File is a simple jsp page or jsp document designed to be used as Custom Tag.

The main advantage of Tag Files is we can build very easily when compared with classic and simple

tags.

The main limitation of tag files is it won't suggestible for doing much processing.

Building and using a Simple Tag File:

  • Write a JSP Page or Document and save it with .tag extension.

2.Place this .tag file in /WEB-INF/tags folder.

  • put a taglib directive in the jsp with "tagdir" attribute.

<%@ taglib prefix="mine" tagdir="/WEB-INF/tags" %>

Example75
JCode Cell
1 
2package tags;
3import javax.servlet.jsp.*;
4import javax.servlet.jsp.tagext.*;
5import java.io.*;
6public class MyCustomTag extends SimpleTagSupport implements DynamicAttributes
7{
8double num;
9String output = "";
10public void setNum(double num)
11{
12this.num = num;
13}
14public void setDynamicAttribute(String ns, String name, Object value)
15{
16double d = Double.parseDouble((String)value);
17if(name == "min")
18{
19output= output + "The Minimum value is :"+Math.min(num,d)+"<br>";
20}
21else if(name == "max")
22{
23output= output + "The Maximum value is :"+Math.max(num,d)+"<br>";
24}
25else if(name == "pow")
26{
27output= output + "The Power value is :"+Math.pow(num,d)+"<br>";
28 
29}
30}
31public void doTag() throws JspException,IOException
32{
33JspWriter out = getJspContext().getOut();
34out.println("<h1>"+output+"</h1>");
35}
36}
37

Demo Program

<h1> Hello....This is from the tag file <br>

tagFileA

|-test.jsp

|-WEB-INF

|-tags

|-mytag.tag

Note: The name of custom tag and tag file should be matched.

Note:

  • Tag Files are internally converted into Simple Tag Handlers and corresponding classes are

available in work folder.

  • It is not required to write TLD File.

Declaring a Tag file with attribute:

We can define attributes for tag files by using attribute directive.

<%@ attribute name="title" required="true" rtexprvalue="true" %>

Example76
JCode Cell
1 
2<%@taglib prefix="mine" tagdir="/WEB-INF/tags" %>
3<h1>This is Tag File Demo Example</h1>
4<mine:mytag/>
5<mine:mytag/>mytag.tag:
6

Demo Program

|-test.jsp

|-WEB-INF

|-tags

|-mytag.tag

Example77
JCode Cell
1 
2<%@taglib prefix="mine" tagdir="/WEB-INF/tags" %>
3<h1> Tag File with Attributes Example</h1>
4<mine:mytag title="kabali"/>mytag.tag:
5<%@ attribute name="title" required="true" rtexprvalue="true" %>
6<h1> Hello....${title} is big flop but business is good 600Cr..</h1>tagFileB
7

Demo Program

|-test.jsp

|-WEB-INF

|-tags

|-mytag.tag

Where web container will search for Tag Files:

webapps1

WEB-INF

tags

MyTags

lib

MyJar.jar

META-INF

MyTld.tld

tags

MyTags

  • Either directly or indirectly in WEB-INF/tags folder
  • Either directly or indirectly in META-INF/tags folder present in jar file of WEB-INF/lib folder

Note:

Whenever we are deploying tag file in some jar then compulsory we have to write tld file.

Example78
JCode Cell
1 
2<%@taglib prefix="mine" tagdir="/WEB-INF/tags" %>
3 
4<mine:header color="red">
5This is Body of tag file
6</mine:header>header.tag:
7<%@ attribute name="color" required="true" rtexprvalue="true" %>
8<%@ tag body-content="tagdependent" %>
9<h1> Hello....This is from the tag file<br>
10 
11<font color="${color}" > <jsp:doBody/></font> </h1>tagFiles1
12

Demo Program

Example79
JCode Cell
1 
2<mine:mytagfile/>
3<mine:mytagfile/>
4

MyTld.tld

<h1> Hello....This is from the tag file deployed in jar file <br>

First we have to Create JAR FileOriginal Folder Structure
durgatagfileJarDemo
META-INFtest.jsp
MyTld.tldWEB-INF

tags

MyTags.taglib

durga.jar

Example80
JCode Cell
1 
2<taglib version="2.1" >
3<tlib-version>1.2</tlib-version>
4 
5<tag-file>
6<name>mytagfile</name>
7<path>/META-INF/tags/mytag.tag</path>
8</tag-file>
9 
10</taglib>mytag.tag:
11

MyTld.tld

MyTags.tag

Example81
JCode Cell
1 
2Create JAR File for durga Folder. META-INF
3jar -cvf durga.jar MyTld.tld
4Place that JAR Folder in lib. tags
5

Disabling Scripting Language

Once scripting language is invalidated, we are not allowed to use scripting elements in JSP,

otherwise we will get translation time error.(Tomcat won't provide support for this feature)

Similarly we can disable expression language globally.

Example82
JCode Cell
1 
2<web-app>
3<jsp-config>
4<jsp-property-group>
5<url-pattern>*.jsp</u-p>
6<scripting-invalid>true</s-i>
7<jsp-property-group>
8</jsp-config>
9</web-app>
10

Disabling Scripting Language

Important 3

JSP FAQ's

Example83
JCode Cell
1 
2<web-app>
3<jsp-config>
4<jsp-property-group>
5<url-pattern>*.jsp</u-p>
6<el-ignored>true</e-i>
7</jsp-property-group>
8</jsp-config>
9</web-app>Top Most
10

Disabling Scripting Language

Differences b/w Include Directive and Include Action

Include DirectiveInclude Action
1) <%@ include file = "second.jsp" %>1) <jsp:include page = "second.jsp"
Contains only one Attribute File.flush = "true"/>

Contains 2 Attributes Page and File.

Example84
JCode Cell
1 
2Differences b/w Static Include and Dynamic IncludeOR
3

Disabling Scripting Language

Translation Time. Hence it is also considered asRuntime. Hence it is also considered as
Static Include.Dynamic Include.
Example85
JCode Cell
1 
2The Content of Target JSP will be included at 2) The Response Target JSP will be included at
3

Disabling Scripting Language

Servlet will be generated. Hence Code sharingseparate Servlets will be generated. Hence Code
between the Components is possible.sharing between the Components is not

possible.

Example86
JCode Cell
1 
2For both including and included JSPs, a Single 3) For both including and included JSPs,
3

Disabling Scripting Language

latest Version included JSP. It is Vendorbe included.

Dependent.

Example87
JCode Cell
1 
2Relatively Performance is High. 4) Relatively Performance is Low.
3There is no Guarantee for Inclusion of 5) Always latest Version of included Page will
4

Disabling Scripting Language

frequently then it is recommended to usefrequently then it is recommended to use
Static Include.Dynamic Include.
Example88
JCode Cell
1 
2If the Target Resource won't change 6) If the Target Resource will change
3

Disabling Scripting Language

When compared with Servlet Programming, Developing JSPs is very easy because the required

mandatory stuff automatically available in every JSP. Implicit objects also one such area ,which are

by default available for every JSP.

The following is the list of all possible 9 JSP implicit objects.

Example89
JCode Cell
1 
2Explain about JSP Implicit Objects?
3

Disabling Scripting Language

Example90
JCode Cell
1 
2request HttpServletRequest(I)
3response HttpServletResponse(I)
4config ServletConfig(I)
5application ServletContext(I)
6session HttpSession(I)
7out javax.serlvet.jsp.JspWriter(AC)
8page java.lang.Object(CC)
9pageContext javax.servlet.jsp.PageContext(AC)
10exception java.lang.Throwable(CC)
11
📝 Key Takeaways
  • Classic model uses doStartTag/doEndTag; simple model uses doTag
  • Handlers reach JSP data via pageContext
  • Tag files are JSP-like files with a tag directive