Creation and working with First Servlet in JSF
In general in MVC based web appl? The mole of the controller is
- Pick of the request from client
- Identify model component
- Perform model component loading instantiation data population
- Access application logic
- Identify target view page
- 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()
- 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”
- Restore view
- Apply request values
- Process validates
- Update model
- Invoke application
- Render response
I) Restore view
- 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
- 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
- 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
- Faces servlet will store view id value in “UIVIEW ROOT” Object which is compiling along with request.
- Faces servlet will store all the incoming ”locate” provided by client browser in “UIVIEW ROOT” node.
- if any Validator are configured for UI Components in page then faces servlet will register all the Validator to the components in component tree.
- If any converters are configured for UI Component in page then faces servlet will register all the converters to the components in component tree.
- It any listeners are configured fore UI Components in page then faces servlet will register all the listeners to component in component tree.
- 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.
- After preparing component tree, Faces servlet will store other component tree in faces context object and Http Session Object
- 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()
- Process Validator
- In this phase, faces servlet will perform the following actions:
- Faces servlet will search fore registered Validator at each and every component in component tree
- If any Validator is identified at component then faces servlet will execute all the Validator
- It any validation error messages are available then it will add validation error messages to “Faces context” object.
- faces servlet will search for the registered convectors at each and every component in component tree
- It any convertor is identified then faces servlet will execute that convertors
- If any conversion errors are identified then faces servlet will check any error message is available in faces context object,
- It any error message is identified then faces servlet will bypass request to “Render Response” phase directly
- 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()
- 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()
- INVOKE APPLICATION
In this phase, faces servlet will perform the following actions.
- Faces servlet will get the registered method name from the components like button, hyperlink, check box, radio button,…from component tree.
- Faces servlet will execute the respective method in the respective managed bean component.
- 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.
- 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()
- 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
In MVC based web applications, the main purpose at model component is,
- To manage application B. logic
- To manage users from data in order to perform data validations and conversions,
- To manage users from data in order to perform the database operations like insert, update, retrieve,….
- 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
- In JSF, Managed bean component should be a POJO class, it should not be extended or implemented from any predefined library.
- In JSF based applications, Managed bean class must be public, non abstract and no final
- All the properties in managed bean class must match the properties name which we defined along with UI Components in user forms
- We must provide set..()methods and getter..() methods for each and every bean property
- We have to declare business methods which are registered with the GUI Components like buttons, hyperlinks,…
- If we want to provide any construction in managed Bean class then that construct fore must be “Public” and “O-argument”.
- Define application logic by declaring business methods
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()
Faces configuration File
The main purpose of faces configuration file is to provide the following con fig details request red by faces servlet.
- Bean component configuration
- Navigation Rules
- 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.
<Web - app>
<Param.name>Javax. faces. CONFIG.FILE</paran-bane>
<Param. Value>/WEB- INF/classes /my config.xml
- In basic JSF application, we need only bean configuration and navigation configurations in faces configuration file
<managed- bean-name> bean logical name</managed-bean- name>
<managed- bean-class> bean fully qualified name </managed-bean- class >
</managed-bean- scope >
<navigation – case>
<from- out come> return value from bean method</from – out come>
<to- view- id>/ to page </to-view. id>