There are three types of directives:
Page directive Include directive Taglib directive
Each one of them is described in detail below with examples: In this tutorial, you will learn –
JSP Page directive JSP Include directive JSP Taglib Directive
JSP Page directive
Syntax of Page directive:
It provides attributes that get applied to entire JSP page. It defines page dependent attributes, such as scripting language, error page, and buffering requirements. It is used to provide instructions to a container that pertains to current JSP page.
Following are its list of attributes associated with page directive:
Language Extends Import contentType info session isThreadSafe autoflush buffer IsErrorPage pageEncoding errorPage isELIgonored
More details about each attribute
language: It defines the programming language (underlying language) being used in the page.Syntax of language: <%@ page language=“value” %> Here value is the programming language (underlying language)
Example: Explanation of code: In the above example, attribute language value is Java which is the underlying language in this case. Hence, the code in expression tags would be compiled using java compiler.
Extends: This attribute is used to extend (inherit) the class like JAVA does
Syntax of extends: Here the value represents class from which it has to be inherited. Example: Explanation of the code: In the above code JSP is extending DemoClass which is within demotest package, and it will extend all class features.
Import: This attribute is most used attribute in page directive attributes.It is used to tell the container to import other java classes, interfaces, enums, etc. while generating servlet code.It is similar to import statements in java classes, interfaces.
Syntax of import: Here value indicates the classes which have to be imported. Example: Explanation of the code: In the above code, we are importing Date class from java.util package (all utility classes), and it can use all methods of the following class.
contentType:
It defines the character encoding scheme i.e. it is used to set the content type and the character set of the response The default type of contentType is “text/html; charset=ISO-8859-1”.
Syntax of the contentType: Example: Explanation of the code: In the above code, the content type is set as text/html, it sets character encoding for JSP and for generated response page.
info
It defines a string which can be accessed by getServletInfo() method. This attribute is used to set the servlet description.
Syntax of info: Here, the value represents the servlet information. Example: Explanation of the code: In the above code, string “Guru Directive JSP” can be retrieved by the servlet interface using getServletInfo()
Session
JSP page creates session by default. Sometimes we don’t need a session to be created in JSP, and hence, we can set this attribute to false in that case.The default value of the session attribute is true, and the session is created.When it is set to false, then we can indicate the compiler to not create the session by default.
Syntax of session: Here in this case session attribute can be set to true or false Example: Explanation of code: In the above example, session attribute is set to “false” hence we are indicating that we don’t want to create any session in this JSP
isThreadSafe:
It defines the threading model for the generated servlet. It indicates the level of thread safety implemented in the page. Its default value is true so simultaneous We can use this attribute to implement SingleThreadModel interface in generated servlet. If we set it to false, then it will implement SingleThreadModel and can access any shared objects and can yield inconsistency.
Syntax of isThreadSafe: Here true or false represents if synchronization is there then set as true and set it as false. Example: Explanation of the code: In the above code, isThreadSafe is set to “true” hence synchronization will be done, and multiple threads can be used.
AutoFlush:
This attribute specifies that the buffered output should be flushed automatically or not and default value of that attribute is true. If the value is set to false the buffer will not be flushed automatically and if its full, we will get an exception. When the buffer is none then the false is illegitimate, and there is no buffering, so it will be flushed automatically. Syntax of autoFlush: Here true/false represents whether buffering has to be done or not Example: Explanation of the code: In the above code, the autoflush is set to false and hence buffering won’t be done and it has manually flush the output.
Buffer:
Using this attribute the output response object may be buffered. We can define the size of buffering to be done using this attribute and default size is 8KB. It directs the servlet to write the buffer before writing to the response object.
Syntax of buffer: Here the value represents the size of the buffer which has to be defined. If there is no buffer, then we can write as none, and if we don’t mention any value then the default is 8KB Example: Explanation of the code: In the above code, buffer size is mentioned as 16KB wherein the buffer would be of that size
isErrorPage:
It indicates that JSP Page that has an errorPage will be checked in another JSP page Any JSP file declared with “isErrorPage” attribute is then capable to receive exceptions from other JSP pages which have error pages. Exceptions are available to these pages only. The default value is false.
Syntax of isErrorPage: Example: Explanation of the code: In the above code, isErrorPage is set as true. Hence, it will check any other JSPs has errorPage (described in the next attribute) attribute set and it can handle exceptions.
PageEncoding:
The “pageEncoding” attribute defines the character encoding for JSP page. The default is specified as “ISO-8859-1” if any other is not specified. Syntax of pageEncoding: Here value specifies the charset value for JSP Example: Explanation of the code: In the above code “pageEncoding” has been set to default charset ISO-8859-1
errorPage:
This attribute is used to set the error page for the JSP page if JSP throws an exception and then it redirects to the exception page. Syntax of errorPage: Here value represents the error JSP page value Example: Explanation of the code: In the above code, to handle exceptions we have errroHandler.jsp
isELIgnored:
IsELIgnored is a flag attribute where we have to decide whether to ignore EL tags or not. Its datatype is java enum, and the default value is false hence EL is enabled by default.
Syntax of isELIgnored: Here, true/false represents the value of EL whether it should be ignored or not. Example: Explanation of the code: In the above code, isELIgnored is true and hence Expression Language (EL) is ignored here. In the below example we are using four attributes(code line 1-2) Example with four attributes Explanation of the code: Code Line 1-2: Here we have defined four attributes i.e.
Language: It is set as Java as programming language contentType: set as text/html to tell the compiler that html has to be format pageEncoding: default charset is set in this attribute isELIgnored: Expression Tag is false hence it is not ignored
Code Line 3: Here we have used import attribute, and it is importing “Date class” which is from Java util package, and we are trying to display current date in the code. When you execute the above code, you will get the following output
Output:
Date is: Current date using the date method of the date class
JSP Include directive
JSP “include directive”( codeline 8 ) 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: Example: Directive_jsp2.jsp (Main file) Directive_header_jsp3.jsp (which is included in the main file) Explanation of the code: Directive_jsp2.jsp: Code Line 3: In this code, we use include tags where we are including the file directive_header_jsp3.jsp into the main file(_jsp2.jsp)and gets the output of both main file and included file. Directive_header_jsp3.jsp: Code Line 11-12: We have taken a variable count initialized to 1 and then incremented it. This will give the output in the main file as shown below. When you execute the above code you get the following output:
Output:
The output is Header file: 2 : This is the main file The output is executed from the directive_jsp2.jsp file while the directive_header_jsp3.jsp included file will be compiled first. After the included file is done, the main file is executed, and the output will be from the main file “This is the main file”. So you will get the output as “Header file: 2” from _jsp3.jsp and “This is main file” from _jsp2.jsp.
JSP 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.
Syntax of taglib directive: Here “uri” attribute is a unique identifier in tag library descriptor and “prefix” attribute is a tag name. Example: Explanation of the code: Code Line 3: Here “taglib” is defined with attributes uri and prefix. Code Line 9: “gurutag” is the custom tag defined and it can be used anywhere