Directives in JSP

JSP directives are the elements of a JSP source code that guide the web container on how to translate the JSP page into it’s respective servlet.
Syntax :

<%@ 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.
Different types of JSP directives :
There are three different JSP directives available. They are as follows:

  1. Page Directives : JSP page directive is used to define the properties applying the JSP page, such as the size of the allocated buffer, imported packages and classes/interfaces, defining what type of page it is etc. The syntax of JSP page directive is as follows:
    <%@page attribute = "value"%>

    Different properties/attributes :
    The following are the different properties that can be defined using page directive :



    • import: This tells the container what packages/classes are needed to be imported into the program.

      Syntax:

      <%@page import = "value"%>

      Example :

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <%-- JSP code to demonstrate how to use page
       directive to import a package --%>
        
      <%@page import = "java.util.Date"%>
      <%Date d = new Date();%>
      <%=d%>

      chevron_right

      
      

      Output:

    • contentType: This defines the format of data that is being exchanged between the client and the server. It does the same thing as the setContentType method in servlet used to.

      Syntax:

      <%@page contentType="value"%>
      Usage Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <%-- JSP code to demonstrate how to use
      page directive to set the type of content --%>
        
      <%@page contentType = "text/html" %>
      <% = "This is sparta" %>

      chevron_right

      
      

      Output :

    • info: Defines the string which can be printed using the ‘getServletInfo()’ method.

      Syntax:

      <%@page info="value"%>

      Usage Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <%-- JSP code to demonstrate how to use page
       directive to set the page information --%>
        
      <%@page contentType = "text/html" %>
      <% = getServletInfo()%>

      chevron_right

      
      

      Output:

    • buffer: Defines the size of the buffer that is allocated for handling the JSP page. The size is defined in Kilo Bytes.

      Syntax:

      <%@page buffer = "size in kb"%>
    • language: Defines the scripting language used in the page. By default, this attribute contains the value ‘java’.
    • isELIgnored: This attribute tells if the page supports expression language. By default, it is set to false. If set to true, it will disable expression language.

      Syntax:

      <%@page isElIgnored = "true/false"%>

      Usage Example:


      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      <%-- JSP code to demonstrate how to use page 
      directive to ignore expression language --%>
        
      <%@page contentType = "text/html" %>
      <%@page isELIgnored = "true"%>
      <body bgcolor = "blue">
      <c:out value = "${'This is sparta}"/>
      </body>

      chevron_right

      
      

      Output:
      (blank page)

    • errorPage: Defines which page to redirect to, in case the current page encournters an exception.
      Syntax:

      <%@page errorPage = "true/false"%>

      Usage Example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      //JSP code to divide two numbers
      <%@ page errorPage = "error.jsp" %>  
        
      <%   
      // dividing the numbers
      int z = 1/0;  
        
       // result 
      out.print("division of numbers is: " + z);   
      %>  

      chevron_right

      
      

    • isErrorPage: It classifies whether the page is an error page or not. By classifying a page as an error page, it can use the implicit object ‘exception’ which can be used to display exceptions that have occured.

      Syntax:

      <%@page eisrrorPage="true/false"%>

      Usage example:

      filter_none

      edit
      close

      play_arrow

      link
      brightness_4
      code

      //JSP code for error page, which displays the exception
      <%@ page isErrorPage = "true" %>  
           
      <h1>Exception caught</h1>  
           
      The exception is: <% = exception %> 
      Output:

      chevron_right

      
      

      Output:

  2. Include directive :

    JSP include directive is used to include other files into the current jsp page. These files can be html files, other sp files etc. The advantage of using an include directive is that it allows code re-usability.

    The syntax of an include directive is as follows:

    <@%include file = "file location"%>

    Usage example:
    In the following code, we’re including the contents of an html file into a jsp page.

    a.html

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <h1>This is the content of a.html</h1>

    chevron_right

    
    

    index.jsp

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <% = Local content%>
    <%@include file = "a.html"%>
    <% = local content%>

    chevron_right

    
    

    Output :

  3. Taglib Directive : The taglib directive is used to mention the library whose custom-defined tags are being used in the JSP page. It’s major application is JSTL(JSP standard tag library).

    Syntax:

    <@%taglib uri = "library url" prefix="the prefix to 
    identify the tags of this library with"%>

    Usage Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <%-- JSP code to demonstrate 
    taglib directive--%>
    <%@ taglib uri = "http://java.sun.com/jsp/jstl/core
    prefix = "c" %>  
        
    <c:out value = "${'This is Sparta'}"/> 

    chevron_right

    
    

    In the above code, we’ve used to taglib directive to point to the JSTL library which is a set of some custom-defined tags in JSP that can be used in place of the scirptlet tag (<%..%>). The prefix attribute is used to define the prefix that is used to identify the tags of this library. Here, the prefix c is used in the <c:out> tag to tell the container that this tag belongs to the library mentioned above.

    Output:



    My Personal Notes arrow_drop_up

    Intern Technical Content Writing at GeeksforGeeks

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



    Improved By : Akanksha_Rai



    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.