JEE Custom Search Portals

Saturday, November 20, 2010


Apache Click in a Click - Part 3 [Servlet Request Life cycle]

This part of the blog will be detailing about the servlet request life cycle of apache click framework. In the previous blog post, [Apache Click in a Click - Part 2 [Servlet Loading Life cycle]], we detailed about the ClickServlet and its initialization life cycle. In the previous blog, we also discussed a little about the different configurations that can be done in click.xml [the application configuration file].

The following pictures shows the life cycle of a servlet request.
When a client (browser) makes a request, it is handled by the ClickServlet's doPost(...) or doGet(...) based on whether the request is a GET request or a POST request. This method in turn calls the handleRequest(...) method where the request is handled by Click.

The first step is to create a page instance based on request. For instantiating the page class, it should have a no argument constructor. So we should make sure that when we subclass the page class, we should have a no argument constructor defined in case we are writing another constructor with arguments.  Also during this process, the page attributes like context, header, format and locale will be set to the page instance. The next step is to bind the request parameter values to the matching page fields.

The page class provides the following empty handler methods which can be overridden by subclasses to implement specific functionality.
  • onSecurityCheck(): This method can be overridden by sub classes on a need basis. This can be used to make sure that the user is authorized to access a page and if necessary to abort further processing.

  • onInit(): This is the next method that is invoked during the life cycle. This is the method we can create the page controls [forms, fields, tables etc] that is required for the page. 
    • As the next step, each of the control's onit() method will be called to perform any initialization process. 
    • The next step is processing of the page's controls. Here the ClickServlet gets all the page controls and iterates through and calls the onProcess() method of each control. If the onProcess() method returns false for any of the controls, the processing of the subsequent controls and the Page's onGet() or onPost() is aborted. 
     
  • onGet(): Once the page's onInit() is executed properly, the next step in the process to call the Page's onGet() method. This is based on whether the request is a GET request.

  • onPost(): As stated above for onGet(), the Page's onPost() is called if the request is a POST request.

  • onRender(): The next step and the most important step is rendering the page template to generate the HTML for display. The ClickServlet gets the model (Map) from the Page then adds the following objects to the model:
    • All public Page fields will be added to the model with the key as the field property's name.
    • context - The Servlet context path or generally termed as context root. eg: /clickapp 
    • format - The Format object used for formatting the display of objects. This object can be used for formatting date, time, currency etc.
    • headElements - The HEAD elements, excluding JavaScript, to include in the page header.An example of this would be the following - 
      • CssImport (for importing external css using <link> element) 
      • CssStyle (for including inline css using <style> element)
    • jsElements - The JavaScript imports and script blocks to include in the pages footer.An example of this would be the following - 
      • JsImport (for importing external js files using the <script> element) 
      • JsScript (for including the inline js using the <script> element)
    • messages - This is the instance of the MessagesMap which is made available to every Page and controls. 
      • We can access the message from a page using the Page's getMessage() methd.
      • We can also get the messages on the page template like $messages.title
    • path - The path of the page template to render
    • request - The pages HttpServletRequest object
    • response - The pages HttpServletResponse object
    • session - This is the instance of SessionMap which is available in each Velocity page using the name "session"
      • For example suppose we have a User object in the session with the attribute name "user" when a user is logged on. We can display the users name in the page when the are logged onto the system.
     #if ($session.user)
       $session.user.fullname you are logged on.
     #else
       You are not logged on.
     #end 

    The next step is merging the template with the page model and writing out results to the HttpServletResponse. When the model is being merged with the template, any Controls in the model may be rendered using their toString() method.
  • onDestroy(): Once all the above steps happen, the onDestroy() method is called on each of the controls and then finally the page's onDestroy() method is called. This method can be used to clean up the resources associated with a control or a page before it is garbage collected. Also this method is guaranteed to run even if an error occurs in any of the above steps.
This completes the servlet request life cycle of an apache click application. In the next part of the blog, we will be concentrating on implementing a simple application with apache click.


2 comments: