Wednesday, April 6, 2011

Chapter 34: The Page Directive

The Page Directive is one of the important components of any JSP Page. It can help us define page specific properties like Buffer size or location of an error page etc. In this chapter we are going to learn about this directive in detail.

So, lets get started!!!

The Page Directive

A JSP page, and any files included via the include directive, can contain one or more page directives but no duplicates. The JSP container will apply all the attributes to the page. The position of these page directives is irrelevant, but it is good practice to keep them together at the top of the page. (So that we can identify them easily)

While you can't duplicate any of the other attributes (which would result in a fatal translation error), you can make multiple uses of the import attribute, the only exception to the no-duplicate rule.

The following is the page directive syntax:

<%@ page

[ language="java" ]

[ extends="package.class" ]

[ import="{package.class | package.*}, ..." ]

[ session="true|false" ]

[ buffer="none|8kb|sizekb" ]

[ autoFlush="true|false" ]

[ isThreadSafe="true|false" ]

[ info="text" ]

[ errorPage="relativeURL" ]

[ contentType="mimeType [ ; charset=characterSet ]" |

"text/html ; charset=ISO-8859-1" ]

[ isErrorPage="true|false" ]

[ pageEncoding="characterSet | ISO-8859-1" ]

%>


While we cover all page directive attributes for completeness, the test objectives specifically name the import, session, isErrorPage, and errorPage attributes, so these are covered in comparatively more detail than the rest.

Importing Classes

Like in normal Java, you import classes inside a JSP page to gain access to them. These imports are cumulative, both in Java and JSP. The import attribute of the page directive is how you import Java classes into a JSP page.

The following is an example of how you would import several packages:

<%@ page import="java.io.*,java.util.*,com.myCompany.*" %>

You can use many page directives or just one with a comma separated list of packages or classes as the value of the import attribute. Like an import declaration in the Java programming language, the value is either a fully qualified type name or a package name followed by the ".*" string, denoting all the public types declared in that package.

