tags:

views:

210

answers:

1

I need to verify the email. I want to do it by generating a unique string and making a link of it for users to click on it in the mail.

I dont know how to generate it using Java.

There are md5, sha1 etc functions in php to generate the string using any unique value like email. Is there same function provided in jsp?

Advice please.

thank you

+5  A: 

One way encryption tools like MD5/SHA/etc does not necessarily generate unique strings. Two different strings can namely generate the same hash. That's after all also the whole idea behind one-way encryption: there's no (reliable) way to find out what the original string was.

Better make use of java.util.UUID, if necessary in combination with a database PK or UK so that you can just generate a new one in case of an (unexpected) constraint violation.

Here's a basic example how to get such a random unique key:

String key = UUID.randomUUID().toString();

That said, JSP is a view technology. You aren't supposed to write raw Java code in JSP files. Use taglibs and EL in JSP only. With taglibs you can control the page flow and with EL you can access backend data. Keep raw Java code in Java classes like servlets, filters, beans, etc.

In this specific case, just have a JSP with a HTML form which submits to some (controller) servlet which in turn generates the key using java.util.UUID, stores it in the DB using the JDBC API, sends an email using the JavaMail API and finally forwards the request to some result JSP.

BalusC
i said use md5/sha with any unique value like email or time and they are unique i guess. Correct me if i am wrong. anyways thanks for the answer. got what i've looking for
RishiPatel
As said, different strings can generate same MD5 hash. That's the whole point of MD5. You cannot trackback the original string. Otherwise MD5 would have been useless as "security encryption".
BalusC
Ohh got the point!
RishiPatel