Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes How to implement this requirement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to implement this requirement" Watch "How to implement this requirement" New topic
Author

How to implement this requirement

Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
I have to design a module which has this requirement.

A html page where users specifies the amount to be deducted by entering the amount in a textbox and presses deduct button. But if the user presses this deduct twice accidentally the amount will be deducted twice from his account. This should be prevented.

I tried disabling the Deduct button once the user presses it using javascript code. But this solution did not work in netscape browser. Is there any better solution to this problem ?

[ December 06, 2006: Message edited by: Satish Kota ]
[ December 06, 2006: Message edited by: Satish Kota ]

SCJP 5.0 77%
Santhosh Reddy
Greenhorn

Joined: Sep 25, 2006
Posts: 27
Try using transaction management
Johnson Abraham
Greenhorn

Joined: May 31, 2004
Posts: 19
You will have to associate your request object with a unique key and a boolean variable. You will also have to maintain a hashtable at the application scope which will keep a record of which unique key is associated with which request. Request object is a hash, so you can set and get objects on your will. So as soon as a request is submitted you need to get the unique key and request id from the request check in the application scopes hashtable to crosscheck if it is the correct key. If the authentication passes set the boolean variable to true. Now true will signify that this request is submitted. so everytime you will have to check for the boolean variable to see if the request is already submitted. If the request is already submitted do not perform any action.
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Johnson Abraham:
You will have to associate your request object with a unique key and a boolean variable. You will also have to maintain a hashtable at the application scope which will keep a record of which unique key is associated with which request. Request object is a hash, so you can set and get objects on your will. So as soon as a request is submitted you need to get the unique key and request id from the request check in the application scopes hashtable to crosscheck if it is the correct key. If the authentication passes set the boolean variable to true. Now true will signify that this request is submitted. so everytime you will have to check for the boolean variable to see if the request is already submitted. If the request is already submitted do not perform any action.



Technically Johnson's suggestion can be implemented by the following way.

Client ckilks submit button.
Below is the algorithm that is to be implemented at the servlet code that intercepts the client request.

session = request.getSession(false);
if(session != null){
String firstClick = session.getAttribute("FIRST_CLICK");
if(firstClick.equals("SET")){
//this is second click of the button.
//take appropriate action.
}else{
session.setAttribute("FIRST_CLICK","SET");
//do processing.
}
}//end of if.


Rahul Bhattacharjee
LinkedIn - Blog
Satish Kota
Ranch Hand

Joined: Feb 08, 2006
Posts: 88
Originally posted by Rahul Bhattacharjee:



Technically Johnson's suggestion can be implemented by the following way.

Client ckilks submit button.
Below is the algorithm that is to be implemented at the servlet code that intercepts the client request.

session = request.getSession(false);
if(session != null){
String firstClick = session.getAttribute("FIRST_CLICK");
if(firstClick.equals("SET")){
//this is second click of the button.
//take appropriate action.
}else{
session.setAttribute("FIRST_CLICK","SET");
//do processing.
}
}//end of if.


When should i reset the FIRST_CLICK attribute in session. Because if i don't reset FIRST_CLICK i will be unable to do any more submission!
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
After the processing , remove value "SET" from session.
Prabhu Venkatachalam
Ranch Hand

Joined: Nov 16, 2005
Posts: 502

Satish,

You want user not to press "Deduct" button in his session time or you don't want him press continously twice?

Thanks,


Prabhu Venkatachalam<br />SCJP 1.4,SCWCD 1.4<br />prabhu.venkatachalam@gmail.com
Chaitanya Devavarapu
Greenhorn

Joined: Dec 06, 2006
Posts: 19
You do one thing.
When you type the URL in the address bar to access your web-application, initially,

1) create a session object.
HttpSession session = request.getSession(true);

2) take a boolean value boolValue and assign it as false.
boolean boolValue = false;

3). Convert this 'boolValue' into 'Boolean object'.
To do this, take a Boolean wrapper class and pass boolValue as an
argument.

Boolean b = new Boolean(boolValue);

4) Now set this Boolean object 'b' as an attribute of session.

session.setAttribute("boolValue_attribute",b);

Now let us come to your problem.....
When your form is submitted, some value is deducted from the database.
When the form is again submitted, the value should not be deducted.

To solve this....

When your form is submitted, request goes to the web-container, and corresponding servlet is executed.
Now into this servlet write the following code.

5). HttpSession session = request.getSession(true);

(I think you know -the purpose of this method.
Because we are passing 'true' value as an attribute of
getSession() method,the web-container checks whether the session
object is created previously or not.
If the session object is already created, it returns the
reference of the object.
If the session object is not created earlier, web-container
creates a new session object).

(Here, in our case, because of the code at line 1, the session
object is already created.
so the above code at line 5, returns the reference of the
existing session object).

5). Now get the value of the 'boolValue_attribute' from session object.

Boolean b1 = (Boolean) session.getAttribute("boolValue_attribute");

(This method returns value of type Object. So we have typecasted
this value into Boolean).

6). Now get the actual value from Boolean object.
To get the wrapped value -
boolean b2 = b1.booleanValue();

7). Now let us compare b2 with false.
if (b2 == false)
{
// code to deduct the value .

booleValue = true; // you must set this 'boolValue' to 'true'.

Boolean b = new Boolean(boolValue);
session.setAttribute("boolValue_attribute",b);
}


8) Now in this case previously
boolValue = false.
But after the form is submitted, and after the if condition is executed,
boolValue = true;

9). Now in this situation, if you submit the form once again, because
the boolValue = true, If condition will not be executed. and the
transaction will not be done.
(i.e, the value will not be deducted once again).

Finally it solves your problem
bye.
yu wang
Greenhorn

Joined: Dec 12, 2006
Posts: 4
You can use javascript to do this. After the user press the button first time , using javascipt to disable the button. When the user click the button second time, there will be nothing happen(No form will be sent to the server).
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
There is a J2EE pattern i think Session Token, it deals with the web application flow. It will also help you with the browser's back button and things like that.


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to implement this requirement
 
Similar Threads
How do i know my market value
Problem with Jtable
hello guy,add me of MSN please~~
Essay exam scheduled in advance?
Gmail