JSP DIRECTIVES 07 Jan22 JSP Directives JSP directives

  • Slides: 25
Download presentation
JSP DIRECTIVES 07 -Jan-22

JSP DIRECTIVES 07 -Jan-22

JSP Directives: • JSP directives provide directions and instructions to the container, telling it

JSP Directives: • JSP directives provide directions and instructions to the container, telling it how to handle certain aspects of JSP processing. • A JSP directive affects the overall structure of the servlet class. It usually has the following form: <%@ directive attribute="value" %> • Directives can have a number of attributes which you can list down as key-value pairs and separated by commas. • The blanks between the @ symbol and the directive name, and between the last attribute and the closing %>, are optional.

Directives affect the servlet class itself A directive has the form: <%@ directive attribute="value"

Directives affect the servlet class itself A directive has the form: <%@ directive attribute="value" %> or <%@ directive attribute 1="value 1" attribute 2="value 2". . . attribute. N="value. N" %> Directives are messages to the JSP container and do not produce output into the current output stream 3

Types of directive tag: Directive Description <%@ page. . . %> Defines page-dependent attributes,

Types of directive tag: Directive Description <%@ page. . . %> Defines page-dependent attributes, such as scripting language, error page, and buffering requirements. <%@ include. . . %> Includes a file during the translation phase. <%@ taglib. . . %> Declares a tag library, containing custom actions, used in the page

The page Directive: The page directive is used to provide instructions to the container

The page Directive: The page directive is used to provide instructions to the container that pertain to the current JSP page. You may code page directives anywhere in your JSP page. Defines attributes that apply to an entire JSP page

The page Directive: By convention, page directives are coded at the top of the

The page Directive: By convention, page directives are coded at the top of the JSP page. Following is the basic syntax of page directive: <%@ page attribute="value" %> XML equivalent of the above syntax as follows: <jsp: directive. page attribute="value" />

The page Directive: Following is the list of attributes associated with page directive: No

The page Directive: Following is the list of attributes associated with page directive: No Attribute 1. buffer 2. auto. Flush Purpose Specifies a buffering model for the output stream. Controls the behaviour of the servlet output buffer. 3. content. Type Defines the character encoding scheme. 4. error. Page Defines the URL of another JSP that reports on Java unchecked runtime exceptions. 5. is. Error. Page Indicates if this JSP page is a URL specified by another JSP page's error. Page attribute. 6. extends Specifies a superclass that the generated

The page Directive: Following is the list of attributes associated with page directive: No.

The page Directive: Following is the list of attributes associated with page directive: No. Attribute Purpose 7. import Specifies a list of packages or classes for use in the JSP as the Java import statement does for Java classes. 8. info Defines a string that can be accessed with the servlet's get. Servlet. Info() method. 9. is. Thread. Saf Defines the threading model for the generated servlet. e 10. language Defines the programming language used in the JSP page. 11. session Specifies whether or not the JSP page participates in HTTP sessions 12 is. ELIgnored Specifies whether or not EL expression within the JSP page will be ignored. 13. is. Scripting. E Determines if scripting elements are allowed for use. nabled

The page Directive: Defines page. attributes that apply to an entire JSP <%@ page

