Thursday, January 8, 2009

Handle duplicate form submission struts

The problem of duplicate form submission arises when a user clicks the Submit button more than once before the response is sent back or when a client accesses a view by returning to a previously bookmarked page. This may result in inconsistent transactions and must be avoided. In our sample application, a similar problem will arise if the customer clicks the submit button more than once while submitting the purchase order.

In Struts this problem can be handled by using the saveToken() and isTokenValid() methods of Action class. saveToken() method creates a token (a unique string) and saves that in the user's current session, while isTokenValid() checks if the token stored in the user's current session is the same as that was passed as the request parameter.

To do this the JSP has to be loaded through an Action. Before loading the JSP call saveToken() to save the token in the user session. When the form is submitted, check the token against that in the session by calling isTokenValid(), as shown in the following code snippet:


public class XXXAction extends DispatchAction
{
public ActionForward load(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
try
{ //save the token
saveToken(request)

// rest of the code for loading the form
}
catch(Exception ex){//exception}
}

public ActionForward submitOrder(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
try
{
// check the token. Proceed only if token is valid
if(isTokenValid(request,true)) {
//implement order submit functionality here
} else {
return mapping.findForward("failure");
}
}
catch(Exception ex){//exception}
}
}

sun IDM Xpress Langauge <and> and <or>



The function is a logical function and evaluates in the context of true and false (1 and 0).
Below provided two examples of the and function.

In the first example, the String, 42, is compared to a null value. The second value
is null, therefore the entire expression returns an integer of 0 (which is considered false).

In the second example, the integer, 42, is compared with a string value of 45.
The string value, <s>45</s> is not a null string so it is considered to be
true in the context of logical operations. The and expression returns the last value
cnsidered to be true if all expressions evaluate to true, therefore, this expression returns the string, "false".


<and>
<s>42</s>
<null/>
</and>----------> returns 0

<and>
<s>42</s>
<s>45</s>
</and> ---------------> returns 45



<or> – Takes any number of arguments and returns zero if all of the argument values are logically false. It returns the first argument that results in a logically true value

see the example below

<or>
<i>o</i>
<null/>
</or>-------> returns 0

<or>
<i>0</i>
<s>47</s>
<s>45</s>
<or>-----> returns 47



accessing class private constructor outside class

We can create object of class which has private constructor, outside the class using the Reflection API.
See the code below.
Class with private constructor
package test;
public class privateconstructor implements NewInterface {
private privateconstructor() { }
public void fine() {
System.out.println("fine"); }}Program creating the above class and calling the method
package test;
import java.lang.reflect.Constructor;
public class Main {
public static void main(String[] args) throws Exception {
privateconstructor keyPair;
Class c = privateconstructor.class;
Constructor[] cons = c.getDeclaredConstructors();
cons[0].setAccessible(true);
keyPair = cons[0].newInstance(); keyPair = (privateconstructor) c.newInstance();
keyPair.fine();
}}
You can acess this private variables and private methods using reflection.