function registered(){
//省略: 沒有表單驗證 驗證碼 (後期添加)
document.forms[0].action="
document.forms[0].submit();
}
function login(){
document.forms[0].action="
document.forms[0].submit();
}
登錄Servlet (LoginServlet)
protectedvoiddoPost(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException, IOException {
PrintWriterpwOut = response.getWriter();
Stringpath = request.getContextPath()+"/index.jsp";
Stringname = request.getParameter("name");
Stringpwd = request.getParameter("pwd");
Useruser = newUser();
user.setName(name);
user.setPwd(pwd);
if(name == null || name.trim().length() <= 0) {
pwOut.println("用戶名不能為空!");
pwOut.println(" ");
return;
}
if(pwd == null || pwd.trim().length() <= 0) {
pwOut.println("密碼不能為空!");
pwOut.println(" ");
return;
}
user = userService.login(user);
if(user == null) {
request.getSession().setAttribute("error", "1");
} else {
request.getSession().setAttribute("user", user);
}
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
UserDao中的登錄方法
publicUserlogin(Useru) {
Stringsql = "select * from t_user where name=? and pwd=? and active='1' ";
QueryRunnerrun = newQueryRunner(C3p0PoolUtils.getDataSource());
try {
Useruser = run.query(sql,newBeanHandler
returnuser;
} catch (SQLExceptione) {
e.printStackTrace();
returnnull;
}
}
註冊Servlet (ReServlet)
PrintWriterpwOut = response.getWriter();
Stringpath = request.getContextPath()+"/index.jsp";
Stringname = request.getParameter("name");
Stringpwd = request.getParameter("pwd");
Stringemail = request.getParameter("email");
if(name == null || name.trim().length() <= 0) {
pwOut.println("用戶名不能為空!");
pwOut.print(" ");
return;
}
if(pwd == null || pwd.trim().length() <= 0) {
pwOut.println("密碼不能為空!");
pwOut.println(" ");
return;
}
if(email==null||email.trim().length()<=0){
pwOut.println("郵箱不能為空,請重新輸入!");
pwOut.println("
");
return;
}
if(email.indexOf("@")==-1){
pwOut.println("郵箱格式不對,請重新輸入!");
pwOut.println("
");
return;
}
//設置對象
Useruser = newUser();
user.setName(name);
user.setPwd(pwd);
user.setEmail(email);
user = userService.login(user);
if (user!=null){
//發送激活郵件
//這裡有一個小知識點,必須新開一個線程來發郵件,不能把發郵件的動作寫在這裡
//如果寫在這裡,用戶的前臺顯示會等待過長時間,不好!
newSendMailThread(user).start();
pwOut.println("您已經註冊成功,請去郵箱激活賬號後再進行登錄,如果沒有收到郵件,請稍等!
");
pwOut.println("
");
}else{
pwOut.println("很抱歉,服務器繁忙,註冊失敗,需要重新註冊!");
}
pwOut.close();
註冊Dao中的方法
publicUserreg(Useruser) {
Stringsql = "insert into t_user(id,name,pwd,email,active,acode) values(?,?,?,?,?,?)" ;
QueryRunnerrun = newQueryRunner(C3p0PoolUtils.getDataSource());
Stringid = UUID.randomUUID().toString().replaceAll("-", ""); Stringacode = UUID.randomUUID().toString().replaceAll("-", "");
try {
run..update(sql, id, user.getName(), user.getPwd(), user.getEmail(), "0", acode);
user.setId(id);
user.setAcode(acode);
user.setActive("0");
} catch (SQLExceptione) {
e.printStackTrace();
returnnull;
}
returnuser;
}
建立郵箱連接SendMailThread.java
publicclassSendMailThreadextends Thread{
privateUseruser = null ;
publicSendMailThread(Useruser) {
this.user = user ;
}
publicvoidrun() {
//跟smtp服務器建立一個連接
Propertiesp = newProperties ();
//設置郵件服務器主機名
p.setProperty("mail.host","smtp.qq.com"); //指定郵件服務器,默認端口 25
//發送服務器需要身份驗證
p.setProperty("mail.smtp.auth", "true"); //要採用指定用戶名密碼的方式去認證
//發送郵件協議名稱
p.setProperty("mail.transport", "smtp");
//開啟SSL加密, 否則會失效
MailSSLSocketFactorysf = null;
try {
sf = newMailSSLSocketFactory();
} catch (GeneralSecurityExceptione) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
p.put("mail.smtp.ssl.enable", "true");
p.put("mail.smtp.ssl.socketFactory", sf);
// 開啟debug調試,以便在控制檯查看
// session.setDebug(true);也可以這樣設置
// p.setProperty("mail.debug", "true");
// 創建session
Sessionsession = Session.getDefaultInstance(p, newAuthenticator() {
@Override
protectedPasswordAuthenticationgetPasswordAuthentication() {
// 用戶名可以用QQ賬號也可以用郵箱的別名
PasswordAuthenticationpa = newPasswordAuthentication("271249758", "oqyecoskkbxnbiie");
// 後面的字符是授權碼,用qq密碼不行!!
returnpa;
}
});
session.setDebug(true);// 設置打開調試狀態
try {
//聲明一個Message對象(代表一封郵件),從session中創建
MimeMessagemsg = newMimeMessage(session);
//郵件信息封裝
//1.發件人
msg.setFrom(newInternetAddress("[email protected]"));
//2.收件人
msg.setRecipient(RecipientType.TO, newInternetAddress(user.getEmail()));
//3.郵件內容:主題、內容
msg.setSubject(user.getName() + " ,歡迎加入我們,點擊鏈接激活賬號");
//StringBuilder這裡只會有一個線程
StringBuildersbd = newStringBuilder();
sbd.append(user.getName() + "
歡迎!確認郵箱地址以激活您的賬號。");
sbd.append("
"");
sbd.append("或者點擊下面鏈接:
");
sbd.append("http://localhost/MyMailDemo/ActiveServlet?acode=" + user.getAcode() + "
");
sbd.append("這是一封自動發送的郵件;如果您並未要求但收到這封信件,您不需要進行任何操作。");
msg.setContent(sbd.toString(), "text/html;charset=utf-8");// 發html格式的文本
// 發送動作
Transport.send(msg);
System.out.println("給" + user.getEmail() + "發送郵件成功。");
} catch (Exceptione) {
e.printStackTrace();
}
super.run();
}
}
郵箱POP3/SMTP服務 例如QQ郵箱
授權碼設置在SendMailThread.java中
閱讀更多 給我一杯溫開水 的文章