Exam Trivia:
In JSP, you do not have to use the page directive for the default import list of java.lang.*, javax.servlet.*, javax.servlet.jsp.*, and javax.servlet.http.*. These are automatically available to you. (Just like in regular Java programs where java.lang.* is available by default.

Declare That a JSP Page Exists Within a Session

The session attribute of the Page Directive indicates whether the page is part of a session. For Ex:
<%@ page session="true" %>

The JSP container does all the work for you. The Web server tracks sessions by storing a session identifier in a cookie. When this attribute is set to "true" (default), the implicit variable named “session” (javax.servlet.http.HttpSession) references the current session for the page. If it is set to "false" then the session object is not available for you to use within the JSP page and any reference to it within the body of the JSP page throws a fatal error. Although, being able to access the session in the JSP page is a good thing, one probably reason I can think of for setting this attribute to “false” is, to save a little memory and gain some performance.

Exam Trivia:
The Session being Null and the Session not being available because session attribute of the page directive was set to false are two different things.

Declare That a JSP Page Uses an Error Page

The errorPage attribute defines a URL that is processed when any throwable object is thrown but not caught by a try-catch block in the page, like so:

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

This is actually a pretty nice feature because, in case anything unexpected happens in your JSP Page, the system knows where to redirect the control to and does not end up throwing a weird looking error trace on the page. Instead, a neat error page is shown.

The throwable object is transferred from the JSP page that generates it to the error page. Internally, it does this by saving the object reference on the common ServletRequest object using the setAttribute() method, with a name of “javax.servlet.jsp.jspException”.

Exam Trivia:
Using autoFlush If autoFlush="true" and the contents of the initial JspWriter have been flushed to the ServletResponse output stream, any subsequent attempt to dispatch an uncaught exception from the offending page to an errorPage may fail. When an error page is also indicated in the web.xml descriptor, the JSP error page applies first, then the web.xml page.

Declare That a JSP Page Is an Error Page

The isErrorPage attribute tells the container if the current page is available to be an error page for another JSP page (the current page is the URL in another page's errorPage attribute). Ex:
<%@ page isErrorPage="true" %>

If this attribute is set to "true" then the variable “exception” is available to you. Otherwise (default is "false"), if you try to reference the exception, you will get a fatal error.

Language Attribute

The language attribute defines the scripting language to be used in the scriptlets, expression scriptlets, and declarations. Ex:

<%@ page language="java" %>

Notice that this declaration also applies to any files included using the include directive. In JSP 1.2, the value for this attribute is "java" and cannot be anything else.

Extends Attribute

The extends attribute works just like it does in regular Java. It allows you to inherit a class by naming the superclass of the class created by runtime compiling this JSP page. Ex:
<%@ page extends="package.class" %>

Use this attribute sparingly because it allows the developer to circumvent the JSP engine.

Buffer Size Attribute

This attribute tells the JSP container how much space to allocate for the initial buffer ("out", which is the JspWriter). Ex:

<%@ page buffer="16kb" %>

You should note that when the buffer is full, it writes the content in the buffer to the output stream. If this attribute is set to "none", all output is written directly to the output stream. The size is set in kilobytes (the suffix “KB” is required). If this attribute is not explicitly set, the page starts with a default size of 8KB.

autoFlush Attribute

The autoFlush attribute tells the JSP container whether the buffered output should be flushed. Ex:
<%@ page autoFlush="true" %>

Exam Trivia:
Do Not Set autoFlush to "false" When the Buffer Is Set to None! It is illegal to set autoFlush to "false" when the buffer is set to none. Also, if autoFlush is "true" and the buffer gets flushed, any subsequent uncaught exception will likely fail to return the specified errorPage reference.

When it is set to "true", the container automatically sends the contents of the buffer to the output stream when the buffer becomes full.

The default setting is "true". You would want to set this to "false" if you wanted to raise an exception to indicate a buffer overflow condition.

isThreadSafe Attribute

The isThreadSafe attribute is used as follows:
<%@ page isThreadSafe="true" %>

The JSP container uses this attribute to handle a page's level of thread safety. If this is set to "false", then the JSP container will service multiple client requests one at a time. Otherwise it will allow simultaneous access to the same page. The "true" (default) setting assumes you have synchronized the page elements.

Info attribute

The info attribute stores an arbitrary string that can subsequently be retrieved with the Servlet.getServletInfo() method. It is used like so:

<%@ page info="myPageInfo" %>

contentType Attribute

The contentType attribute tells the browser what kind of page it is. This attribute specifies the MIME type of the page's output. The output type is included in the HTTP header prior to data being printed to the client. The syntax is

<%@ page contentType="text/html; charset=ISO-8859-1" %>

The default is text ("text/html"), but you can specify many other types. The different types you can set are:
1. application/msword – For Microsoft Word Documents
2. application/pdf – For Acrobat pdf files
3. application/vnd.ms-excel – For Microsoft Excel Spreadsheets

You cannot use MIME types that are binary format (like GZIP) as these cannot be constructed in a JSP file. Explaining all the possible MIME types would be an overkill and is definitely not part of the SCWCD Exam so we will not be covering them. However, if you are an enthusiast and want to learn it, you can visit http://www.oac.uci.edu/indiv/ehood/MIME/MIME.html for the full list of MIME types.

Let us look at the web.xml file in CATALINA_HOME/conf/web.xml for the MIME types that all Web applications will use before we wrap up this chapter. The following is a sample code snippet declaring two MIME types for the jpeg image file:
< mime-mapping >
< extension >jpeg< / extension >
< mime-type >image/jpeg< / mime-type >
< / mime-mapping >
< mime-mapping >
< extension >jpg< / extension >
< mime-type >image/jpeg< / mime-type >
< / mime-mapping >

Previous Chapter: Chapter 33 - JSP Tags & XML

Next Chapter: Chapter 35 - JSP Lifecycle

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers