Saturday, August 27, 2011

Notes for OCPJP

Section 7: Fundamentals

JSP
Jsps are servlets that are written in HTML, and is a server side web component, that can be used to generate dynamic web pages.
JSP technology makes available all the dynamic capabilities of Java Servlet technology but provides a more natural approach to creating static content. The main features of JSP technology are as follows:
• A language for developing JSP pages, which are text-based documents that
describe how to process a request and construct a response
• An expression language for accessing server-side objects
• Mechanisms for defining extensions to the JSP language
A JSP page is a text document that contains two types of text: static data, which
can be expressed in any text-based format (such as HTML, SVG, WML, and XML), and JSP elements, which construct dynamic content.
The recommended file extension for the source file of a JSP page is .jsp.

JSP Vs Servlet
Servlets generates HTML from Java code, where as Jsp embeds Java code inside static HTML.
Servlets are suited to decide to handle business logic, where as Jsp pages are well suited to display contents.

The MVC architecture
The MVC architecture is a widely used architectural approach for interactive applications that distributes functionality among application objects so as to minimize the degree of coupling between the objects. To achieve this, it divides applications into three layers: model, view, and controller. Each layer handles specific tasks and has responsibilities to the other layers:
• The model represents business data, along with business logic or operations that govern access and modification of this business data. The model notifies views when it changes and lets the view query the model about its state. It also lets the controller access application functionality encapsulated by the model.
• The view renders the contents of a model. It gets data from the model and specifies how that data should be presented. It updates data presentation when the model changes. A view also forwards user input to a controller.
• The controller defines application behavior. It dispatches user requests and selects views for presentation. It interprets user inputs and maps them into actions to be performed by the model. In a web application, user inputs are HTTP GET and POST requests. A controller selects the next view to display based on the user interactions and the outcome of the model operations.

Components of JSP
                 There are 3 main components of JSP
*HTML code
*JSP tags
*JSP implicit objects

1)HTML code
 This component of JSP page decides the static presentation layout of the page.

2)JSP tags
 This component is used to embed programming logic in HTML pages.They are broadly classified into six types.
•Declaration tags
•Expression tags
•Scriptlet tags
•Directive tags
•Comment tags
•Action tags

3)JSP Implicit objects
   There are 9 implicit objects
•request
•response
•pageContext
•session
•application
•out
•config
•page
•exception

Life cycle of JSP page
A JSP page services requests as a servlet. Thus, the life cycle and many of the capabilities of JSP pages (in particular the dynamic aspects) are determined by Java Servlet technology.
When a request is mapped to a JSP page, the web container first checks whether the JSP page's servlet is older than the JSP page. If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.
Event sequence of the JSP page life cycle:
 (1) JSP page translation
 (2) JSP page compilation
 (3) load class
 (4) create instance
 (5) call the jspInit method
 (6) call the _jspService method
 (7) call the jspDestroy method.

1. JSP Page Translation:
A java servlet file is generated from the JSP source file. This is the first step in its tedious multiple phase life cycle. In the translation phase, the container validates the syntactic correctness of the JSP pages and tag files. The container interprets the standard directives and actions, and the custom actions referencing tag libraries used in the page.

2. JSP Page Compilation:
The generated java servlet file is compiled into a java servlet class.
Note: The translation of a JSP source page into its implementation class can happen at any time between initial deployment of the JSP page into the JSP container and the receipt and processing of a client request for the target JSP page.

3. Class Loading:
The java servlet class that was compiled from the JSP source is loaded into the container.

4. Execution phase:
In the execution phase the container manages one or more instances of this class in response to requests and other events.
The interface JspPage contains jspInit() and jspDestroy(). The JSP specification has provided a special interface HttpJspPage for JSP pages serving HTTP requests and this interface contains _jspService().

5. Initialization:
jspInit() method is called immediately after the instance was created. It is called only once during JSP life cycle.

6. _jspService() execution:
This method is called for every request of this JSP during its life cycle. This is where it serves the purpose of creation. Oops! it has to pass through all the above steps to reach this phase. It passes the request and the response objects. _jspService() cannot be overridden.

7. jspDestroy() execution:
This method is called when this JSP is destroyed. With this call the servlet serves its purpose and submits itself to heaven (garbage collection). This is the end of jsp life cycle.
jspInit(), _jspService() and jspDestroy() are called the life cycle methods of the JSP.

1 comment:

  1. Good hierarchy of explanation in all your RoboGuice Tutorials. Can you please explain me jspDestroy() execution more clearly?

    ReplyDelete

MVC - MVP : Difference between these design patterns?

In traditional UI development - developer used to create a  View  using window or usercontrol or page and then write all logical code ...