JSP Directives: Advance Java
JSP Directives: Advance Java
JSP Directives
LEVEL – PRACTITIONER
About the Author
2
Icons Used
Hands on
Questions Tools Exercise
Best Practices
Demonstration & Industry
Workshop
Standards
3
Objectives
4
What is a JSP Directive ?
A directive provides meta data information about the JSP file to the web
container.
Examples:
Directive Purpose
Page Provides information about page, such as scripting
language that is used, content type, or buffer size
etc.
Include Used to include the content of external files.
Taglib Used to import custom tags defined in tag libraries.
Custom tags are typically developed by developers.
6
JSP Directive Syntax
Where
7
Page Directive
The page directive is used to provide the metadata about the JSP page to
the container.
Page directives may be coded anywhere in JSP page.
By standards, page directives are coded at the top of the JSP page.
A page can have any number of page directives .
Any attribute except the import attribute can be used only once in a JSP
page.
Single can contain more than one attribute specified.
Syntax :
<%@page attribute1=“value” attribute2=“value” %>
8
Attributes for Page Directive
Attribute Purpose
Specifies a buffering model for the output stream. Same as the servlet buffer.
buffer
<%@ page buffer="none|8kb|sizekb" %>
Controls the behavior of the servlet output buffer.
autoFlush
<%@ page autoFlush=“True\False" %>
Defines the character encoding scheme.
contentType
<%@ page contentType="text/html;charset=ISO-8859-1" %>
The errorPage directive takes a valid relative URL to a JSP file to which the
errorPage control is redirected in case of any exceptions..
<%@ page errorPage="relativeURL" %>
Works in tandem with the page errorPage directive and specifies that this JSP is
isErrorPage an error page.
<%@ page isErrorPage="true|false" %>
9
Attributes for Page Directive
Attribute Purpose
Specifies a super class that the generated servlet must extend
extends
<%@page extends="myServletClass"%>
Specifies a list of packages or classes to be imported for JSP to use similar to
java import..
import <%@page import="java.util.Date , java.io.FileReader"%>
More than one package can be imported either by using separate page
directive or using comma separated list in a single directive.
Defines a string that can be accessed with the servlet's getServletInfo()
info method.
<%@ page info=“information" %>
Defines the threading model for the generated Servlet.
isThreadSafe
<%@ page isThreadSafe="True|False" %>
10
Attributes for Page Directive
Attribute Purpose
Defines the programming language used in the JSP page.
language
<%@page language="java" %>
Specifies whether or not the JSP page participates in HTTP sessions
session
<%@page language="java" session="true" %>
Specifies whether or not EL expression within the JSP page will be
isELIgnored ignored.
<%@ page isELIgnored="True|False" %>
Determines if scripting elements are allowed for use.
<%@ page isScriptingEnabled="True|False" %>
isScriptingEnabled
Setting to false will throw error during translation if your page contains
any scripting element such as scriplets , expression etc
11
Are you confused…
Buffer
Error Page
Import
Session
12
Include Directive
This directive inserts a HTML file or a JSP file into another JSP file at translation
time as illustrated below, User views the combined
output of both the pages.
User requests for
mypage.jsp
R R
E
E
3 During the translation phase
header.jsp Q S
U P the contents of header.jsp
O
E
N
are translated along with
2 S
Includes T S mypage.jsp , gets included
E inside mypage_jsp.java as a
1
single file
mypage.jsp mypage_jsp.java
Translation
mypage.jsp
+
Header.jsp
13
More on Include Directive
The include process is static, it means that the text of the included file is
added to the JSP file. (Similar to copy pasting the contents).
The included file can be a JSP file, HTML file, or text file.
If the included page is a JSP page it will be translated along with the main
JSP page.
Note :
Be careful that the included file should not contain <html>, </html>,
<body>, or </body> tags.
Because the entire content of the included file is added to the main JSP file,
these tags would conflict with the same tags in the main JSP file, causing
an error.
14
How to create an include directive?
Example :
In this demo the associates will get familiarized with some of the commonly
used page directives and the include directive.
We will be using the following page directive attributes and include directive
in this demo
1. import
2. errorPage
3. isErrorPage
4. Session
5. buffer
16
Lend a Hand :Page and Include Directive
Create welcome.jsp as shown below . The page should contain the following fields,
1 : Text box to accept the name
2 : Submit button for form submit.
Header page to be
included in all the
pages.
Footer page to be
included in all the
pages.
18
Lend a Hand : Develop Welcome Page
Includes header.html
Includes footer.html
19
Lend a Hand :Greetings Page design
20
Lend a Hand – Develop Greetings page.
Throw Exception if
name is null
Includes header.html
Includes footer.html
21
Lend a Hand – Error Page Design
22
Lend a Hand – Develop Error Page
23
Lend a Hand – Develop Header and Footer
Header.html
footer.html
24
Lend a Hand – Deploy and Run
Note : In case error page is not getting displayed in Internet Explorer uncheck the
option “ show friendly error message “ under the advanced tab in internet option.
25
Time To Reflect