JSP Directives Elements

JSP Directives Elements

Directives are elements that relay message to the JSP container and affect how it compiles the JSP page. The JSP directives gives direction to server that how to do process of a page.

#Syntax:

There are three types of directives in JSP:

There are three types of directives in JSP:

 

  1. Page Directive: This directive is used to defines page-dependent attributes.

    Syntax:

  2. Include Directive: This directive is used to insert the text contained in another file, it may be static content or another jsp page.

    Syntax:

  3. Taglib Directive: This directive is used to declare custom tag in jsp page.

    Syntax:

Let’s get more understand about these directives.

 

  1. Page Directives: The jsp:directive.page element defines a number of page-dependent properties and communicates these to the JSP container.You can use page directives anywhere in your JSP page. Some attributes are listed below which are used in Page directives tag.

    # buffer

    #import

    #contentType

    # errorPage

    #isErrorPage

    #extends

    #info

    #language

    #isELIgnored

    #isThreadSafe

    #autoFlush

    #session

    #pageEncoding

 

 

1.buffer: This attributes is used to specify the buffer size in KB so that we can handle output generated by JSP page. The default value of buffer attribute is 8kb, we can change it according to our requirement.
Syntax is:

if you want to change the value of buffer so just replace “value” as you want.

Example: Let’s do it in JSP page, like below code.

Output: The output will be look like below image.

2

2.import: This attribute is most useful attributes in Page Directives. By the name it can be understand that with the help of this attribute we can import other java classes, interface , packages etc.

Syntax:

So on place of value you can use anything which is valid and you want to important. Like if you want to import  java.util.Date class so you can do it like below code.

 

Example: Let’s understand it by an simple example like I did in jsp, check below code.

Output: The output of this attribute of page directive will look like below image.

3

3.contentType:  This attribute is used to define contentType of JSP page, the default contentType is text/html.

Syntax:

 

 

Default value:

You can change it according to you like if you want to change it in xml so just enter the value like below code

 

 

Example: In below example we have used contentType as “msword”, checkout how it will respond.

Output: Simply run this code and the output will instruct you to download the ms word file and then you can open it. Check out below image.

4

Simply “Save file” or “Open with” and you will get output like below image.

 

5

 

4.errorPage:  The errorPage attribute is used to handle exception, if any error is occurred so we can simply use errorPage for this, the brief example of errorPage is briefly described in exception implicit object, here i have used an simple example so that we did not use to go back for it.

 

Example: You need to create an exception.jsp page, which will shown when an error occurred.

 

#exception.jsp:

 

#directive.jsp: Include exception.jsp page in this jsp , which is called when an error occurred.

#Output: This two jsp files will give result like below image to us. Because 1 can not be divided by 0 so an exception will occurred.

 

6

5.isErrorPage: This attribute of page directive is used to specify that current page is used as an error page for other jsp page or not. The default value is “false”, If it is “true” means it will use when an exception will occurred else it will not use. As you can see I have used it in above attribute that is “errorPage” the line is

 

If I did it “false” so this will no more in use.

Syntax:

Example: Check above example, that is related to this attribute also.

 

 

6.extends: The extends means you are inheriting parent class , means using inheritance in JSP page. This is rarely used and we can use it if we have extended HttpServlet and overridden some of it’s implementation.

 

Syntax:

7.info : We can use this attribute to sets the information of the JSP page and later on we can retrieve it by using getServletInfo() method of servlet interface.

 

Syntax:

 

 

Value can be your message like:

Example: Need to do

 

 

8.language:  This attribute specify the scripting language which is used in the page. The default value is “Java” and this is only language is supported by it.

Syntax:

  1. isELIgnored: With the help of this attribute we can Ignore Expression Language(EL) in jsp. Expression Language is enabled by default, it means the default value is “false”. We can ignore it by doing it “true”.

Syntax:

 

 

10.isThreadSafe:  The Servlet and JSP page both are support multithreaded means more than one thread can execute simultaneously. This attribute is allows you to handle multithreading concept, If you do not want to use multithreading so just change the value as “false” and if you want to allow multithreading in your jsp page just do it “true”.

Syntax:

  1. autoFlush: This attribute is used to handle buffer , if it is “true” means buffer should be flushed whenever it is full and if it is false so it will throw an exception whenever buffer overflows.

Syntax:

12.session:  By default JSP page creates a session, The default value for session is “True” , If we do not want that it creates default session so just do it “false”. Sometimes we do not need session in JSP page so in that case this attribute will use.

Syntax:

13.pageEncoding:This attribute is used to sets the response  encoding type of our JSP page. The default value of this attribute is “ISO-8859-1”.

Syntax: