Group Discounts available for 3+ students and Corporate Clients

Creation and working with First Servlet in JSF

 Faces servlet 

In general in MVC based web appl? The mole of the controller is

  1. Pick of the request from client
  2. Identify model component
  3. Perform model component loading instantiation data population
  4. Access application logic
  5. Identify target view page
  6. Forward request to target view page
  • JSF is MVC based web application f/w, it has provided javax.feices webapp faces serve let as. Controller
  • JSF frame work has provided faces servlet as an implementation class to javax. Servlet. Servlet inter face
  • In JSF, FACE SERVLET has provided the implementation for the following methods from servlet interface
  • public void init (servlet config config)throws servlet exception
  • public void service(servlet request , servlet response ) throws servlet exception, ID exception
  • public servlet config get servlet config get()
  • public String Get Servlet Into()
  • public void destroy()

Screenshot_9

Above diagram

  • When we start the server the main job of the container is to recognize each and every web application and it will prepare a separate servlet context object for each and every web app.
  • While recognizing web app container will identify web.xml file then container perform web.xml file loading. Parsing and reading the content.
  • If any application level data is identified then container will sore that data in servlet context objects
  • while reading web.xml file content, it container identify “load on startup” configuration under any servlet then containers will perform that servlet loading instantiation and initialization at the time at servlet start up only
  • In JSF based web application, containers will identify startup configuration under faces servlet configuration with this will search for javax faces web app. Faces servlet class file under classes folder if it is not available then container will search for it  all this jar files provided in “lib” folder
  • In the above context container will identify faces servlet under JSF- ape.jar file then container perform face servlet loading instantiation and initialization at the time of server stat up
  • In faces servlet initialization faces servlet will recognize faces config xml file under WEB-INF folder then faces servlet will perform faces – config .xml file loading parsing
  • After faces- config.xml file parsing .faces servlet will prepare “lifecycle” object
  • After the server startup we have to access a page at client browser, we have to provide data in respective fields and we have to submit request from client browser. To server or any hyperlink or by selecting check boxes or radio buttons. Etc
  • When request is submitted to server container will identify the requested resource is faces servlet through web.xml file and container will execute service method of faces servlet
  • When container bypass request to faces servlet, faces servlet will remark a separate object with the present request called as “faces context” object to store the data which is associated with current request.
  • After preparing ”faces context” object container will invoke life cycle object where lifecycle object will process the request by following ”JSF Request processing lifecycle”
  1. Restore view
  2. Apply request values
  • Process validates
  1. Update model
  2. Invoke application
  3. Render response

 

I) Restore view

  1. This phase will check whether the current request is fore any resource or action. If the current request is for any resource then faces servlet will identify the requested resource and by pass the flow of execution directly or render response phase
  2.  IF the present request is for any particular action then faces servlet will perform the following actions

i)Faces servlet will check any ”component free” is existed the present request or not, if any component free is existed then faces servlet will retrieve it

ii)It  no component tree is existed w.r.to the present request then Faces servlet has to create new component tree. While preparing component tree Faces servlet will perform the following action

  1. Faces servlet will create a root node for <f:view> tag in component tree and child nodes for each and every JSF component Which are available in the respective page
  2. Faces servlet will store view id value in “UIVIEW ROOT” Object which is compiling along with request.
  3. Faces servlet will store all the  incoming ”locate” provided by client browser in “UIVIEW ROOT” node.
  4. if any Validator are configured for UI Components in page then faces servlet will register all the Validator to the components in component tree.
  5. If any converters are configured for UI Component in page then faces servlet will register all the converters to the components in component tree.
  6. It any listeners are configured fore UI Components in page then faces servlet will register all the listeners to component in component tree.
  7. Faces servlet will identify respective model component or Bean component w.r.to the present request through Faces- config.xml file and perform bean loading and instantiation at the time of bean instantiation default values or any initial values will be stored in bean properties these initial values will be set to the respective components in component tree.
  8. After preparing component tree, Faces servlet will store other component tree in faces context object and Http Session Object

 

  1. Apply Request values
  • In this phase faces servlet will take request parameter values from request object and it will set request parameter values to the corresponding components in component tree
  • To set request parameter values to component tree the following method from UI view root:- Public void process Decode()

 

  1. Process Validator
  1. In this phase, faces servlet will perform the following actions:
  2. Faces servlet will search fore registered Validator at each and every component in component tree
  3. If any Validator is identified at component then faces servlet will execute all the Validator
  4. It any validation error messages are available then it will add validation error messages to “Faces context” object.
  5. faces servlet will search for the registered convectors at each and every component in component tree
  6. It any convertor is identified then faces servlet will execute that convertors
  7. If any conversion errors are identified then faces servlet will check any error message is available in faces context object,
  8. It any error message is identified then faces servlet will bypass request to “Render Response” phase directly
  9. It no error message is identified in faces context object then faces servlet will bypass request to the next phase id “update model”
    To perform process Validator phase, faces servlet will execute the following method from UI view root class Public void process Validator()

 

  1. Update model:

