Previous | Home | Next |
Custom Tags in JSP
This tags are user-defined tags and used scriptlet tag .
Custom Tags advantages
- Eliminates srciptlet tag .
- Separation of business logic for easy to maintain.
- Reusability
Syntax
<prefix:tagname attr1=value1....attrn=valuen />
<prefix:tagname attr1=value1....attrn=valuen />
Another way to be used
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
<prefix:tagname attr1=value1....attrn=valuen >
body code
</prefix:tagname>
<prefix:tagname attr1=value1....attrn=valuen >
body code
</prefix:tagname>prefix="c" %> First Name:<c:out value="${param.fname}"></c:out><br/>
Last Name:<c:out value="${param.lname}"></c:out>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %> First Name:<c:out value="${param.fname}"></c:out><br/>
Last Name:<c:out value="${param.lname}"></c:out>
JSP Custom Tag API
In this tag is used javax.servlet.jsp.tagext package
Example of custom tag with tablib
message.tld
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>My Custom Tag</short-name>
<tag>
<name>MyMsg</name>
<tag-class>beginnersbook.com.Details</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
Using custom tag in JSP : It is created a custom tag named MyMsg and using it.
Note: taglib directive used TLD file path in uri field andv we have created the message.
Custom tag is called like this: <prefix:tagName/>. Our prefix is myprefix and tag name is MyMsg so we have called it as <myprefix:MyMsg/> in the below JSP page.
<%@ taglib prefix="myprefix" uri="WEB-INF/message.tld"%>
<html>
<head>
<title>Custom Tags in JSP Example</title>
</head>
<body>
<myprefix:MyMsg/>
</body>
</html>
Output :
Previous | Home | Next |