JSP Tutorial

Index  
 

JSP Tags

 
  Another important syntax element of JSP are tags.  JSP tags do not use <%, but just the < character.  A JSP tag is somewhat like an HTML tag.  JSP tags can have a "start tag", a "tag body" and an "end tag".  The start and end tag both use the tag name, enclosed in < and > characters.  The end starts with a / character after the < character.  The tag names have an embedded colon character : in them, the part before the colon describes the type of the tag.  For instance:
<some:tag>
body
</some:tag>
If the tag does not require a body, the start and end can be conveniently merged together, as
<some:tag/>
Here by closing the start tag with a /> instead of > character, we are ending the tag immediately, and without a body.  (This syntax convention is the the same as XML.)

Tags can be of two types: loaded from an external tag library, or predefined tags.   Predefined tags start with jsp: characters.  For instance, jsp:include is a predefined tag that is used to include other pages.

We have already seen the include directive.  jsp:include is similar.  But instead of loading the text of the included file in the original file, it actually calls the included target at run-time (the way a browser would call the included target.  In practice, this is actually a simulated request rather than a full round-trip between the browser and the server).  Following is an example of jsp:include usage

<HTML>
<BODY>
Going to include hello.jsp...<BR>
<jsp:include page="hello.jsp"/>
</BODY>
</HTML>
Try it and see what you get.  Now change the "jsp:include" to "jsp:forward" and see what is the difference.  These two predefined tags are frequently very useful.

Exercise:  Write a JSP to do either a forward or an include, depending upon a boolean variable (hint:  The concepts of mixing HTML and scriptlets work with JSP tags also!)
 

 

 
Index