Introduction of Directives

Introduction of Directives : Directives are used to convey special information about the page container .directives may be used to specify the scripting language for the page, to include the content of another page ,or to indicate that the page uses a custom tag library. directives do not directly produce an output that is visible to end user when the page is requested; instead, they generate side effects that change the way the jsp container processes the page

JSP directives are as a message from a JSP page to the JSP container that control the processing of the entire page. They are used to set global values .All directives have scope to the entire JSP file. They are used to set page level instruction, insert data form files and specify custom tag libraries. JSP directives control how the JSP compiler generates the servlet. Actually a directive is a way for you to give special instruction to the container at page translation time. The functionality of JSP directives is similar to the one provided by the C preprocessor.

A JSP directive affects the overall structure of the servlet class. it usually has the following form:

<%@ directive attribute=”value” %>

How ever , you can also combine multiple attribute settings for a single directive, as follows:

<%@ directive attribute1=”value1″ attribute2=”value2″ — attrubuteN=”value N” %>

Include Directive: The include directive inserts a file of text or code in JSP file at translation time, when the JSP is compiled. When the include Directive is used, the include process is static.

The included file can be an HTML file, a JSP page, a text file, XML document, or a code file written in the Java programming language. Be careful that the included file does not contain <html>, </html>, <body>, or </body> tags. Because the entire content of the included file is added to the including JSP page, these tags would conflict with the same tags in the including JSP page, causing an error.

Some of the behaviors of the include directive depend on the particular JSP container you are using, for example:

  • The included file might be open and available to all requests, or it might have security restrictions.
  • The JSP page might be recompiled if the included file changes.

JSP Syntax:

<%@ include file=”relativeURL” %>

OR

<jsp:directive.include file=”relativeURL” />

 

Example

include.jsp:

 

 

<html>

<head><title>An Include example</title></head>

<body bgcolor=”white”>

<font color=”blue”>

The current date and time are:

<%@ include file=”date.jsp” %>

</font>

</body>

</html>

 

 

date.jsp:

 

<%@ page import=”java.util.*” %>

<%= (new java.util.Date() ).toLocaleString() %>

Leave a Reply

Your email address will not be published. Required fields are marked *