#%@page import="java.sql.*"%*
#%@page contentType="text/html" pageEncoding="UTF-8"%*
#!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"*
#html*
#head*
#meta http-equiv="Content-Type" content="text/html; charset=UTF-8"*
#title*apnafb.com#/title*
#/head*
#body*
#%! String fname,lname,email,password,sex,month; int date,year;%*
#%
Connection con =null;
Statement st= null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/snp","root","");
st=con.createStatement();
String sqlc;
sqlc="Select * from userinfo";
email=request.getParameter("email");
password=request.getParameter("password");
Boolean exist=false;
ResultSet rs= st.executeQuery(sqlc);
while(rs.next())
{
if(rs.getString("semail").equals(email)&& rs.getString("spassword").equals(password))
{
exist=true;
fname=rs.getString("fname");
lname=rs.getString("lname");
sex=rs.getString("sex");
date=rs.getInt("date");
month=rs.getString("month");
year=rs.getInt("year");
}
}
if(!exist) response.sendRedirect("/snc/index.jsp");
}
finally
{
try
{
if(st!=null)
{
st.close();
}
if(con!=null)
{
con.close();
}
}
catch(SQLException s)
{
}
}
%*
#center*#h1* Welcome #%=fname%* #%=lname%* #/h1*#/center*
#br*#br*
#center*#h2*
Your Password is #%=password%* #br*
Your Email is #%=email%* #br*
You are #%=sex%* #br*
Your Date of Birth is : #%=date%* #%=month%*, #%=year%* . #br*
#h1* Thanks for Visiting Our Site again....!!#/h1*
#/h2* #/center*
#/body*
#/html*
Coding, Programming examples using amazing small applications only for educational purpose.
Saturday, August 6
SignupServlet sending mail for email confirmation
Using 2 files :
1. signupservlet.java
2. Mail.java
_______________________________________
signupservlet.java
... _______________________________________
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.Random;
public class signupservlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/snp","root","");
Statement st=con.createStatement();
String fname,lname,semail,spassword,sex,month;
int date,year;
fname=request.getParameter("fname");
lname=request.getParameter("lname");
semail=request.getParameter("semail");
spassword=request.getParameter("spassword");
sex=request.getParameter("sex");
date=Integer.parseInt(request.getParameter("date"));
month=request.getParameter("month");
year=Integer.parseInt(request.getParameter("year"));
Random randomGenerator = new Random();
int randomid=randomGenerator.nextInt(1000000);
String sqlc="insert into userinfo Values('"+fname+"','"+lname+"','"+semail+"','"+spassword+"','"+sex+"'," +date+ ",'"+month+"',"+year+",NULL,'"+randomid+"')";
st.executeUpdate(sqlc);
//handling email confirmation
Mail mail = new Mail();
month="";
month+= "http://localhost:8084/snc/signupconfirm.jsp"+"?randomid="+randomid+"&email="+semail;
Mail.sendMail("apnafb@gmail.com","mltfrmqnbsp","smtp.gmail.com","465","true","true",true,"javax.net.ssl.SSLSocketFactory","false",semail,"apnafb.com Email Conformation",month);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("#html*");
out.println("#body*");
out.println("#center*#h1* Just One Step left...!! #/h1* #br/*");
out.println("#center*#h2* Go to your Email Where we have sent you a link..!! Click that link to verify you email...!!#/h2*#/center*");
out.flush();
}
catch(Exception e)
{
response.sendRedirect("/snc/index.jsp");
}
}
}
____________________________________________________
Mail.java
____________________________________________________
package servlet;
import java.util.*;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail
{
public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String to,String subject,String text)
{
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
if(!"".equals(port))
props.put("mail.smtp.port", port);
if(!"".equals(starttls))
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
if(debug)
props.put("mail.smtp.debug", "true");
else
props.put("mail.smtp.debug", "false");
if(!"".equals(port))
props.put("mail.smtp.socketFactory.port", port);
if(!"".equals(socketFactoryClass))
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
if(!"".equals(fallback))
props.put("mail.smtp.socketFactory.fallback", fallback);
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("prembhaarti@gmail.com"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
}
catch (Exception mex)
{
mex.printStackTrace();
return false;
}
}
}
1. signupservlet.java
2. Mail.java
_______________________________________
signupservlet.java
... _______________________________________
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.Random;
public class signupservlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/snp","root","");
Statement st=con.createStatement();
String fname,lname,semail,spassword,sex,month;
int date,year;
fname=request.getParameter("fname");
lname=request.getParameter("lname");
semail=request.getParameter("semail");
spassword=request.getParameter("spassword");
sex=request.getParameter("sex");
date=Integer.parseInt(request.getParameter("date"));
month=request.getParameter("month");
year=Integer.parseInt(request.getParameter("year"));
Random randomGenerator = new Random();
int randomid=randomGenerator.nextInt(1000000);
String sqlc="insert into userinfo Values('"+fname+"','"+lname+"','"+semail+"','"+spassword+"','"+sex+"'," +date+ ",'"+month+"',"+year+",NULL,'"+randomid+"')";
st.executeUpdate(sqlc);
//handling email confirmation
Mail mail = new Mail();
month="";
month+= "http://localhost:8084/snc/signupconfirm.jsp"+"?randomid="+randomid+"&email="+semail;
Mail.sendMail("apnafb@gmail.com","mltfrmqnbsp","smtp.gmail.com","465","true","true",true,"javax.net.ssl.SSLSocketFactory","false",semail,"apnafb.com Email Conformation",month);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("#html*");
out.println("#body*");
out.println("#center*#h1* Just One Step left...!! #/h1* #br/*");
out.println("#center*#h2* Go to your Email Where we have sent you a link..!! Click that link to verify you email...!!#/h2*#/center*");
out.flush();
}
catch(Exception e)
{
response.sendRedirect("/snc/index.jsp");
}
}
}
____________________________________________________
Mail.java
____________________________________________________
package servlet;
import java.util.*;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail
{
public synchronized static boolean sendMail(String userName,String passWord,String host,String port,String starttls,String auth,boolean debug,String socketFactoryClass,String fallback,String to,String subject,String text)
{
Properties props = new Properties();
props.put("mail.smtp.user", userName);
props.put("mail.smtp.host", host);
if(!"".equals(port))
props.put("mail.smtp.port", port);
if(!"".equals(starttls))
props.put("mail.smtp.starttls.enable",starttls);
props.put("mail.smtp.auth", auth);
if(debug)
props.put("mail.smtp.debug", "true");
else
props.put("mail.smtp.debug", "false");
if(!"".equals(port))
props.put("mail.smtp.socketFactory.port", port);
if(!"".equals(socketFactoryClass))
props.put("mail.smtp.socketFactory.class",socketFactoryClass);
if(!"".equals(fallback))
props.put("mail.smtp.socketFactory.fallback", fallback);
try
{
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
MimeMessage msg = new MimeMessage(session);
msg.setText(text);
msg.setSubject(subject);
msg.setFrom(new InternetAddress("prembhaarti@gmail.com"));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, userName, passWord);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
return true;
}
catch (Exception mex)
{
mex.printStackTrace();
return false;
}
}
}
Subscribe to:
Posts (Atom)