In apply request values phase, all the request parameter values we were set to the components which are available in component tree

  • In this process Validator phase, we have checked weather the data is valid or not
  • After the data validations and conversions the data which is available in component tree is true data, which we have to use in application logic. To use this data in application logic, first we have to set this data to the properties of model component
  • Update model phase will set trustable data from component tree to the model properties.
  • To perform this phase, faces servlet will execute the following method. Public void process updates()

 

  1. INVOKE APPLICATION

In this phase, faces servlet will perform the following actions.

  1. Faces servlet will get the registered method name from the components like button, hyperlink, check box, radio button,…from component tree.
  2. Faces servlet will execute the respective method in the respective managed bean component.
  3.  If no return value is identified from managed bean component method then faces servlet will bypass request to “Render Response” phase to maintain the same page at client browser.
  4. It any return value is identified from the respective managed Bean component method then faces servlet will identify the target view page from faces – config. Xml file and by pass request to “Render Response” phase to generate response
  • To perform this phase, faces servlet will execute the following method

Public void process application()

 

  1. Render Response phase 

In this phase, Faces servlet will convert the respective component tree into the equivalent html or xml or wml and etc formations in order to send response to client browser in html or xml or WML,…formats

  • It the request is coming from “Restore View” phase then render response phase will send the requested resource as response to client.
  • If the request is coming from “Process Validator” phase then render response phase will get all the validation and conversion error messages from “Faces context” Object and send the same page as response to client by embedding error messages.
  • It the request is coming from invoke application phase then render response phase will send the result page as response to client.
  • To perform this phase, faces servlet will execute

 

4.Model Component

In MVC based web applications, the main purpose at model component is,

  1. To manage application B. logic
  2. To manage users from data in order to perform data validations and conversions,
  3. To manage users from data in order to perform the database operations like insert, update, retrieve,….
  4. To transferee the data from controller layer to view later.

In JSF web applications, Managed bean component is acting as model

To prepare managed bean component in JSF based web applications, we have to use the following guide lines

  1. In JSF, Managed bean component should be a POJO class, it should not be extended or implemented from any predefined library.
  2. In JSF based applications, Managed bean class must be public, non abstract and no final
  3. All the properties in managed bean class must match the properties name which we defined along with UI Components in user forms
  4. We must provide set..()methods and getter..() methods for each and every bean property
  5. We have to declare business methods which are registered with the GUI Components like buttons, hyperlinks,…
  6. If we want to provide any construction in managed Bean class then that construct fore must be “Public” and “O-argument”.
  7. Define application logic by declaring business methods

 

Ex

Public class Login bean

{

Private string u name;

Private string u pwd;

Public void set u name(string u name)

{

This. U name = u name;

}

Public void set upwd(String upwd)

{

This. Upwd= upwd;

}

Public string get u name()

{

Return u name;

}

Public string get u pwd()

{

Return u pwd;

}

Public string log in()

{

— logx—

Return “Success”;

}

}

 

Faces configuration File

The main purpose of  faces configuration file is to provide the following con fig  details request red by faces servlet.

  1. Bean component configuration
  2. Navigation Rules
  3. Validators
  4. Convertors
  5. Listeners
  6. Local configuration.

The default name and location of faces configuration file is “Faces config.xml” file under WEB.INF folder

  • In JSF based web application, it is possible to change the name and location of faces configuration file but we have to configure that name and location under web. In the form of a context parameter
  • Here context parameter name is fixed is Javax. faces.

CONFIG –FILE

<Web – app>

<Context. parm>

<Param.name>Javax. faces. CONFIG.FILE</paran-bane>

<Param. Value>/WEB- INF/classes /my config.xml

<Context param

—-

<\web.app>

  • In basic JSF application, we need only bean configuration and navigation configurations in faces configuration file

 

2faces-config>

—–

<managed- bean>

<managed- bean-name> bean logical name</managed-bean- name>

<managed- bean-class> bean fully qualified name  </managed-bean- class >

<managed- bean-score>session/application

</managed-bean- scope >

</managed-bean >

<navigation mull>

<from-view-id>/form page</from-view-id>

<navigation – case>

<from- out come> return value from bean method</from – out come>

<to- view- id>/ to page </to-view. id>

<\navigation-case>

<\navigation-mule>

</faces-config>

 

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Creation and working with First Servlet in JSF"

    Leave a Message

    Your email address will not be published. Required fields are marked *

    Support


    Please Enter Your Details and Query.
    Three + 6