Web programming

Units WEB1P and WEB2P

JSP tags

Introduction

Originally, JSP scriptlets (bits of code between <% and %> tags) were conceived as the means of embedding Java code in a JavaServer Page. However, their use has become discouraged because:

  1. they make HTML pages totally un-XML-like
  2. they frequently result in ugly code (if something print this html else print this other html)
  3. code embedded in scriptlets can't be reused by any other page or class

It is much better to use tags to perform actions in a JSP page.

Tag libraries

JSP 2.0 allows developers to specify and implement their own tags.

Tags are defined in the context of a tag library. The <@taglib> directive is used to specify that a JSP page uses a specific library. Libraries are identified globally by a URI (i.e. this is the universal name of the library), but identified within a particular JSP page by a prefix (chosen by the JSP author).

A tag library is specified in a Tag Library Definition (TLD) file. This is accompanied by Java classes that implement the tags. The base classes for developing tags are in package javax.servlet.jsp.tagext in J2EE 1.4.

Standard tag libraries

JSTL 1.1 defines 4 standard tag libraries:

The reference implementation for these comes from the Apache Jakarta Taglibs project. A library containing the relevant jar files (jstl.jar and standard.jar) comes with NetBeans (4.1 up).

Custom tag libraries

Developers can develop their own tag libraries. Tools such as NetBeans provide assistance in creating TLD files, adding tag definitions to them, and creating the class that implements the tag.

Resources

 

Last updated by Prof Jim Briggs of the School of Computing at the University of Portsmouth

 
The web programming units include some material that was formerly part of the WPRMP, WECPP, WPSSM and WEMAM units.