0% found this document useful (0 votes)
69 views27 pages

JSP Directives: Advance Java

Here are the steps to create the application: 1. Create a JSP page called index.jsp with the following code: <%@ page import="java.util.Date" %> <%@ page errorPage="error.jsp" %> <%@ page session="true"%> <%@ page buffer="8kb"%> <form action="greet.jsp"> Name: <input type="text" name="name"><br> <input type="submit" value="Submit"> </form> 2. Create another JSP page called greet.jsp <%@ page isErrorPage="true" %> <% String name = request.

Uploaded by

aaaaaaa2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views27 pages

JSP Directives: Advance Java

Here are the steps to create the application: 1. Create a JSP page called index.jsp with the following code: <%@ page import="java.util.Date" %> <%@ page errorPage="error.jsp" %> <%@ page session="true"%> <%@ page buffer="8kb"%> <form action="greet.jsp"> Name: <input type="text" name="name"><br> <input type="submit" value="Submit"> </form> 2. Create another JSP page called greet.jsp <%@ page isErrorPage="true" %> <% String name = request.

Uploaded by

aaaaaaa2010
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Advance Java

JSP Directives

LEVEL – PRACTITIONER
About the Author

Created By: Renjith(t-renjith)/ Shanmu (105110)

Credential Trainer / Sr Architect


Information:

Version and 1.0, January 12’th 2012


Date:

2
Icons Used

Hands on
Questions Tools Exercise

Coding Test Your Case Study


Standards Understanding

Best Practices
Demonstration & Industry
Workshop
Standards

3
Objectives

After completing this chapter you will be able to understand:


 What is JSP directive
Type of Directives

4
What is a JSP Directive ?

 A directive provides meta data information about the JSP file to the web
container.

 Web container uses this during the translation/compilation phase of the


JSP life cycle.

Examples:

 Importing tag libraries

 Import required classes

 Set output buffering options

 Include content from external files


5
Types of Directives

The JSP specification defines three directives,

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.

NOTE: Taglib directive will be explained in detail in JSP custom


tags session.

6
JSP Directive Syntax

A JSP directive is declared using the following syntax.

<%@directive attribute="value" %>

Where

 directive – The type of directive (page ,taglib or include)

 attribute – Represents the behavior to be set for the directive to act


upon.

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…

Confused on seeing so many directives?

Just relax and focus on the following 4 directives


which are commonly used in application development.

Buffer
Error Page
Import
Session

Lets do some hands on these directives.

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?

Include directive is created using the following syntax

<%@include attribute =“value”%>

Include can have only file attribute.

<%@include file =“value”%>

Where file specifies the relative path of the file to be included.

Example :

<%@include file =“header.html”%>

Includes a file named header.html


15
Lend a Hand – Page and Include Directive

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

Scenario : You are asked to create an application which accepts a person’s


name and prints a greeting . If the name user input is null the system should
display an error page.
Develop the components as given below
 welcome.jsp : The page which accepts the name from the user
 errorPage.jsp : The page to be displayed in case of error
 greetings.jsp : The page which displays the greetings to the user
 header.html :- The header for all the pages which can be reused across
pages
 footer.html :- The footer for all the pages which can be reused across
pages
17
Lend a Hand :Welcome Page design

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.

The main page


containing the form
control.

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

Greetings page should be created as shown.

20
Lend a Hand – Develop Greetings page.

The session , buffer , errorPage , import


attributes are set for the current page
using the page directive

Throw Exception if
name is null

Includes header.html

Includes footer.html

21
Lend a Hand – Error Page Design

Error page should be created as shown below

22
Lend a Hand – Develop Error Page

isErrorPage attribute is set

Implicit object exception is


available since isErrorPage
attribute is set true.

23
Lend a Hand – Develop Header and Footer

Header.html

footer.html

24
Lend a Hand – Deploy and Run

Step 1 : Deploy and Run the application


Step 2 : Invoke welcome.jsp from the browser
http://localhost:5001/DirectivesDemo/welcome.jsp
The welcome page displayed with header and footer included.
Step 3 : Enter name and submit
Greetings page will be displayed with the greeting message.
Step 4 : Invoke the welcome page again from the browser . Submit the page
without entering the name. Error page will be displayed.

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

Associates to quickly summarize the following before ending the session


 What are JSP directives?
 What directive is used to set the error page for a jsp page?
 What directive is used for importing packages into a jsp page?
 How to disable session for a jsp page?
 What directive is used for including external pages into a JSP
page?
26
Advance Java

You have successfully completed –


JSP Directives

You might also like