Wednesday, April 6, 2011

Chapter 35: JSP Page Lifecycle

Every JSP Page has a lifecycle, just like a servlet. After all, a JSP Page gets converted into a servlet before it is executed and hence it is obvious that the JSP Page would have a life-cycle. In this chapter, we are going to see just that.

So, lets get started!!!

The JSP Life-Cycle

The JSP page lifecycle is on the exam. A JSP's life starts when it is first requested. The Servlet Container parses the JSP, creates a servlet, runs the servlet, handles responding to the request, and manages servlet persistence. Smart containers place the servlet in memory only once upon the first request to speed up processing.

Though the life of a JSP looks simple, you might find it better if I put it all in a table for you to refer quickly. Well here we are:

JSP Life-Cycle Step Description
Page translation < % = expression % > Translates this to a string. It is equivalent to out.print(expression);.
Page compilation < % yourCode % > Compiles this.
Load class Loads the JSP page's servlet class upon first request.
Create instance Instantiates an instance of the servlet class.
Call jspInit Initializes the servlet instance by calling the jspInit method.
Call _jspService Invokes the _jspService method, passing a request and a response object.
Call jspDestroy If the container needs to remove the JSP page's servlet, it calls the jspDestroy method.
A JSP page is always converted to a servlet. Therefore the lifecycle of a JSP page is largely determined by how the JSP container handles Java servlets.

Each JSP page is eventually converted to a servlet class and then compiled. Each time a request is sent to a JSP page, the container compares the file dates. If the JSP is younger than the servlet, the container recompiles it and then sends the request to the servlet. The compiling process is performed automatically when the server receives a request for that page.

There are two phases of a JSP page's life: translation and execution. In the translation phase, the container starts building the final page, adding contents from any includes and skipping JSP comments (but retaining HTML comments). The container interprets all the Java code, such as directives, actions, and the custom actions referencing tag libraries that occur in the page. Once this is done, the container compiles the result into a servlet class. This completes the translation phase and the servlet is ready to receive requests.

The execution phase involves instantiating request and response objects and invoking the correct servlet based on the request. After the servlet finishes its work, it hands the response object to the container. The container then sends the response back to the client.

Exam Trivia:
The JSP Remains a Static Page Until Requested! The translation and compilation phases can yield errors that are seen only when the page is requested for the first time.

Previous Chapter: Chapter 34 - The Page Directive

Next Chapter: Chapter 36 - JSP Implicit Objects

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