項(xiàng)目基本思路:
1、前后端分離,使用AJAX中的GET POST方法請(qǐng)求Servlet
2、Servlet調(diào)用Service方法,再進(jìn)一步調(diào)用Dao操作數(shù)據(jù)庫
3、將Servlet中獲取的值以Json格式返回前端,進(jìn)行解析
將表單中提交的數(shù)據(jù)轉(zhuǎn)換成username=aaa&&password=bbb格式只需一行代碼
$(this).serialize() this指代的是需要提交的表單
使用classLoader加載文件時(shí)不需要加/
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
用戶進(jìn)行登錄操作后,可以將User對(duì)象存入Session中,在需要判斷用戶是否登錄時(shí)可以獲取Session判斷是否為null
如果頁面有亂碼現(xiàn)象可用response.setContentType("text/html;charset=utf-8")解決
郵箱激活碼的實(shí)現(xiàn):
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 發(fā)郵件工具類
*/
public final class MailUtils {
private static final String USER = " "; // 發(fā)件人稱號(hào),同郵箱地址
private static final String PASSWORD = " "; // 如果是qq郵箱可以使戶端授權(quán)碼,或者登錄密碼
/**
*
* @param to 收件人郵箱
* @param text 郵件正文
* @param title 標(biāo)題
*/
/* 發(fā)送驗(yàn)證信息的郵件 */
public static boolean sendMail(String to, String text, String title){
try {
final Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.qq.com");
// 發(fā)件人的賬號(hào)
props.put("mail.user", USER);
//發(fā)件人的密碼
props.put("mail.password", PASSWORD);
// 構(gòu)建授權(quán)信息,用于進(jìn)行SMTP進(jìn)行身份驗(yàn)證
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用戶名、密碼
String userName = props.getProperty("mail.user");
String password = props.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// 使用環(huán)境屬性和授權(quán)信息,創(chuàng)建郵件會(huì)話
Session mailSession = Session.getInstance(props, authenticator);
// 創(chuàng)建郵件消息
MimeMessage message = new MimeMessage(mailSession);
// 設(shè)置發(fā)件人
String username = props.getProperty("mail.user");
InternetAddress form = new InternetAddress(username);
message.setFrom(form);
// 設(shè)置收件人
InternetAddress toAddress = new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO, toAddress);
// 設(shè)置郵件標(biāo)題
message.setSubject(title);
// 設(shè)置郵件的內(nèi)容體
message.setContent(text, "text/html;charset=UTF-8");
// 發(fā)送郵件
Transport.send(message);
return true;
}catch (Exception e){
e.printStackTrace();
}
return false;
}