07 December 2008

Building UI components using JSP custom tags

In the name of Allah, the gracious the merciful

In this article, we will build custom UI components using JSP custom tags so that we can customize your own UI components by adding for example style sheet effects.

Lets start:

Tocreate any custom tag in JSP, first you need to figure it out, so we expect that tag will looks like that

id=”txt1” size=”20” value=”${param.username}” />

so, the TextBox component appears to have id attribute that is mandatory, size and value attributes that are not.

Lets build the Tag Handler class:

we will use JSP 2.0 new SimpleTagSupport class as a superclass for our handler:

package org.daz;

import javax.servlet.jsp.tagext.*;

import javax.servlet.jsp.JspWriter;

import javax.servlet.jsp.JspException;

public class UITagHandler extends SimpleTagSupport {

private java.lang.String id;
private java.lang.String value = "";
private java.lang.String size;

public void doTag() throws JspException {

JspWriter out=getJspContext().getOut();


String tag = "<input type='text' name='" + id + "' size='"
+ size + "'"
+ " value = '" + value + "' />";


catch (java.io.IOException ex) {

throw new JspException(ex.getMessage());


public void setId(java.lang.String value) {

this.id = value;


public void setValue(java.lang.String value) {

this.value = value;


public void setSize(java.lang.String value) {

this.size = value;



Note the doTag() method, it calls getJspContext() to get a JspContext instance that can get a JspWriter from to write tag contents.

As you noticed that we utilize the class <input> tag of HTML, so we need to customize that tag as we need as I said before.

Lets now create the TLD file:

version="1.0" encoding="UTF-8"?>

version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee"

























That's all about the custom UI component, lets try it in.

create a JSP page and code that in it:

taglib uri=”http://daz.org/uicustomtags
prefix=”ui” %>

action="doAction.do" method="post" >

<ui:TextBox id="firstname" size="50" /> <br

<ui:TextBox id="lastname" size="50" /> <br

languages <ui:TextBox id="fav" size="30" />
<br />

type="submit" />


this Servlet to handle the JSP page ( as a Controller ):

import java.io.*;

import java.net.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class TestServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


PrintWriter out = response.getWriter();
out.println ("Hello " + request.getParameter("firstname") + " " + request.getParameter("lastname"));

out.println ("<br> we know that you like " + request.getParameter("fav")+ " programming language.");




note: put this in your DD file to make the Servlet is the action of the form:









Now our example has finished, but you should realize that this idea also can be used to make the design of your JSP pages more easier.
thus instead of using <jsp:include> and <c:import> tags to import headers and footers for your pages, you can build your own custom tags that represent each part of you page, example you would

design tag for the header <ui:header id=”hdr1” param1=”${xx}
param2=${xxx} /> and also for the navigation pans and the footer,
so your page design became easier and more clearer.

Post a Comment