发表于: 2017-10-27 17:53:03
1 792
今日完成:
小课堂准备加上弄表格,弄了大半天,正式开始任务7
因为这里是一个简单的用户登录,和普通的增删改查,就套用task4框架的基本页面
用户除去普通的 ID NAME PWD 字段 再增加手机 邮箱 头像字段!
创建实体类
在数据库中创建相应的表
create table student(studentId varchar(40),studentName varchar(40),studentPhone varchar(40),studentEmail varchar(40),studentHead varchar(40));
第二步申请容联免费的短信通道
莫办法,等审核...
时间不等人..搞SendCloud邮箱
申请完后得到如下API
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class SendCommonPostMail {
public static void main(String[] args) throws IOException {
final String url = "http://api.sendcloud.net/apiv2/mail/send";
final String apiUser = "您账户中的API_USER";
final String apiKey = "您自己设置的API_KEY";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httPost = new HttpPost(url);
List params = new ArrayList();
// 您需要登录SendCloud创建API_USER,使用API_USER和API_KEY才可以进行邮件的发送。
params.add(new BasicNameValuePair("apiUser", apiUser));
params.add(new BasicNameValuePair("apiKey", apiKey));
params.add(new BasicNameValuePair("from", "service@sendcloud.im"));
params.add(new BasicNameValuePair("fromName", ""));
params.add(new BasicNameValuePair("to", "收件人地址"));
params.add(new BasicNameValuePair("subject", "来自SendCloud的第一封邮件!"));
params.add(new BasicNameValuePair("html", "你太棒了!你已成功的从SendCloud发送了一封测试邮件,接下来快登录前台去完善账户信息吧!"));
httpost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
// 请求
HttpResponse response = httpclient.execute(httPost);
// 处理响应
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 正常返回
// 读取xml文档
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
} else {
System.err.println("error");
}
httpost.releaseConnection();
}
}
将文件依赖引入pom,先做个Demo测试一下效果,但时间不够了,明儿赶早测
最后讲讲关于昨天代码优化的问题!
第一,关于cookies取值的一个工具提取,因为每次取cookie时都需要通过迭代来实现取值,非常的麻烦,这里则提取了一个小的工具代码如下
public class ServletUtil {
public static String getCookie(HttpServletRequest request, String name) {
Cookie[] cookies = request.getCookies();
if (name == null || cookies == null) {
return null;
}
for (Cookie cookie : cookies) {
if (name.equals(cookie.getName())) {
return cookie.getValue();
}
}
return null;
}
}
想当的简单,我们每次只需要往该工具中存放,request和需要取出的cookie名称就行,源代码优化后只有一行
第二,是关于token 令牌的验证的使用,这里在自动登录时完全取代了以前通过帐号密码来实现用户身份验证的传统模式,大大提高了用户密码的安全
每一次自动或手动登录后都会去数据库中更新我们用户最后一次的登录时间,将ID加登录时间进行加密,在验证信息时将cookie中的token解密与数据库中对应用户的ID和登陆时间对比,从而判断用户身份是否有效或是否超时.
明日计划:任务7把短信和图片存储申请完毕速度做,小课堂拖了整整一天....
问题:无
收获:自己讲了一遍AOP对其更加熟悉,但讲的一般般,动态代理和CGLIB代理的实现过程感觉没必要深入...
评论