CS320 - Web and Internet Programming

infestationwatchSoftware and s/w Development

Oct 28, 2013 (3 years and 7 months ago)

65 views

CS320
-

Web and Internet Programming

Notes Assignment

Joe Lieman

Wednesday (08/13/03)

------------------------------------------------------------------------------------------------------------


Project 4 is due on Sunday before midnight (8
-
17
-
2003).

JSP

Tag Libraries



The ability to create tag libraries is a recent addition to the JavaServer Page (JSP)
specification that lets you create reusable tags that resemble XML/HTML.



JSP
tag libraries (
taglibs
)

let a JSP programmer encapsulate Java code normally u
sed
multiple times withi
n a JSP file into a reusable tag
.

With tag libraries, you can
define
your own

custom tag
s

and then
substitute it in any code you want
.

HTML
Tag
, Scriplet


VS


JSP T
ag Library

<HTML>,
<% …

%>



<jsp:useBean

/>



Finite, fixed,
b
uild in
, etc



Tag that is defined in tags library



Example:

< jsp : useBean
id = “ t ”







scope = “ s “







class = “ c “ / >




What happen in bold is t get instantiated. The conversion in Java code is



probably like:

Text t = new Test( );



We need 3 files in order to create a JSP tag. A .jsp file that contains JSP codes, a .java
class file, and a .tld file.


Creating our first

JSP tag

1.
Implement the JSP tag in a
tag handler class (
Jav
a class
)
.

When you use a custom
tag in your JSP,
Tomcat reads this file and

executes the functionality of the tag. A tag
handler class implements one of two interfaces: javax.servlet.jsp.tagtext.BodyTag or
javax.servlet.jsp.tagtext.Tag. Your tag handler cl
ass is implemented as part of a tag
library.


-

Create ExampleTag.java in directory
../WEB
-
INF/classes/your_username
.

Here is the ExampleTag.java looks like:



package jlieman2;


import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.
io.*;


public class ExampleTag extends TagSupport {





private String name;





public int doStartTag(){





try {







JspWriter out = pageContext.getOut();







out.print("Custom
tag example"); // + name);






} catch(IOException ioe){







System.out.println("Error in example tag: " + ioe.getMessage());






}





return (SKIP_BODY);





}



}



Compile ExampleTag.java using command:

javac
-
classpath /jakarta
-
tomcat
-
4.1.18/common
/lib/servlet.jar ExampleTag.java


2.
Write the Tag Library Descriptor (TLD).
The TLD defines the tag library and
provides additional information about each tag, such as the name of the tag handler class,
attributes, and other information about the tags.


-

Create this file named example.tld and save it in your home directory.

<?xml version="1.0" encoding="ISO
-
8859
-
1" ?>

<!DOCTYPE taglib PUBLIC "
-
//Sun Microsystems. Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web
-
jsptaglibrary_1_1.dtd
">


<taglib>


<tlibversion>1.0</tlibversion>


<jspversion>1.1</jspversion>


<shortname>example
-
tags</shortname>


<info>A sample tag library</info>


<tag>


<name>example</name>


<tagclass>jlieman2.ExampleTag</tagclass>


<bodycontent>e
mpty</bod
ycontent>


<info>Simple example</info>


</tag>

</taglib>



3.

Reference the tag library in your JSP source

using the JSP <taglib> directive. A tag
library is a collection of JSP tags. Include this directive at the top of your JSP source.
Then
Use yo
ur custom tag in your JSP
.

<%@ page language="java" %>

<%@ taglib uri="example.tld" prefix="exam" %>


<HTML>

<BODY>


<H1
><exam:example /></H1>

</BODY>

</HTML>


JSP tab libraries are defined in a tag library descriptor (tld). To use a custom tag library

from a JSP page, reference its tag library descriptor with a
<%@ taglib %>

directive. For
example:

<%@ taglib uri="myTLD" prefix="mytaglib" %>



uri


specify the location of tld file.


prefix


The prefix attribute assigns a label to the tag librar
y.



4.

Restart Tomcat.

5.

Try it in your browser by visiting
http://cs.calstatela.edu:8080/your_username/taglib.jsp





Passing Parameter to Servlet

Modify your files so they will l
ook like these:

ExampleTag.java:

package jlieman2;


import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.io.*;


public class ExampleTag extends TagSupport {


private String name;


public int doStartTag(){


try {


JspWriter o
ut = pageContext.getOut();


out.print("Custom
tag example " + name);


} catch(IOException ioe){


System.out.println("Error in example tag: " + ioe.getMessage());


}


return (SKIP_BODY);


}



/*
Manipulator method to set the name*/


pu
blic void setName(String name){


this.name = name;


}

}


example.tld:

<?xml version="1.0" encoding="ISO
-
8859
-
1" ?>

<!DOCTYPE taglib PUBLIC "
-
//Sun Microsystems. Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web
-
jsptaglibrary_1_1.dtd
">


<taglib>


<tlibversion>1.0</tlibversion>


<jspversion>1.1</jspversion>


<shortname>example
-
tags</shortname>


<info>A sample tag library</info>


<tag>


<name>example</name>


<tagclass>jlieman2.ExampleTag</tagclass>

<bodycontent>e
mpty</bodycon
tent>


<info>Simple example</info>


<attribute>



<name>name</name>


<required>false</required>


</attribute>


</tag>

</taglib>


taglib.jsp:

<%@ page language="java" %>

<%@ taglib uri="example.tld" prefix="exam" %>


<HTML>

<BODY>


<H1><exam:example name="
Joe
" /></
H1>

</BODY>

</HTML>


More example:

Modify your files so they will look like these:

ExampleTag.java:

package jlieman2;


import javax.servlet.jsp.*;

import javax.servlet.jsp.tagext.*;

import java.io.*;


publi
c class ExampleTag extends TagSupport {


private String name;


public int doStartTag(){


try {


JspWriter out = pageContext.getOut();


out.print("<font size=
\
"5
\
" color=
\
"blue
\
">");


} catch(IOException ioe){


System.out.println("Err
or in example tag: " + ioe.getMessage());


}


r
eturn (EVAL_BODY_INCLUDE);


}



public int doEndTag(){


try {


JspWriter out = pageContext.getOut();


out.print("</font>");


} catch(IOException ioe){


System.out.println("Error i
n end tag: " + ioe.getMessage());


}


return(EVAL_PAGE);


}



public void setName(String name){


this.name = name;


}

}


example.tld:

<?xml version="1.0" encoding="ISO
-
8859
-
1" ?>

<!DOCTYPE taglib PUBLIC "
-
//Sun Microsystems. Inc.//DTD JSP Tag L
ibrary 1.1//EN"
"http://java.sun.com/j2ee/dtds/web
-
jsptaglibrary_1_1.dtd">


<taglib>


<tlibversion>1.0</tlibversion>


<jspversion>1.1</jspversion>


<shortname>example
-
tags</shortname>


<info>A sample tag library</info>


<tag>


<name>example</name>


<tagclass>jlieman2.ExampleTag</tagclass>


<bodycontent>JSP</bodycontent>


<info>Simple example</info>


<at
tribute>



<name>name</name>


<required>false</required>


</attribute>


</tag>

</taglib>



taglib.jsp:

<%@ page language="java" %>

<%@ taglib uri="example.tld" prefix="exam" %>


<HTML>

<BODY>


<exam:example name="Joe
">Here is my sample text</exam:example>

</BODY>

</HTML>