Welcome to Abdul Malik Ikhsan's Blog

JSP Custom Tag Library

Posted in Tutorial JSP by samsonasik on August 30, 2009

Bosan sama JSTL & Standard Action ? Gunakan Custom Tag Library Anda sendiri. Pada tutorial kali ini, saya akan memaparkan penggunaan SimpleTagSupport untuk menjalankan tag buatan kita sendiri ( tentunya ada aturan mainnya 🙂 ). Class SimpleTagSupport mengextends Interface SimpleTag yang mengextends Interface JSPTag. Tentang method2 detail nya, mungkin bisa dicari sendiri di google, kali ini saya lebih memfokuskan pada penggunaan method doTag saja :).

Ok, let’s start. Pertama, buat project yang struktur directory nya seperti di bawah ini. (Jika belum terbiasa menggunakan WAR (Web Application Archive) , Anda bisa gunakan ROOT directory ) :
web

Nah, kalau sudah, kita isi source code masing2.
1). wordpress.tld

<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
 version="2.0">

 <tlib-version>1.2</tlib-version>
 <uri>simpletagwordpress</uri>
 <tag>
 <description>Worst use of a custom tag</description>
 <name>simpletag</name>
 <tag-class>wordpress.SamsonSimpleTag</tag-class>
 <body-content>empty</body-content>
 </tag>
</taglib>

2). SamsonSimpleTag.java

package wordpress;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;

public class SamsonSimpleTag extends SimpleTagSupport{
 public void doTag() throws JspException, IOException{
 PageContext pageContext = (PageContext) getJspContext();
 JspWriter out = pageContext.getOut();
 out.print("Test Tag Buatanku");
 }
}

And The View :
3). wordpress.jsp

<%@ taglib prefix="tagbuatanku" uri="simpletagwordpress" prefix="r"%>
<tagbuatanku:simpletag/>

<!-- agar ketika class diubah & dikompilasi ulang, otomatis reload -->
<r:reload/>

Asik kan 🙂
————————
referensi : Basham, Bryan ; Sierra, Kathy ; Bates, Bert : Head First Servlet & JSP

Advertisements
Tagged with:

3 Responses

Subscribe to comments with RSS.

  1. Mas Dhani said, on September 30, 2009 at 3:12 pm

    Walah si Ikhsan.. si Ali kamana..? masih di bandung???

    • samsonasik said, on September 30, 2009 at 5:18 pm

      iya, si Ali sekarang masih di bandung jg, kalau ga salah, ngajar di smk4. kamu skrg kerja dmn?

  2. mulya said, on October 7, 2009 at 11:25 pm

    mbantuin temen nyebar link (sorry send to all) iPhone,blackberry,iPod,PSP,PS3,nitendo wii atau gadget mantap lain GRATIS dikirim kerumah.. MAU?? klik http://www.xpango.com?ref=92010743


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: