DIRECTIVES
JSP directives are JSP elements that provide global information about a JSP page. An example would be a directive that included a list of Java classes to be imported into a JSP. Three possible directives are currently defined by the JSP specification page, include, and taglib.
The page Directive
The page directive defines information that will globally affect the JSP containing the directive.
The syntax of a JSP page directive is
<%@ page attribute="value" %>
Note Because all mandatory attributes are defaulted, we are not required to specify any page directives.
Attributes for the page Directive are:
language=”scriptingLanguage”
Tells the server which language will be used to compile the JSP file. Java is currently the only available JSP language, but we hope there will be other language support in the not-toodistant future.
extends=”className”
Defines the parent class from which the JSP will extend. While we can extend JSP from other servlets, doing so will limit the optimizations performed by the JSP/servlet engine and is therefore not recommended.
import=”importList”
Defines the list of Java packages that will be imported into this JSP. It will be a commaseparated list of package names and fully qualified Java classes.
session=”true|false”
Determines whether the session data will be available to this page. The default is true. If our JSP is not planning on using the session, then this attribute should be set to false for better performance.
buffer=”none|size in kb”
Determines whether the output stream is buffered.The default value is 8KB.
autoFlush=”true|false”
Determines whether the output buffer will be flushed automatically, or whether it will throw an exception when the buffer is full. The default is true.
isThreadSafe=”true|false”
Tells the JSP engine that this page can service multiple requests at one time. By default, this value is true. If this attribute is set to false, the SingleThreadModel is used.
info=”text”
Represents information about the JSP page that can be accessed by invoking the page’s
Servlet.getServletInfo() method.
errorPage=”error_url”Represents the relative URL to a JSP that will handle JSP exceptions.
isErrorPage=”true|false”
States whether the JSP is an errorPage. The default is false.
contentType=”ctinfo”
Represents the MIME type and character set of the response sent to the client.
Example
Next listing presents a page that uses a class not in the standard JSP import list: java.util.Date
<%@ page import=”java.util.Date” %>
<html>
<body>
The current time is <%= (new Date()).toString() %>
</body>
</html>Output:
Current date
Next listing presents a page that uses contentType attribute to generating excel spreadsheets.
<%@ page contentType="application/vnd.ms-excel" %>
<%-- Note that there are tabs, not spaces, between columns. --%>
1997 1998 1999 2000 2001
12.3 13.4 14.5 15.6 16.7Output:
The page Directive
The page directive defines information that will globally affect the JSP containing the directive.
The syntax of a JSP page directive is
<%@ page attribute="value" %>
Note Because all mandatory attributes are defaulted, we are not required to specify any page directives.
Attributes for the page Directive are:
language=”scriptingLanguage”
Tells the server which language will be used to compile the JSP file. Java is currently the only available JSP language, but we hope there will be other language support in the not-toodistant future.
extends=”className”
Defines the parent class from which the JSP will extend. While we can extend JSP from other servlets, doing so will limit the optimizations performed by the JSP/servlet engine and is therefore not recommended.
import=”importList”
Defines the list of Java packages that will be imported into this JSP. It will be a commaseparated list of package names and fully qualified Java classes.
session=”true|false”
Determines whether the session data will be available to this page. The default is true. If our JSP is not planning on using the session, then this attribute should be set to false for better performance.
buffer=”none|size in kb”
Determines whether the output stream is buffered.The default value is 8KB.
autoFlush=”true|false”
Determines whether the output buffer will be flushed automatically, or whether it will throw an exception when the buffer is full. The default is true.
isThreadSafe=”true|false”
Tells the JSP engine that this page can service multiple requests at one time. By default, this value is true. If this attribute is set to false, the SingleThreadModel is used.
info=”text”
Represents information about the JSP page that can be accessed by invoking the page’s
Servlet.getServletInfo() method.
errorPage=”error_url”Represents the relative URL to a JSP that will handle JSP exceptions.
isErrorPage=”true|false”
States whether the JSP is an errorPage. The default is false.
contentType=”ctinfo”
Represents the MIME type and character set of the response sent to the client.
Example
Next listing presents a page that uses a class not in the standard JSP import list: java.util.Date
<%@ page import=”java.util.Date” %>
<html>
<body>
The current time is <%= (new Date()).toString() %>
</body>
</html>Output:
Current date
Next listing presents a page that uses contentType attribute to generating excel spreadsheets.
<%@ page contentType="application/vnd.ms-excel" %>
<%-- Note that there are tabs, not spaces, between columns. --%>
1997 1998 1999 2000 2001
12.3 13.4 14.5 15.6 16.7Output:
Use of error Page and isErrorPage attribute:-
Error1.jsp : generate an arithmetic exception
<%@ page language="java" ErrorPage="Error2.jsp" %>
<html>
<body>
<center>
<%
out.println(0/0);
%>
</center>
</body>
</html>
Error2.jsp
<%@ page language="java" isErrorPage="true" %>
<html>
<body>
<center>This is a error page<hr><br><br>
<%=exception%>
</center>
</body>
</html>Output:
The include Directive
The include directive is used to insert text and/or code at JSP translation time. The syntax of the include directive is shown in the following code snippet:
<%@ include file="relativeURLspec" %>
The file attribute can reference a normal text HTML file or a JSP file, which will be evaluated at translation time. This resource referenced by the file attribute must be local to the Web application that contains the include directive. Here’s a sample include directive:
<%@ include file="header.jsp" %>
Note: Because the include directive is evaluated at translation time, this included text will be evaluated only once. Thus, if the included resource changes, these changes will not be reflected until the JSP/servlet container is restarted or the modification date of the JSP that includes that file is changed.
The taglib Directive
The taglib directive states that the including page uses a custom tag library, uniquely identified by a URI and associated with a prefix that will distinguish each set of custom tags to be used in the page.
JSP introduced an extremely valuable new capability, the ability to define our own JSP tags. We define how the tag, its attributes, and its body are interpreted. In order to use custom JSP tags, we need to define three separate components: the tag handler class that defines the tag’s behavior, the tag library descriptor file that maps the XML element names to the tag implementations, and the JSP file that uses the tag library.
The Tag Handler Class
When defining a new tag, our first task is to define a Java class that tells the system what to do when it sees the tag. This class must implement the javax.servlet.jsp.tagext.Tag interface. This is usually accomplished by extending the TagSupport or BodyTagSupport class. Next listing is an example of a simple tag that just convert a numeric value in String. Save CustomTag.java in
WEB-INF/classes/beanpack.
package beanpack;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class CustomTag extends TagSupport {
private String number;
public void setNumber(String number) {
this.number=number;
}
public int doStartTag() throws JspTagException {
String temp=null;
if(number.equals("10")) {
try{
pageContext.getOut().write("TEN");
}catch(IOException e) {
throw new JspTagException("tag could not write to jsp
out");
}}
return EVAL_BODY_INCLUDE;
}
}
The Tag Library Descriptor File
Once we have defined a tag handler, our next task is to identify the class to the server and to associate it with a particular XML tag name. This task is accomplished by means of a tag library descriptor file. This file contains some fixed information, an arbitrary short name for our library, a short description, and a series of tag descriptions. Save this next file as CustomTag.tld in WEB-INF.
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library
1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>joshna</short-name>
<description>Simple Tag example</description>
<tag>
<name>convert</name>
<tag-class>beanpack.CustomTag</tag-class>
<body-content>JSP</body-content>
<description>Simple</description>
<attribute>
<name>number</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>The JSP File
Once we have a tag handler implementation and a tag library description, we are ready to write a JSP file that makes use of the tag. Next listing gives an example. Somewhere before the first use of our tag, we need to use the taglib directive. This directive has the following form:
<%@ taglib uri="..." prefix="..." %>
The required uri attribute can be either an absolute or relative URL referring to a tag library descriptor file. We use web.xml file that maps an absolute URL for a tag library descriptor to a file on the local system.
<taglib>
<taglib-uri>MANU</taglib-uri>
<taglib-location>/WEB-INF/CustomTag.tld</taglib-location>
</taglib>The prefix attribute, also required, specifies a prefix that will be used in front of whatever tag name the tag library descriptor defined. For example, if the TLD file defines a tag named tag1 and the prefix attribute has a value of test, the actual tag name would be test:tag1.
CustomDemo.jsp
<%@ taglib uri="MANU" prefix="josna" %>
<josna:convert1 number="10" > </josna:convert1>Output:
TEN
Once we have defined a tag handler, our next task is to identify the class to the server and to associate it with a particular XML tag name. This task is accomplished by means of a tag library descriptor file. This file contains some fixed information, an arbitrary short name for our library, a short description, and a series of tag descriptions. Save this next file as CustomTag.tld in WEB-INF.
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library
1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>joshna</short-name>
<description>Simple Tag example</description>
<tag>
<name>convert</name>
<tag-class>beanpack.CustomTag</tag-class>
<body-content>JSP</body-content>
<description>Simple</description>
<attribute>
<name>number</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>The JSP File
Once we have a tag handler implementation and a tag library description, we are ready to write a JSP file that makes use of the tag. Next listing gives an example. Somewhere before the first use of our tag, we need to use the taglib directive. This directive has the following form:
<%@ taglib uri="..." prefix="..." %>
The required uri attribute can be either an absolute or relative URL referring to a tag library descriptor file. We use web.xml file that maps an absolute URL for a tag library descriptor to a file on the local system.
<taglib>
<taglib-uri>MANU</taglib-uri>
<taglib-location>/WEB-INF/CustomTag.tld</taglib-location>
</taglib>The prefix attribute, also required, specifies a prefix that will be used in front of whatever tag name the tag library descriptor defined. For example, if the TLD file defines a tag named tag1 and the prefix attribute has a value of test, the actual tag name would be test:tag1.
CustomDemo.jsp
<%@ taglib uri="MANU" prefix="josna" %>
<josna:convert1 number="10" > </josna:convert1>Output:
TEN


No comments:
Post a Comment