一、字符串的替换
Json字符串中的字段值常常会有一些回车换行符,可能不能通过json字符串校验,事先先处理一下。

//一个由http请求返回的json字符串

String json_project = "{\"Fld_T_00006\": \"发布\n项目\",\"Fld_T_00007\": \"\",\"Fld_T_00008\": \"\",\"Fld_T_00009\": \"\",\"Fld_T_00010\": \"\",\"Fld_S_00004\": \"38f4dab4-92e6-4051-bc82-4a1ac6665ad9\",\"Fld_S_00002\": \"ce7cb784-d936-4871-911a-d88c4c053912\",\"Fld_T_00003\": \"svn://192.168.1.1\"}";

//字符串的替换方法replaceAll()
json_project = json_project.replaceAll("\n","");
json_project = json_project.replaceAll("\r","");
注意:①替换后要重新赋值;②\n,\r的斜杠不要写反

//通过模板来替换
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(json_project);
json_project = m.replaceAll("");

二、java代码实现http请求

HttpGet httpGet= new HttpGet("http://192.168.1.211:8001/project/get");
CloseableHttpResponse response = null;

response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();

//查看返回信息
System.out.println("entity = " + entity);

三、mysql动态视图

project_member 结构:项目id,成员id(一对多)
需求:按照项目id查找对应的项目成员数量

select pm.projectid AS projectid,COUNT(pm.memberid) AS members,'project_members' AS users from project_member pm GROUP BY pm.projectid

四、通过邮箱找回密码,关于验证码的储存
存放在session中

①发送邮件,并将用户与随机码绑定
//randomString为随机产生的验证码
HttpSession session = _request.getSession(true);
session.setAttribute(randomCode, loginname);
session.setMaxInactiveInterval(timeout);

//如果找回密码访问的是同一台主机下,可能要用到主机和端口
 //服务主机名称
String serverName = this._request.getServerName();
//服务主机端口
int serverPort = this._request.getServerPort();
//下面通过邮件发送的链接
 String pwd_url = "http://" + serverName + ":" + serverPort + "/toRestPwd?code=" + randomCode;

②用户通过邮箱中的链接进入修改密码界面,验证用户
HttpSession session = this._request.getSession(false);
if (session == null) throw new Exception("服务超时!");

String loginname = (String)session.getAttribute(randomCode);
if (loginname == null) throw new InfoException("服务超时或密码已经修改!");
//用户合法则进入修改页面界面

③修改完密码,移除验证码,邮件中的链接失效
HttpSession session = _request.getSession(false);
if (session == null) throw new InfoException("服务超时!");
session.removeAttribute(randomCode);

存放application中

①存储
ServletContext application = this._request.getServletContext();
application.setAttribute(randomCode, loginname);

②验证
 ServletContext application = this._request.getServletContext();
if(application==null) throw new InfoException("服务超时!");

String loginname = (String) application.getAttribute(randomCode);
if (loginname == null) throw new InfoException("服务超时或密码已经修改!");

③移除
 ServletContext application = this._request.getServletContext();
 if(application==null) throw new InfoException("服务超时!");
application.removeAttribute(randomCode);

比较:
session:只有在当前发送找回密码请求的浏览器中,且期间浏览器没有关闭,且在session失效期内,链接可用。
application:只要服务器没关闭,没有点击链接修改过密码,链接一直有效。

application中设置失效时间可通过定时器来实现。

五、产生随机码

//length表示生成字符串的长度
public static String getRandomString(int length) { 
String base ="abcdefghijklmnopqrstuvwxyz0123456789";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < length; i++) {
int number = random.nextInt(base.length());
      sb.append(base.charAt(number));
    }
   return sb.toString();
}

六、MySQL中concat函数

select concat('qaa',' sds ','we') from dual
结果: qaa sds we

select concat('qaa','','we') from dual
结果: qaawe

select concat(null,'12','we','zxc') from dual
结果: null

concat函数的参数可以为多个,只有任何一个参数都不为null,则结果为各个参数的字符串拼接,若其中一个为null,则返回null。

七、MySQL中IFNULL函数的使用
①变量

SELECT IFNULL(userid,'Unknown') as userid FROM svn_record;

②子查询

select t1.* from table1 t1 where t1.num>IFNULL((select num from table2 where t_id=t1.id ),(select num from table2 where t_id is null));

注:table2 中通过t_id与table1中的id进行关联,并保持table2 只有唯一一条与之对应,t_id为空的情况又只有唯一一条。当table1的记录在table2中找不到与之对应的关联记录,则默认与t_id为空的记录保持关联。