Google

Dec 10, 2011

JSF Interview Questions and Answers: postback and viewstate

Q. What is the difference between initial request and postback?
A. Initial request (e.g. HTTP GET) is the request that is made from a browser in order to display a page. Postback happens when the browser posts the page back to the server with form values, etc. Initial request is created by clicking a link, pasting an URL in address bar, while a postback request is create by posting a form by clicking a submit button or any post request. Initial request passes only restore View & Render Response phases, while postback request process under all phases described in the JSF life cycle diagram.


During the restore view phase of the life cycle, ViewHandler retrieves the ResponseStateManager object in order to test if the request is a postback or an initial request. If a request is a postback, the restoreView method of ViewHandler is called. This method uses the ResponseStateManager object to re-build the component tree and restore state.
The ResponseStateManager object is the only one that knows what rendering technology is being used and is therefore the only one that can look at a request, which is rendering-technology specifiec. Here are the basic steps.

  • An isPostBack method on ResponseStateManager returns true if the current request is a postback.
  • A getState method is called by the restoreView method of ViewHandler to retrieve the component tree state from the current request.
  • A writeState method that writes out the state to the client. This method is called by the renderView method of ViewHandler during the render response phase.

Q. What is a viewstate in JSF?
A. In JSF, there is a viewstate associated with each page, which is passed back and forth with each submits. The reason for the viewtate is that the HTTP is a stateless protocol. The state of the components across requests need to be maintained. The viewstate can change in between requests as new controls like UIInput can be added or modified. The view state is divided into two parts.

Part 1: Structure of the components
Part 2: Defines the state of the components. For example. enabled/disabled, input values, checked/unchecked, selected item, etc.

Understanding the JSF lifecycle helps understand the viewstate.



It can be achieved by either storing the viewstate on the server side in a session and then passing the viewstate id to the client via a hidden field as shown below:




Server side: In web.xml file, set the state saving strategy

<context-param>
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
     <param-value>server</param-value>
</context-param>

The hidden field passed to the client

<input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="cdx43wedsfdg654ed" />

Client side: serializing the view state on the client. Do the following in web.xml

<context-param>
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
     <param-value>client</param-value>
</context-param>


More JSF Interview Questions and Answers

Labels:

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home