1.md5
三步:
1.獲取一個messageDigest加密對象,加密方式為md5
2.獲取一base64encoder對象,用于最后輸出base64編碼
3.md5加密對象給str加密后,用base64輸出。完成加密
public static String encoderStrByMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException{
MessageDigest msgDigest = MessageDigest.getInstance("MD5");
BASE64Encoder base64En = new BASE64Encoder();
return base64En.encode(msgDigest.digest(str.getBytes("utf-8")));
}
2.Properties文件使用
用與記錄系統的配置。數據庫名稱。路徑。密碼。文件路徑等。配套寫個PropertiesUtil用于使用Properties文件
properites文件格式:
key1=value1
key2=value2
Util:1.為本類獲得資源文件,即將properties文件打成輸入流
2.創建一個Properties類對象 加載資源輸入流
3.調用Properties對象的get()方法,傳入key正取到value
public static String getValueForKey(String key){
Properties properties = new Properties();
InputStream input = new PropertiesUtil().getClass().getResourceAsStream("/diary.properties");
try {
properties.load(input);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (String)properties.get(key);
}
3.Cookies 用于記住密碼操作
cookies用戶記住密碼。在用戶登錄成功之后將cookies通過響應response.addcookie存放到瀏覽器中
// 登錄成功
// 如果選擇了記住密碼
if (remember.equals("remember-me")) {
this.rememberMe(userName, password, response);
}
//記住密碼
private void rememberMe(String username,String password,HttpServletResponse response) {
Cookie cookie = new Cookie("user", username+"-"+password);
cookie.setMaxAge(1*60*60*24*7);//cookie有效期一周
response.addCookie(cookie);
}
在jsp頁面中嵌入java代碼
一定判斷用戶是不是第一次登錄,是的話從cookies中取得記住的用戶名密碼,不是的話就有服務器轉發的,因為服務器轉發一般通過request,session,所以吧cookies中的用戶名密碼放到pageContext中,讓el表達式優先取得cookies中的用戶名密碼
<%
if(request.getAttribute("user")==null){//第一次用戶登錄,不是后臺回調轉發的
String userName = null;
String password = null;
Cookie[] cookies = request.getCookies();
for(int i = 0 ; cookies!=null && i<cookies.length ; i++){
if(cookies[i].getName().equals("user")){
userName = cookies[i].getValue().split("-")[0];
password = cookies[i].getValue().split("-")[1];
}
}
if(userName == null){
userName = "";
}
if(password == null){
password = "";
}
//放到pageContext 讓EL表達式優先獲取
pageContext.setAttribute("user", new User(userName,password));
}%>
4.一個關于jdbc鏈接的bug
WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
首先恭喜,出現這個的時候MySQL說明已經安裝成功了,這是警告不是錯誤,以后使用是不影響的。大概的意思就是說建立ssl連接,但是服務器沒有身份認證,這種方式不推薦使用。
解決辦法:
原來的連接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "letmein");
現在的連接url:Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false","root", "letmein");