These can provide translation time instructions to the jsp engine like session tracking is required or not,is current jsp error page or not,which scripting language we are using in jsp etc.

Syntax:

<%@ directive-name attributeList %>

There are 3 types of directives

  1. page directive
  2.        <%@ page attrname=attrvalue%>
    

    it can be used to define page specific properties.

  3. include directive
  4.        <%@ include file="header.jsp"%>
    

    To include the content of header.jsp in the current page at traslation time(static include)

  5. taglib directive
  6.        <%@taglib prefix="mine" uri="www.java2learn.com"%>
    

    To make custom tags available to the jsp.

page Directives

page directive can contain the following 13 attributes.

  1. import
  2. session
  3. contentType
  4. isELIgnored
  5. isThreadSafe
  6. isErrorPage
  7. errorPage
  8. language
  9. extends
  10. buffer
  11. autoFlash
  12. info
  13. pageEncoding

import

We can use import attribute for importing classes and interfaces in the jsp. This is exactly similar to core java import statement.

%@ page import="java.util.*"%>
The server time is:<%=newDate()%>
To import multiple packages the following are various possibilities:
    <%@ page import="java.util.*"%>
    <%@ page import="java.sql.*"%>

    <%@ page import="java.util.*"  import="java.sql.*"%>

    <%@ page import="java.util.*",import="java.sql.*"%>

The following packages are not required to import explicitly to the jsp because these are available by default in every jsp.

javax.servlet.*;
javax.servlet.http.*;
javax.servlet.jsp.*;
java.lang.*;

With in the same jsp we are not allowed to take any attribute multiple times with different values of page directive,but this rule is not applicable for import attribute.

session

We can use session attribute to make session object unavailable to the jsp. By default session object available in every jsp.If we don't want session object we have to declare page directive as follows.

<%@ page session="false"%>

in this case session implicit object not available to the jsp. If we are not declaring session attribute with true value the equivalent generated Servlet code is.

HttpSession session=null;
session=pageContext.getSession();

If we are declaring session attribute explicitly with false value .Then the above lines od code won't be generated in Servlet and hence session object won't be available in the Jsp. Example:

<%@ page seession="faslse"%>
<%=session.getId()%>
//Compile-time Error:session can't be resolved

The allowed values for the session attribute are

"true","True","TRUe","False","faLSE",
"abc"//Translation Error:Invalid value for session

contentType

We can use this attribute to specify content type of the response.(MIME)

<%@ page contentType="image/JPEG"%>

The default value of the contentType attribute is text/html

isELIgnored

In jsp 1.2 version sun people introduced EL for the elimination of java code in the jsp. We can replace jsp expression tag with EL syntax

Example:

<%=request.getParameter("uname")%>

This we can replace This by using EL

${param.uname}

isELIgnored="true";
EL is disabled in the jsp and EL syntax simply treated as "template text".

isELIgnored="false";
EL syntax should be evaluated and prints it's values to the jsp.

Example:

    <%@ page isELIgnored="true"%>
    the result is :${10/2}
   //output:The Result is :${10/2}
    <%@ page isELIgnored="false"%>
    the result is :${10/2}
  // output:The Result is :5.0

The default value for isELIgnored is false .But in jsp1.2 version it was true

isThreadSafe

We can use this attribute to provide threadSafty to the jsp

isThreadSafe="true"

It means the jsp page is already Thread safe and it is not required to implement SingleThreadModel interface. In this case jsp page can process any no.of client requests simultaneously.

isThreadSafe="false"

The current jsp is not Thread safe hence to provide Thread Safety generated Servlet will implement SingleThreadModel interface. In this case jsp page can process only one requestat a time. The default value for isThreadSafe attribute is "true"

include directive

If several jsp's required same code then it is recommended to separate the common code in a separate file and we can include where ever it is required.
The main advantages of this approaches are

  1. It provides re-usability.
  2. It improves maintainability.
  3. Enhancement will become very easy.

We can achieve this inclusion either by using include directive or by include action.

include directive

<%@ include file="menu.jsp"%>

The inclusion will be happened at translation time hence include directive is also considered as static include or translation time include.

include action

<jsp:include page="menu.jsp"/>

The inclusion will be happened at request processing time hence This inclusion is also called "dynamic inclusion" or "run time inclusion".

Note:
page is the attribute applicable for include action,Where as file attribute applicable for include directive.flush attribute is applicable only for include action but not include directive.

taglib directive

We can use This directive to make custom tags available to the jsp

<%@ taglib prefix="mine" uri="www.java2learn.com"%>

taglib directive contains 2 mandatory attributes

  1. prefix
  2. uri

By using prefix we can access custom tags in the jsp and uri describes the location of tag libraries.