JSP Tag Libraries

Скачать в pdf «JSP Tag Libraries»

Custom JSP tags


The JSP specification explains how to extend the page functionality with custom made tags that allow the JSP developer to expose complex Java code in a simple tag. Developing these custom tags is the subject of this book.

We ’ll look at each of these syntax elements later in this chapter, but let’s first look at an actual JSP file to see its syntax.

2.3.2 A JSP example


A sample JSP file is in listing 2.2 which, again, implements a simple dynamic content generation task. The syntax uses scriptlets to perform conditional HTML and is easier to follow than the servlet. All we’ve done is embed standard Java syntax directly in an HTML file, and embedded it between <% and %> characters.


Listing 2.2 Sample JSP file


<HTML>


<BODY>


<% if(request.getServerName().equals(«localhost»))    { %>


You asked for the server located on your local machine.


<% } else { %>


request.getServerName()



%>


You asked for the server <


<% } %>


</BODY>


</HTML>


It is very obvious by the title that JSP is central to this book. It is mandatory that you know how to create the simple JSP files in which your custom tags will be contained. To this end, we will discuss its syntax elements, how to use them, and how the JSP runtime executes the generated pages.

2.3.3 Scriptlets


Scriptlets are probably the most common JSP syntax element. In essence, a scriptlet is a portion of regular Java code embedded in the JSP content within <% … %> tags. The Java code in scriptlets is executed when the user asks for the page. Script-lets can be used to do absolutely anything the Java language supports, but some of their more common tasks are:

Скачать в pdf «JSP Tag Libraries»