Java Developer
Java Developer

Reputation: 1901

How to conform email using Java?

Actually in my java application Registration is their so i'm sending mail to User email.

<bean id="activateAccountTemplate" class="org.springframework.mail.SimpleMailMessage">
    <property name="subject" value="Account activation" />
    <property name="text">
        <value>
        <![CDATA[
            <html><body><p>Dear %s</p><p>Click <a href="http://localhost:8080/EClass/reset.jsp?a=%s">here</a> to activate your account.</p></body></html>
        ]]>
        </value>
    </property>
</bean>

So this is working perfectly.After Click the link

http://localhost:8080/EClass/reset.jsp?a=tdpTA3Dz8DYSI+9F/DpMxmxGD/a1Kl+3oYqXc1NNH0U=

Hear i'm using Encryption Mechanisum.

And my Database have column like..S_id, username, password, active(T/F).

My Requirement is When ever register Active always F(false) but after click the link active will be true(When ever Active is true then only Login allowed).

So How to do in reset.jsp? Actually my thinking is

  1. Write a controller for this and update Active column is True(But how to get sid for this)

  2. Getting SID, Actually encrypt key shown above will generate so this key will be Decrypt and send to controller and using DB Query select particular user and set active true.(But hear also how to get url key... request.getParameter(?) )

Please give me suggestion... i'm stuck how to do this..

Give some sample code on reset.jsp

EDIT : Actually i'm following MVC Principle so with out reset.jsp how to write direct controller class..

Upvotes: 0

Views: 188

Answers (2)

Foredoomed
Foredoomed

Reputation: 2239

You should make active and other stuffs into a separate table.

table user

id   username   password


table activation

id active expired_time token user_id

Once a user completes the sign up, there is one record should be insert into the activation table. active field is false and expired time is 2 hours after now, etc.

Then use the token to look up the table and do the active work.

Upvotes: 1

Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

Your reset.jsp could be something like this :

<%
    String token = request.getParameter("a");

    // Connection to DB executing this query :
    // "UPDATE users SET active = true WHERE S_id = " + token
%>

Since you have probably already code to access your database, I wont include this example part, but it put you on the right track. It would also be really useful to catch if there is one row updated and return information to user if it has worked properly, if no row updated, to verify their URL.

Hope this help.

EDIT :

Same method as a Servlet :

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Reset extends HttpServlet {

  public void doGet(HttpServletRequest request, HttpServletResponse response)
                               throws ServletException, IOException {
    String token = request.getParameter("a");

    // Connection to DB executing this query :
    // "UPDATE users SET active = true WHERE S_id = " + token

    response.setContentType("text/html");
    PrintWriter out = res.getWriter();

    out.println("<HTML><BODY>Your response to the user depending on rows updated from SQL</BODY></HTML>");
  }
}

You will also need to define in web.xml :

<servlet>
  <servlet-name>reset</servlet-name>
  <servlet-class>myservlets.reset</servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>reset</servlet-name>
  <url-pattern>/reset</url-pattern>
</servlet-mapping>

And change your link :

<a href="http://localhost:8080/EClass/reset?a=%s">here</a>

EDIT :

Same method as Spring Controler :

@Controller
@RequestMapping(value = "/activation"
public class AccountActivationController
{
    @RequestMapping(method = RequestMethod.GET)
    public String setupForm(@RequestParam("a") String token, ModelMap model) {
        // Connection to DB executing this query :
        // "UPDATE users SET active = true WHERE S_id = " + token

        return successful ? "activationSuccessful" : "activationError";
    }
}

Can't help much since I've never done Spring before...

Upvotes: 1

Related Questions