Documente Academic
Documente Profesional
Documente Cultură
Lecture 38
There is also another problem attached to it. Generally web page designers which have
enough knowledge to work with HTML and some scripting language, faced lot of
difficulties in writing some simple lines of java code. To overcome these issues, java
provides us the mechanism of custom tags.
Motivation
To give you an inspiration, first have a glance over the code snippet we used in JSP of the
course outline example of last lecture. Of course, not all code is given here; it’s just for
your reference to give you a hint.
<%
CourseDAO courseDAO = new CourseDAO();
………………
// iterating over ArrayList
for (…………………… ) {
……………………
……………………
// displaying courseoutline
}
………………
%>
Can we replace all the above code with one single line? Yes, by using custom tag we can
write like this:
By only specifying the course/page name, this tag will display the course outline in
tabular format. Now, you must have realized how significant changes custom tags can
bring on.
- 475-
Handout 38
Web Design & Development CS-506
Types of Tags
1. Simple Tag
2. Tag with Attribute
3. Tag with Body
- 476 -
Handout 38
Web Design & Development CS-506
1. Simple Tag
A simple tag has the following characteristics:
Tag Library
Prefix Tag Name
-477-
Handout 38
Web Design & Development CS-506
So far, we have used many built-in tags. Now the time has come to build your own one.
Custom tags can be built either by using JSP 1.2 specification or JSP 2.0 (latest)
specification.
To develop custom tags using JSP 1.2 involves lot of cumbersome (too difficult for
James Gossling also☺). However, JSP 2.0 brings lots of goodies like
Tag Handler is also a java class that is implicitly called when the associated tag is
encountered in the JSP.
Must implement SimpleTag interface
Usually extend from SimpleTagSupport class that has already implemented
SimpleTag interface.
For example,
public class MyTagHandler extends SimpelTagSupport {
………………………
………………………
}
- 478 -
Handout 38
Web Design & Development CS-506
doTag() method
- By default does nothing
- Need to implement / override to code/write functionality of tag
- Invoked when the end element of the tag encountered.
JSP implicit objects (e.g. out etc) are available to tag handler class through
pageContext object.
Note: If you are using any IDE (like netBeans® 4.1, in order to build custom tags,
the IDE will write .tld file for you.
3. Deployment
Note: Any good IDE will also perform this step on your behalf
- 479 -
Handout 38
Web Design & Development CS-506
The next step is to call the tag by its name as defined in TLD. For example, if tag name is
hello then we write:
What actually happened behind the scenes? Container calls the doTag() method of
appropriate tag handler class. After that, Tag Handler will write the appropriate response
back to the page.
Approach
Extend Tag Handler class from SimpleTagSupport class and override
doTag() method
Build TLD file
Deploy
Note: As mentioned earlier, if you are using any IDE (like netBeans® 4.1), the last two
steps will be performed by the IDE.
WelcomeTagHandler.java
package vu;
- 480 -
Handout 38
Web Design & Development CS-506
try {
customtags.tld
If using IDE, this file will be written automatically. In this file you specify the tag name
along with Tag Handler class.
<!—
Specifying the tag name and tag class. Also mentioning that
this tag has no body
-->
<tag>
<name>welcome</name>
<tag-class>vu.WelcomeTagHandler</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
- 481 -
Handout 38
Web Design & Development CS-506
index.jsp
<%--
using taglib directive, specifying the tld file name as well as
prefix. Note that you you use any value for the prefix attribtute
--%>
<html>
<body>
<h3>
<%-- calling welcome tag with the help of prefix --%>
<mytag:welcome />
</h3>
</body>
</html>
Behind the scenes, container will call these setter methods implicitly and pass the value
of the custom tag attribute as an argument.
- 482-
Handout 38
Web Design & Development CS-506
Approach
Extend Tag Handler class from SimpleTagSupport class
- Add instance variable of type String
- Write setter method for this attribute
- Override doTag() method
CourseOutlineBean.java
This is the same file used in the last example
package vubean;
import java.io.*;
// no argument constructor
public CourseOutlineBean() {
sessionNo = 0;
topic = "";
assignment = "";
}
// setters
public void setSessionNo(int s){
sessionNo = s;
}
- 483-
Handout 38
Web Design & Development CS-506
// getters
public int getSessionNo( ){
return sessionNo;
}
} // end class
CourseDAO.java
No changes are made to this file too.
package vu;
import java.io.*;
import java.sql.*;
import java.util.*;
public CourseDAO() {
establishConnection();
}
try{
// establishing conection
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}catch(Exception ex){
System.out.println(ex);
}
}
- 484 -
Handout 38
Web Design & Development CS-506
try{
ResultSet rs = pStmt.executeQuery();
int sNo;
String topic;
String assignment;
while ( rs.next() ) {
sNo = rs.getInt("sessionNo");
topic = rs.getString("topic");
assignment = rs.getString("assignment");
if (assignment == null){
assignment = "";
}
cBean.setSessionNo(sNo);
cBean.setTopic(topic);
cBean.setAssignment(assignment);
}catch(Exception ex){
System.out.println(ex);
} finally {
// to close connection
releaseResources();
}
} // end retrieveCourseOutline
- 485-
Handout 38
Web Design & Development CS-506
if(con != null){
con.close();
}
}catch(Exception ex){
System.out.println();
}
} // end releaseResources
MyTagHandler.java
The tag handler class uses JavaBeans (CourseOutlineBean.java &
CourseDAO.java), and includes the logic of displaying course outline in tabular
format.
package vutag;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.util.*;
/**
* Declaration of pageName property.
*/
private String pageName;
- 486-
Handout 38
Web Design & Development CS-506
/**
* Setter for the pageName attribute.
*/
public void setPageName(java.lang.String value) {
this.pageName = value;
}
/**
* display method used to print courseoutline in tabular form
*/
private void display(ArrayList courseList)throws JspException{
PageContext pc = (PageContext)getJspContext();
JspWriter out = pc.getOut();
try{
CourseOutlineBean courseBean =
(CourseOutlineBean)courseList.get(i);
}
out.print("</TABLE>");
}catch(java.io.IOException ex){
throw new JspException(ex.getMessage());
}
}
- 487 -
Handout 38
Web Design & Development CS-506
mytaglibrary.tld
<!—
Specifying the tag name and tag class. Also mentioning that
this tag has no body
-->
<tag>
<name>coursetag</name>
<tag-class>vutag.MyTagHandler</tag-class>
<body-content>empty</body-content>
<!—
Specifying the attribute name and its type
-->
<attribute>
<name>pageName</name>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
- 488 -
Handout 38
Web Design & Development CS-506
index.jsp
This page is used to display the course options to the user in the radio button form.
<html>
<body>
<h2>Select the page you want to visit</h2>
<h3>
<input type="radio" name = "page" value="java"/>
Java
</h3>
<br>
controller.jsp
Based upon the selection made by the user, this page will redirect the user to respective
pages. Those are web.jsp and java.jsp
<html>
<body>
<%
if (pageName.equals("web")) {
response.sendRedirect("web.jsp");
} else if (pageName.equals("java") ) {
response.sendRedirect("java.jsp");
}
%>
</body>
</html>
- 489-
Handout 38
Web Design & Development CS-506
java.jsp
<%-- using taglib directive, specifying the tld file and prefix --%>
<%@taglib uri="/WEB-INF/tlds/mytaglibrary.tld" prefix="mytag"%>
<html>
<body>
<center>
<h2> Welcome to Java Learning Center </h2>
<h3> Course Outline</h3>
<%--
calling coursetag and specifying java as attribute
value
--%>
web.jsp
<%-- using taglib directive, specifying the tld file and prefix --%>
<%@taglib uri="/WEB-INF/tlds/mytaglibrary.tld" prefix="mytag"%>
<html>
<body>
<center>
<h2> Welcome to Java Learning Center </h2>
<h3> Course Outline</h3>
<%--
calling coursetag and specifying java as attribute
value
--%>
-------------------
- 490 -
Handout 38
Web Design & Development CS-506
References:
- 491 -