实现短信验证码的实列

package cn.bdqn.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.bdqn.biz.UserBiz;
import cn.bdqn.pojo.User;
import cn.bdqn.util.MsgUtil;
@Controller
public class MaggerController {
@Resource
private  UserBiz userBiz;
@RequestMapping(value="/sendMsg.html")
public void sendMsg(String telephone,Model model,HttpSession session){
System.out.println("jinlai");
MsgUtil mu=new MsgUtil();
int mobile_code=mu.maggeres(telephone);
System.out.println(mobile_code);
session.setAttribute("mobile_code", mobile_code);
}
@RequestMapping(value="/checkNum.html",method=RequestMethod.POST)
public String checkNum(String telephone,String mobileCode,HttpSession session,Model model){
String code=session.getAttribute("mobile_code").toString();
if(mobileCode.equals(code)){
System.out.println("是否匹配"+mobileCode.equals(code));

User user=new User();
user.setTelephone(telephone);
user.setPassword(code);
int row=userBiz.addUser(user);
if(row>0){
return "redirect:index.html";
}else{
return "magger";
}
}
model.addAttribute("mmsg", "验证码输入错误;");
return "magger";
}

}

2:写一个工具类,

package cn.bdqn.util;


import java.io.IOException;




import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
/**

@author zhu
版本:1.0 
*/
//短信验证方法
public class MsgUtil {
//获取网站的链接
private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

public int maggeres(String tel){

HttpClient client = new HttpClient(); 
PostMethod method = new PostMethod(Url);


client.getParams().setContentCharset("UTF-8");
method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=UTF-8");


int mobile_code = (int)((Math.random()*9+1)*100000);


   String content = new String("您的验证码是:" + mobile_code + "。请不要把验证码泄露给其他人。");


NameValuePair[] data = {//提交短信
   new NameValuePair("account", "cf_741852147"), 
   new NameValuePair("password", "9f14579cd06fbbd2a877765f51431f70"), //查看密码请登录用户中心->验证码、通知短信->帐户及签名设置->APIKEY
   //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
   new NameValuePair("mobile",tel ), 
   new NameValuePair("content", content),
};
method.setRequestBody(data);


try {
client.executeMethod(method);

String SubmitResult =method.getResponseBodyAsString();


//System.out.println(SubmitResult);


Document doc = DocumentHelper.parseText(SubmitResult);
Element root = doc.getRootElement();


String code = root.elementText("code");
String msg = root.elementText("msg");
String smsid = root.elementText("smsid");


System.out.println(code);
System.out.println(msg);
System.out.println(smsid);


if("2".equals(code)){
System.out.println("短信提交成功");
}


} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mobile_code;
}
}

3:实现电话号码的判断

package cn.bdqn.util;


import java.security.MessageDigest;


public class StringUtil {
public static String str;
public static final String EMPTY_STRING = "";


private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };


private static String byteToHexString(byte b) {
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}



public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}


public static String MD5Encode(String origin) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
resultString = byteArrayToHexString(md.digest(resultString
.getBytes()));
} catch (Exception ex) {
}
return resultString;
}


}

4:jsp 页面

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../favicon.ico">


    <title>注册</title>


    <!-- Bootstrap core CSS -->
    <link href="dist/css/bootstrap.min.css" rel="stylesheet">


    <!-- Custom styles for this template -->
    <link href="css/signin.css" rel="stylesheet">


    <!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
    <!--[if lt IE 9]><script src="../../assets/js/ie8-responsive-file-warning.js"></script><![endif]-->
    <script src="assets/js/ie-emulation-modes-warning.js"></script>

    <!-- <script src="https://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script> -->
    <!-- <script src="https://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script> -->
    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#getNum").click(function(){
var tel=$("#telephone").val()
$.ajax({
url:"sendMsg.html?telephone="+tel,
success:function(data){
alert("短信已发送!");
}
})
})
})

</script>


  <body>


<div class="container">
<form class="form-signin" role="form" action="checkNum.html" method="post">
<h2 class="form-signin-heading">验证码注册</h2>
用户名:<input type="text" class="form-control" placeholder="手机号码"
autofocus name="telephone" id="telephone">
<!-- <button class="btn btn-lg btn-primary btn-block" id="getNum">获取验证码</button> -->
<input type="button" value="获取验证码" id="getNum">
<!--实现异步,实现输入的验证码与发出的验证码进行匹对,如果匹对则调动注册方法 -->
验证码<input type="text" name="mobileCode" >

<div class="checkbox">
          <!-- <label>
            <input type="checkbox" value="remember-me"> 记住我
          </label> -->
        </div>
<button class="btn btn-lg btn-primary btn-block" type="submit">注册</button>${msg}
</form>
${mmsg}
</div>
<!-- /container -->
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值