借助 HttpServletRequest request 获取对应的session
public void sendCodeMsg(HttpServletRequest request, String mobile) throws Exception { String code = "" + (int) ((Math.random() * 9.0D + 1.0D) * 100000.0D);//获取随机6位数 this.sendMsg(mobile, String.format(SmsParamConfigure.Code_Content, new Object[] { code })); //通过短信接口发送短信验证码 logger.info("电话号码" + mobile + "; 验证码:" + code); //输出短信信息 request.getSession().setAttribute(Check_Code + mobile, code); //在session中加入相应的手机号 和 验证码 } session.removeAttribute(Check_Code + mobile); //移除session中对应的值