JSP Directive and Objects

clangpotatoSoftware and s/w Development

Oct 28, 2013 (3 years and 9 months ago)

65 views

JSP Directive and Objects

JSP Directives

As discussed before

There are 3 types of elements in JSP


Directive

Elements


Scripting

Elements


Standard Action

Elements


We will be learning about JSP Directives

What are Directives

Q. What are directive?

Ans. Directives give directions or messages.


In JSP, directive
give directions

to the JSP
Container.

Directive tell JSP Container how to translate
a JSP page into Servlet

Types of Directive

<%@
directive

(attribute="value")* %>


OR

<%@
directive

attribute1="value1" attribute2="value2" ... %>


Where directive is the name of directive
.


There are 3 types of Directive in JSP

1.Page Directives
-

<%@ page att1=“value1” att2=“value2”>

2.Include Directives
-

<%@ include att1=“value1” att2=“value2”>

3.Tag Library Directives


For creating custom tags.

<%@ page %>

Page directive:

Syntax for using page directive is

<%@ page attr1=value1 attr2=value2 ….%>


So what all attributes are available?

Attributes of page directive

The following attributes can be used with page directive

info

contentType

pageEncoding

language

import

session

buffer

autoFlush

errorPage

isErrorPage

Attribute info of page directive

Syntax

<%@ page info=“this page calculates sum” %>


-
This attribute is not important.

-

It is just a text string informing the purpose of
JSP page.

Attribute contentType of page
directive

contentType


specifies the kind of data returned by browser.

A browse can return text , html, xml.

By default

<%@ page contentType=“text/html” %>

But if you write a program where
browser returns
xml
, then

<%@ page contentType="text/xml" %>



Attribute pageEncoding of page
directive

pageEncoding is used if you want to display characters of
some other language.


For ex:

If you develop a web site that is in hangul, and I open that
web site from an english computer

In that case, browser would not display characters properly

But if I use pageEncoding then browser will display
characters propertly


Syntax:

<%@ page pageEncoding=“ISO
-
8859
-
1” %>

Attribute language of page directive


Language


Is used to specify the scripting language.


By default is is Java.


<%@ page language = “java” %>

You can use javascript also

In that case

<%@ page language=“javascript” %>


Attribute
import

of page directive

import


It is very important

In previous example,I used
Calendar

class

In another example, I used
Enumerator

class.

When I used Enumerator class, I wrote

java.util.Enumerator

If I use
<%@ page import=“java.util.*”
, then

I can use only Enumerator in JSP page.

Attribute
session

of page directive


session


This attribute is used to specify the scope of
objects and variables.

Syntax

<%@ page session=“true” %>

Default value of session attribute is true.

We will use session attribute with JavaBeans.

Attribute
errorPage
and

isErrorPage

of page directive


errorPage attribute tells the JSP which
page to display when an error occurs.


If there is no errorPage used, then the error
are displayed.

But If we want, we can display a custom
error page.

Custom error page will display error in a
beautiful manner.

Continued….


Syntax

<%@ page errorPage="handleError.jsp" %>


If I use this statemetn in my JSP file, then

If any error occurs, handleError.jsp will be
displayed on browser.

isErrorPage


errorPage and isErrorPage are used
together.


The web page handleError.jsp first line will
be

<% page isErrorPage=“true” %>

This means, handleError.jsp is an error
page.

It will be displayed when an error occurs.

Example of errorPage and
isErrorPage


Code of ErrorPage.jsp

<%@ page isErrorPage="true" %>


<html>



<body>




<h1>Error</h1>




An error occurred while processing your

request.

<p>


The error message is:
<%= exception.getMessage() %>.


</p>

</body>

</html

Continued


SourceCode for hello.jsp

<%@ page errorPage="ErrorPage.jsp" %> <html>


<body>

<%

String str = new
Sring
(“hello”);


%>

</body>


</html>

This is wrong,

therefore ErrorPage.jsp should be
displayed