Pages

Wednesday, 21 March 2012

Java Interview Questions

1.How to synchronize ArrayList?
    Ans:
            ArrayList<E> arrayListToSync = null; 
           // ArrayList elements addition
                 -----------
          List<E> SychronizedList= Collections.synchronizedList(arrayListToSync); 

Struts Interview Questions

1.What is the return type of validate method in ActionForm?
Ans: ActionErrors

2.What is the return type of reset method in ActionForm?
Ans: void

3.How to prevent multiple form submission?
Ans:
On initial request:
  1. call saveToken(request) in your Action class
  2. forward to the JSP displaying the form
On form submit:
  1. have the Action check if isTokenValid(request)
  2. if true, process request then call resetToken(); otherwise, we're dealing with a double-submit and will skip processing it.
What's happening under the hood:
  • Struts will generate a unique value (the token) and keep it in the session context
  • When the JSP is rendered, Struts inserts the token as a hidden field
  • The hidden field token is submitted along with the rest of the form and isValidToken() checks the value that came in with the current request against the value that was saved in the session context by the most recent saveToken() call. If the two token values match, the submission is valid.
--> Reference : http://www.coderanch.com/how-to/java/HowToPreventMultipleFormSubmits
--> For More : http://www.javaken.com/forum/showthread.php?t=4234

4. Diffence between DispatchAction and LookupDispatchAction.?
    Ans : Say you have 3 buttons on your form: add, delete, and update. If your website is in English and you're not planning to use internatoinalization, you would code the three buttons:

<html:submit property="action">add</html:submit>
<html:submit property="action">update</html:submit>
<html:submit property="action">delete</html:submit>

You could then use DispatchAction and code the methods add(), update(), and delete(), and Struts would forward to the appropriate method based on the button pressed.

However, if you use internationalization, this won't work because the label of the button could be in any number of languages. In this case, you need to use LookupDispatch action and code your
JSP like this:

<html:submit property="action"><bean:message key="label.add" /></html:submit>
<html:submit property="action"><bean:message key="label.update /></html:submit>
<html:submit property="action"><bean:message key="label.delete" /></html:submit>
You'll then code the getMethodKey() to find the appropriate method based on the message key. That way, regardless of what language is actually used at runtime, Struts will still dispatch to the correct method.