Directives are basically used to configure the code that is generated by container in a servlet. As a part of JSP we have three types of directives; they are page directives, include directives and taglib directives.
Page directives are basically used for supplying compile time information to the container for generating a servlet. The page directive will take the data in the form of (key, value) pair.
<%@ page attribute_name1=attribute_value1, attribute_name2=attribute_value2, ...................................., Attribute_nameN=attribute_valueN %>
The following table gives the page directive attribute name and page directive attribute value:
|Attribute name||Attribute value|
|import||This attribute is used for importing either pre-defined or user-
defined packages. The default value is java.lang.*
<%@ page import="java.sql.*, java.io.*" %>
|contentType||. This attribute is used for setting the MIME type (plain/text,
img/jpeg, img/gif, audio/wave, etc.). The default value is
<%@ page contentType="img/jpeg" %>
|language||This attribute represents by default java i.e., in order to
represent any business logic a JSP program is making use of java language. The attribute language can support any of the other programming languages for developing a business logic at server side.
<%@ page language="java" %>
|isThreadSafe||This attribute represents by default true which represents one server side resource can be accessed by many number of clients (each client is treated as one thread). At a time if we make the server side resource to be accessed by a single client then the value of isThreadSafe is false.
<%@ page isThreadSafe="false" %>
|When we write 'n' number of JSP pages, there is a possibility of
occurring exceptions in each and every JSP page. It is not recommended for the JSP programmer to write try and catch blocks in each and every JSP page. It is always recommended to handle all the exceptions in a single JSP page.
isErrorPage is an attribute whose default value is true which indicates exceptions to be processed in the same JSP page which is not recommended. If isErrorPage is false then exceptions are not processed as a part of current JSP page and the exceptions are processed in some other JSP page which will be specified through an attribute called errorPage.
<%@ page isErrorPage="false" errorPage="err.jsp" %>
|Whenever the server side program want to send large amount of data to a client, it is recommended to make autoflush value as false and we must specify the size of the buffer in terms of kb. The default value of autoflush is true which represents the server side program gives the response back to the client each and every time. Since, the buffer size is zero.
<%@ page autoflush="false" buffer="12kb" %> <%@ page autoflush="true" %> [by default]
|session||When we want to make 'n' number of independent requests as consecutive requests one must use the concept of session. In order to maintain the session we must give the value of session attribute has true in each and every JSP page (recommended). The default value of session is true which represents the session is applicable to current JSP page.
<%@ page session="true" %>- session will be created or old session will be continued.
|info||Using this attribute it is recommended for the JSP programmer to specify functionality about a JSP page, on what date it is created and author.|
Write a JSP page which illustrates the concept of isErrorPage and errorPage?
<%@ page isErrorPage="false" errorPage="ErrorPage.jsp" %> <html> <body> <%= 30 / 0%> </body> </html>
<%@ page isErrorPage="true" %> <html> <body> Exception is <%= exception%> generated...<br> Exception message is <%= exception.getMessage()%><br> <%@ include file="copyright.html" %> </body> </html>
<html> <br> <br> <br> <em>All copy rights are reserved for kvr.com<em> </html>
Develop JSP pages which will participate in session?
<%@ page session="true" %> <html> <body> <form name="first" action="Second.jsp"> Enter your name : <input type="text" name="first_name"><br> <input type="submit" value="Send"> </form> </body> </html>
<%@ page session="true" %> <html> <body> <% String str = request.getParameter("first_name");%> Your name is <h4><%= str%></h4> <% session.setAttribute("name", str);%> <form action="Third.jsp"><br> Send the request to next page<br> <input type="submit" value="Send"> </form> </body> </html>
<%@ page session="true" %> <html> <body> Value in third page from session objects is <h4><%= session.getAttribute("name")%></h4> </body> </html>
<% include file = "file name to be included" %>
<% include file = "copyright.html" %>