I am working on a program that accepts...
Articles and Tips: qna
01 Jun 2002
Q.
I am working on a program that accepts a user's credit card. I have fields for the user's name, credit card number, and credit card expiration date. I have programmed this using JSP and LDAP.
Everything works fine if the user enters their credit card information, but if the user re-enters the credit card information, a Java exception is thrown. The exception indicates that an attribute already exists. Here is the guilty code:
// Get the current connection object LDAPConnection connection = (LDAPConnection)session.getValue ("Connection"); String course = request.getParameter("course"); String userdn = (String) session.getValue("userdn"); String name = (String) session.getValue("name"); LDAPAttribute attrToModify = new LDAPAttribute( "aspenRegisteredCourses", course); LDAPModificationSet modificationSet = new LDAPModificationSet(); modificationSet.add( LDAPModification.ADD, attrToModify ); connection.modify( userdn, modificationSet );
Should I check to see if the credit card attribute existed, when the user attempted to resave it, and then delete the attribute values? This would allow me to be able to create the new credit card data. Do you think my solution is appropriate, or is there a better way?
A.
I am sure that your solution would work, but fortunately there is a simpler way to solve your problem. Look at the modificationSet.add line in your code. Here you are doing an LDAP modification to the credit card attribute. A modification/add will expect to be able to create the attribute. If the attribute already exists, you get the exception thrown. Modify the line that contains the modificationSet.add to do a REPLACE instead. Your code will look like this:
modificationSet.add( LDAPModification.REPLACE, attrToModify );
Using REPLACE instead of ADD will replace the current credit card information with the new information. This will solve your problem in a nutshell.
(Thanks to Alan Clark of the Novell eDirectory Development Team for the idea of this question.)
* Originally published in Novell AppNotes
Disclaimer
The origin of this information may be internal or external to Novell. While Novell makes all reasonable efforts to verify this information, Novell does not make explicit or implied claims to its validity.