How to Write Custom Tag Library In Java Text

Jonathan Friesen - Writing Coach

In this example, we are going to create a custom tag that prints the current date and time. For creating any custom tag, we need to follow following steps:

    create the tag handler class and perform action at the start or at the end of the tag. create the tag library descriptor tld file and define tags create the jsp file that uses the custom tag defined in the tld file
to create the tag handler, we are inheriting the tagsupport class and overriding its method dostarttag .to write data for the jsp, we need to use the jspwriter class. The pagecontext class provides getout method that returns the instance of jspwriter class. File: mytaghandler.java tag library descriptor tld file contains information of tag and tag hander classes. Then, one day, someone clever decided that they needed to have dynamic content on their website, invented a scripting language that generated html, and all hell broke loose.

Within months, web pages had grown into a jungle of html, scripting code, and sql statements. at first, javas take on all this was the servlet. Able to leap across buildings in a single bound, jsps looked like a scripting language and acted like scripting language, but in actual fact were nothing more than a clever way of writing servlets! jsps solved a lot of problems that were inherent within servlets, but they introduced a number of issues as well. Worse still, jsps allowed you to avoid having to write javabeans to take care of your business logic putting all your site functionality into the web page itself was a quick fix that, more often than not, would come back and bite you when you least expected it.

Finally, the typical front end developer took to jsp scripts like a fish to a bicycle, shying away from the complexities of vectors, request objects and null pointer exceptions in favour of simpler pleasures. Why am i telling you all this? because, just when it seemed like everything was headed in a downward spiral, there came a solution! enter stage left, the java standard tag library.

meet the jstl
the java standard tag library or jstl is nothing more than a java tag library that provides us with a number of standard functions. Ok, ok put another way, the jstl is going to make your quagmire of jsp script and html look like regular plain old html pages again.

Your pages will be more readable no more scriptlets , your code architecture will become a lot cleaner no more html in your javabean classes and, best of all, the front end developers you work with will invite you to the pub at lunchtime like they used to. Ok, just to whet your appetite before we talk about how to get the thing installed, heres a fairly typical piece of code you might see in a jsp page: clear as mud, right? heres the same functionality written using the jstl: by now, the html developers you work with arent just inviting you to the pub, theyre getting the rounds in as well! ok, lets get this thing installed. You can get hold of a copy from the jakarta apache site see resources at the end of this article.

    create a new web application in tomcat you can just create a new directory under the webapps/ directory. Create a web inf directory within jstl_stuff, and a lib directory within web inf. Copy everything from the lib directory of your jstl archive into lt tomcat home gt /webapps/jstl_stuff/web inf/lib.

    A Level English Paper

to test the installation, create a page entitled index.jsp in the /jstl_stuff directory, and add the following code: now try browsing to
the components explained
im going to be referencing the following class simpsons.homer for the examples. You can compile it and place it in lt tomcat home gt /webapps/web inf/classes/simpsons, or you could write your own. Note: in the following examples, we need to declare that were going to use the homer class in our jsp pages. If you want to get to grips with either of the others, see the resource links at the end of this article. In order to inform tomcat that we are about to use the jstl core tags in our page, we need to include the following taglib directive in every page in which jstl tags are used: as i mentioned earlier, the idea behind the jstl is to provide a basic level of functionality to the programmer, so that jsp scriptlets are no longer needed.

For example, if we want to assign a value to a variable, we can use the set action: to output something to html, we can use the out action: running this should provide us with a single doh! you can set the property of a javabean as well. For example, if wanted to set homer s iq property we could do: veteran jsp coders will probably be scratching their heads at this point. This part of code might look unusual: welcome to the jstl expression language or el ! the el is a method of accessing java variables in a much simpler way than the old jsp style involved. Using it, we can access the variables of an object in a wide variety of ways: the above would access the homer objects iq property. The above would return the second element of the homer objects braincell collection.