The JSP directives are considered as instructions or the messages through which the web container is able to know how to translate the JSP page into servlet class. In other words we can also say that JSP directives affect the structure of the servlet class and are special instructions to the web container. There are three types of directives:
- Page directive
- Include directing
- taglib directive
The page directive is used to define those attributes that are applied to the entire JSP page.
The include directive is used to include a file when the page is being translated.
The taglib directive is used to declare a tag library.
include and taglib directive will be discussed later.
The page directive should be the first statement of the JSP file, we can insert it anywhere but it’s a good practice to make it the first statement of the JSP file. We use the page directive as it tells the web container the page dependent properties when translation is going on.
The following is the syntax of using page directive:
<%@ page attribute = “value” %>
In this syntax the attributes can be one of the following:
- import attribute
- language attribute
- extends attribute
- session attribute
- isThreadSafe attribute
- isErrorPage attribute
- errorPage attribute
- contentType attribute
- autoFlush attribute
- buffer attribute
The classes and the packages that should be imported to the servlet class are defined by the import attribute. This attribute is similar to the java import statement which also imports packages or classes. Consider the following line of code in which we have used the import attribute to import java. sql:
< % @ page import = “java.sql. * ” % >
The language attribute is used to define the programming language that will be used for the scripting of the JSP page.
The extends attribute is used to define the superclass that is extended by the servlet class and from the JSP page.
The session attribute is used to declare that whether the JSP page is using the HTTP session or not. It returns either true or false.
This attributes also returns either true or false depending if the JSP page is thread safe or not.
This attribute is used to declare that we can use the present JSP page as an error page for another JSP page.
This attribute is used to indicate another page for handling all the run time errors that will be thrown by the current JSP page. The URL of the other page that will be used to handle the exceptions is specified by this attribute.
The contentType attribute is used to set the character set encoding for the JSP page and also for the generated response page.
This attribute is used to specify if the buffered output is flushed automatically. The default value of this attribute is always true.
The buffer attribute is used to specify the buffering characteristics for the server output.