Labels
According to abbreviationfinder, JSP tags to simplify the code and give more functionality.
Developing websites using tags has certain advantages such as:
- facilitate learning.
- facilitate maintenance.
- promote modularity and reuse.
- simplify your code and reduce the number of lines required.
Its syntax would be:
<% @ taglib uri = “/ taglib / lycka” prefix = “lycka” %>… < lycka : hello />…
When generating the Java code for the Servlet, this hello tag will be interpreted by the Application Server as belonging to the tag library (Tag Library) lycka. This library will be identified in the descriptor file of our application (web.xml) with the resource name (URI) / taglib / lycka.
< taglib – uri > / taglib / lycka </ taglib – uri > < taglib – location > / WEB – INF / tags / lycka. tld </ taglib – location >
An implementation of this descriptor file, / WEB-INF / tags / lycka.tld could be:
<? xml version = “1.0” encoding = “ISO-8859-1” ?> <! DOCTYPE taglib PUBLIC “- // Sun Microsystems, Inc.//DTD JSP Tag Library 1.1 // EN” “http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd” > < Taglib > < tlibversion > 1.0 </ tlibversion > < jspversion > 1.1 </ jspversion > < shortname > simp </ shortname > < uri > http: //www.hachisvertas.net/jcs/taglibs/lycka </ uri> < info > A simple sample tag library </ info > < tag > < name > hello </ name > < tagclass > org. lcyka. taglibs. myTag </ tagclass > < bodycontent > empty </ bodycontent > < info > Praise the beauty of my cat. </ info > </ tag > </ taglib >
And finally, the application server will replace the tag with its associated Java code, org.lcyka.taglibs.miLabel:
package org.lcyka.taglibs ; import… ; public class myLabel extends TagSupport { public int doStart { try { pageContext. getOut (). print (“I’m learning JSP”); } catch ( IOException ioe) { } return SKIP_BODY; }
And finally the browser would show:
I am learning JSP
JSP tags
They are the tags belonging to the JSP specification.
They provide basic functionality.
A first group of tags provides page-level functionality in a very simple way:
- <jsp: forward>, redirect the request to another URL
- <jsp: include>, includes the text of a file within the page
- <jsp: plugin>, download a Java plugin (an applet or a Bean).
A second group allows you to manipulate JavaBean components without Java knowledge.
- <jsp: useBean>, allows you to manipulate a Bean (if it does not exist, the Bean will be created), specifying its scope, class and type.
- <jsp: getProperty>, gets the specified property from a previously declared bean and writes it to the response object.
- <jsp: setProperty>, sets the value of a previously declared bean property.
JSTL tags
They are provided by Sun within the JSTL distribution.
- core, iterations, conditionals, URL manipulation and other general functions.
- xml, for XML manipulation and for XML-Transformation.
- sql, to manage database connections.
- i18n, for the internationalization and formatting of character strings as figures.
Tags Struts TagLib
Distributed by Apache to work together with the Struts Framework.
- PHP
- Bean
- HTML
- Logic
- Nested
- vjgp
Custom labels
We have previously seen an example to create a custom tag stored in our own tag library.
To develop custom tags, we will use the Tag Libraries API.
The Java Servlet API is:
javax. servlet. *
The JSP API extends from this API,
javax. servlet. jsp. *
Finally, the Tag Libraries API extends from the latter.
javax. servlet. jsp. tagext. *
The most relevant of this API are:
- The interfaces
- Tag, which all tags must implement.
- BodyTag, extends the previous one and defines additional methods to inspect the body of a tag.
- The classes
- BodyContent, a handler to read and write to the body of a tag.
- BodyTagSupport, which implements the BodyTag interface.
- TagAttributeInfo, to obtain the information of the tag attributes declared in the TLD.
- TagData, which contains the attribute values.
- TagExtraInfo, to specify extra information about a tag, such as the variables that you enter in the code or the attributes that will be validated.
- TagInfo, based on the TLD information.
- TagLibraryInfo, represents the information of a TLD.
- TagSupport, implements the Tag interface.
- VariableInfo, contains information such as the type and scope of the variables created or modified by the label.
We can find a more detailed description at http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/jsp/tagext/package-summary.html
Another tag example could be the following Java code:
package org.lycka.taglibs ; import… ; public class LowerCaseTag extends BodyTagSupport { public int doAfterBody () throws JspException {try { BodyContent body = getBodyContent (); JspWriter writer = body. getEnclosingWriter (); String bodyString = body. getString (); if (bodyString ! = null ) { writer. print (bodyString. toLowerCase ()); } } catch ( IOException ioe) { throw new JspException (“Error: IOException while writing to the user”); } return SKIP_BODY ; } }
Upon finding the start of the tag, the runtime will first invoke the doStart () method after the class is instantiated. It can return one of the following values:
- SKIP_BODY, does not process the content of the body of the tag.
- EVAL_BODY_INCLUDE, evaluates the body of the tag.
- EVAL_BODY_TAG, evaluates the body of the tag and casts the result to another stream stored in a tag property.
The doAfterBody () method after processing the body of the tag.
Finally the doEndTag () method will be invoked. You can return:
- EVAL_PAGE, to continue processing the JSP page
- SKIP_PAGE, to stop processing the JSP page, to for example redirect the page
Declared in the library descriptor as
< tag > < name > lowercase </ name > < tagclass > org. lycka. taglibs. LowerCaseTag </ tagclass > < bodycontent > JSP </ bodycontent > < info > Put body in lowercase. </ info > </ tag > < tag > < name > lowercase </ name > < tagclass > org. lycka. taglibs. LowerCaseTag </ tagclass > < bodycontent > JSP </ bodycontent > < info > Put body in lowercase. </ info > </ tag >
Used in JSP page
<% @ taglib uri = “/ taglib / lycka” prefix = “lycka” %>… < lycka : lowercase > I am learning JSP. </ lycka: lowercase >
And your output would be
I am learning JSP