The page Directive: Defines page. attributes that apply to an entire JSP <%@ page [ language="java" ] [ extends="package. class" ] [ import="{package. class | package. *}, …”] [ session="true|false" ] [ buffer="none|8 kb|sizekb" ] [ auto. Flush="true|false" ] [ is. Thread. Safe="true|false" ] [ info="text" ] [ error. Page="relative. URL" ] [ content. Type="mime. Type [ ; charset=character. Set ]" [ is. Error. Page="true|false" ] %>

Attribute Syntax Example language <%@ page language="value" %> <%@ page language="java" content. Type="text/html; charset=ISO

Attribute Syntax Example language <%@ page language="value" %> <%@ page language="java" content. Type="text/html; charset=ISO 8859 -1" page. Encoding="ISO-88591"%> Extends <%@ page extends="value" %> <%@ page extends="demotest. Demo. Class" %> import <%@ page import="value" %> <%@ page language="java" content. Type="text/html; charset=ISO 8859 -1" import="java. util. Date" page. Encoding="ISO-8859 -1"%> content. Typ e <%@ page content. Type="val ue" %> <%@ page language="java" content. Type="text/html; charset=ISO -8859 -1" page. Encoding="ISO-88591"%> is. ELIgnored <%@ page language="java" is. ELIgnored="true content. Type="text/html; " /false" %> page. Encoding="ISO-8859 -1"

Attribut e info session Syntax <%@ page info="value" %> <%@ page language="java" content. Type="text/html;

Attribut e info session Syntax <%@ page info="value" %> <%@ page language="java" content. Type="text/html; charset=ISO-8859 -1" info=“JSP Directive Example" page. Encoding="ISO-8859 -1"%> <%@ page language="java" session="true content. Type="text/html; charset /false"%> =ISO-8859 -1" session="false"%> <% @ page is. Thread. Safe ="true/false" %> auto. Flush <% @ page auto. Flush="tr is. Thread. Saf e Example <%@ page language="java" content. Type="text/html; charset= ISO-8859 -1" is. Thread. Safe="true"%> <%@ page language="java" content. Type="text/html; charset=

Attribut Syntax Example e buffer <%@ page language="java" buffer="valu content. Type="text/html; charset = e"

Attribut Syntax Example e buffer <%@ page language="java" buffer="valu content. Type="text/html; charset = e" %> ISO-8859 -1“ buffer="16 KB"%> <%@ page is. Error. Page= "true/false" %> page. Enc <%@ page oding page. Encodi ng="vaue" %> error. Page <%@ page error. Page=" value" %> is. Error. Page <%@ page language="java" content. Type="text/html; charset = ISO-8859 -1" is. Error. Page="true"%> <%@ page language="java" content. Type="text/html; " page. Encoding="ISO-8859 -1" error. Page="error. Handler. jsp"%>

Page Directives Example (5 types) <%@ page language="java" content. Type="text/html; " page. Encoding="ISO-8859 -1”

Page Directives Example (5 types) <%@ page language="java" content. Type="text/html; " page. Encoding="ISO-8859 -1” is. ELIgnored="false"%> <%@page import="java. util. Date" %> <!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 01 Transitional//EN" "http: //www. w 3. org/TR/html 4/loose. dtd"> <html> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859 -1"> <title>Directive Example</title> </head> <a>Date is: </a> </body> <head> <body> <%= new java. util. Date() %> </html>

The include Directive: The include directive is used to include a file during the

The include Directive: The include directive is used to include a file during the translation phase. This directive tells the container to merge the content of other external files with the current JSP during the translation phase. You may code include directives anywhere in your JSP page. The general usage form of this directive is as follows: <%@ include file="relative url" > The filename in the include directive is actually a relative URL. If you just specify a filename with no associated path, the JSP compiler assumes that the file is in the same directory as your JSP. You can write XML equivalent of the above syntax as follows: <jsp: directive. include file="relative url" />

The include Directive: JSP "include directive“ is used to include one file to the

The include Directive: JSP "include directive“ is used to include one file to the another file This included file can be HTML, JSP, text files, etc. It is also useful in creating templates with the user views and break the pages into header&footer and sidebar actions. It includes file during translation phase Syntax <%@ of include directive: include…. %>

The include Directive: Includes a static file in a JSP file, parsing the file's

The include Directive: Includes a static file in a JSP file, parsing the file's JSP elements. Syntax <%@ include file="relative. URL" %> The <%@ include %> directive inserts a file of text or code in a JSP file at translation time, when the JSP file is compiled. <%@ include %> process is static. A static include means that the text of the included file is added to the JSP file. The included file can be: JSP file, 2. HTML file, 3. text file. 1.

The include directive inserts another file into the file being parsed The included file

The include directive inserts another file into the file being parsed The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives Syntax: The <%@ include file="URL " %> URL is treated as relative to the JSP page If the URL begins with a slash, it is treated as relative to the home directory of the Web server include directive is especially useful for inserting things like navigation bars 17 The

Include_Main. File. jsp <%@ page language="java" content. Type="text/html; charset=ISO-8859 -1“ page. Encoding="ISO-8859 -1"%> <%@

Include_Main. File. jsp <%@ page language="java" content. Type="text/html; charset=ISO-8859 -1“ page. Encoding="ISO-8859 -1"%> <%@ include file="Header_File. jsp" %> <!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 01 Transitional//EN" "http: //www. w 3. org/TR/html 4/loose. dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859 -1"> <title> Include Directive Example </title> </head> <body> </html> <a> This is the main file </a> </body>

Header_File. jsp <%@ page language="java" content. Type="text/html; charset=ISO-8859 -1" page. Encoding="ISO-8859 -1"%> <!DOCTYPE html

Header_File. jsp <%@ page language="java" content. Type="text/html; charset=ISO-8859 -1" page. Encoding="ISO-8859 -1"%> <!DOCTYPE html PUBLIC "-//W 3 C//DTD HTML 4. 01 Transitional//EN" "http: //www. w 3. org/TR/html 4/loose. dtd"> <html> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859 -1"> <head> </head> <body> <a> Header file : </a> <%int count =1; count++; out. println(count); %> : </body> </html>

The taglib Directive: The Java Server Pages API allows you to define custom JSP

The taglib Directive: The Java Server Pages API allows you to define custom JSP tags that look like HTML or XML tags and a tag library is a set of user-defined tags that implement custom behaviour. The taglib directive declares that your JSP page uses a set of custom tags, identifies the location of the library, and provides a means for identifying the custom tags in your JSP page.

The taglib Directive: Syntax: <%@ taglib uri="uri" prefix="prefix. Of. Tag" > Where the uri

The taglib Directive: Syntax: <%@ taglib uri="uri" prefix="prefix. Of. Tag" > Where the uri attribute value resolves to a location the container understands and the prefix attribute informs a container what bits of markup are custom actions. The prefix is the same as the prefix you specify in the taglib directive, and the tagname is the name of a tag implemented in the tag library. You can write XML equivalent of the above syntax as follows: <jsp: directive. taglib uri="uri" prefix="prefix. Of. Tag" />

The taglib Directive: JSP taglib directive is used to define the tag library with

The taglib Directive: JSP taglib directive is used to define the tag library with "taglib" as the prefix, which we can use in JSP. More detail will be covered in JSP Custom Tags section JSP taglib directive is used in the JSP pages using the JSP standard tag libraries It uses a set of custom tags, identifies the location of the library and provides means of identifying custom tags in JSP page.

The taglib Directive: Defines a tag library and prefix for the custom tags used

The taglib Directive: Defines a tag library and prefix for the custom tags used in the JSP page. Syntax <%@ taglib uri="URITo. Tag. Library" prefix="tag. Prefix" %> Example <%@ taglib uri="http: //thathost/tags" prefix="public" %> <public: loop> </public: loop> The <%@ taglib %> directive declares that the JSP file uses custom tags, names the tag library that defines them, and specifies their tag prefix.

Example: For example, suppose the custlib tag library contains a tag called hello. If

Example: For example, suppose the custlib tag library contains a tag called hello. If you wanted to use the hello tag with a prefix of mytag, your tag would be <mytag: hello> and it will be used in your JSP file as follows: <%@ taglib uri="http: //www. example. com/custlib" prefix="mytag" %> <html> <body> <mytag: hello/> </body> </html> We would be able to call another piece of code using <mytag: hello>. We will see how to develop our custom tags and how to use them in a separate session.

THANK YOU…

THANK YOU…