浏览 3306 次
锁定老帖子 主题:往手机发验证码
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2009-02-24
最后修改:2009-02-24
首先是页面,此页面是用于输入用户的手机号码:addModeCode.jsp <%@ page contentType="text/html; charset=gb2312" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <style type="text/css"> <!-- .STYLE71 {color: #000000} --> </style> <head> <title></title> </head> <body> <form name="f1" method="post" action="addMobCodeAction"> <input type="hidden" name="method" value=""> <table width="692" border="0" cellspacing="0" cellpadding="0" align="center" background="images/front/center_bg.gif"> <tr> <td><span class="STYLE71">短信订阅</span></td> </tr> </table> <br> <table width="692" style="BORDER-COLLAPSE: collapse" borderColor="#111111" cellSpacing="0" borderColorDark="#d9dce8" borderColorLight="#d9dce8" border="1" align="center"> <tr bgcolor="#EAEFFC"> <td colspan="3" align="center"><span class="STYLE71">用户绑定手机号码</span></td> </tr> <tr bgcolor="#f9f9f9" onMouseOver="this.style.background='#ECEFF6'; " onmouseout ="this.style.background='#f9f9f9'; this.style.borderColor='#f9f9f9'"> <td height="25" align="right" width="37%">你所要绑定的移动手机号码:</td> <td width="30%"><input type="text" name="mob_code" value="" onKeyPress="onlyNumber(this.value);"></td> <td width="33%"><img src="images/front/queding.gif" width="63" height="20" name="bt1" onClick="addMob();" style="cursor:hand"></td> </tr> <tr bgcolor="#FFFFFF" onmouseover="this.style.background='#eeeeee'; " onmouseout ="this.style.background='#fff'; this.style.borderColor='#fff'"> <td width="37%" align="right" height="25">请输入你手机收到的验证码:</td> <td width="30%"><input type="text" name="check_num" value=""></td> <td width="33%"><img src="images/front/queding.gif" width="63" height="20" name="bt12" onClick="checkNum();" style="cursor:hand"></td> </tr> </table> </form> </body> </html> <script language="javascript"> function addMob(){ if(isMobileNo(f1.mob_code.value)){ f1.method.value="addMob"; f1.submit(); }else{ alert("您输入的手机号码错误,请重新输入"); f1.mob_code.focus(); return false; } } function checkNum(){ if(f1.check_num.value!=null && f1.check_num.value!=""){ f1.method.value="checkNum"; f1.submit(); }else{ alert("请输入验证码!"); f1.check_num.focus(); return false; } } </script> 然后提交到servlet也就是addMobCodeAction import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class AddMobCodeAction extends HttpServlet{ private static final String CONTENT_TYPE = "text/html; charset=GBK"; public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(CONTENT_TYPE); String method = request.getParameter("method"); if (method != null && method.equals("check")) { SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute("sessionInfo"); String url = ""; if(isMobile(sessionInfo.getMobCode())){ url = "userSmsCustomAction?method=search"; } else { url = "front/addMobCode.jsp"; } RequestDispatcher requestDispatcher = request.getRequestDispatcher(url); requestDispatcher.forward(request, response); } if (method != null && method.equals("addMob")) { PrintWriter out = response.getWriter(); String temp = addMob(request,response); if(temp.equals("1")){ out.println("<script language='javascript'>alert('验证码已经发出,请在下一行填写你手机收到的验证码!');history.go(-1);</script>"); out.close(); return; }else{ out.println("<script language='javascript'>alert('对不起,手机号码输入错误!');history.go(-1);</script>"); out.close(); return; } } if(method != null && method.equals("checkNum")){ PrintWriter out = response.getWriter(); String temp = checkNum(request,response); if(temp.equals("1")){ out.println("<script language='javascript'>"); out.println("alert('验证码正确,绑定手机号码成功!');window.close();"); out.println("window.location='userSmsCustomAction?method=search';"); out.println("</script>"); out.close(); return; }else{ out.println("<script language='javascript'>alert('验证码输入错误,请重新输入!');history.go(-1);</script>"); out.close(); return; } } if(method != null && method.equals("update")){ RequestDispatcher requestDispatcher = request.getRequestDispatcher("front/addMobCode.jsp"); requestDispatcher.forward(request, response); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * 绑定号码 */ private String addMob(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute("sessionInfo"); String mobCode = request.getParameter("mob_code"); if (isMobile(mobCode)) { SoapSendSms send = new SoapSendSms(); String randomNum = this.getRandom(); String msg = "用户短信订阅验证码: " + randomNum; send.validate(mobCode, msg);//调用Service接口,向用户发送验证码.; sessionInfo.setCheckNum(randomNum); return "1"; } else { return "0"; } } /** * 确认验证码 */ private String checkNum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { SessionInfo sessionInfo = (SessionInfo) request.getSession().getAttribute("sessionInfo"); String random_num = request.getParameter("check_num"); String mobCode = request.getParameter("mob_code"); if(random_num.equals(sessionInfo.getCheckNum())){ Database db = new Database(); try{ // SysUserUtil.updateMob(db,mobCode,sessionInfo.getLoginame()); 更新数据库 // sessionInfo.setMobCode(mobCode); 将手机号码写进session }catch(Exception e){ e.printStackTrace(); }finally{ db.cleanup(); } return "1"; }else{ return "0"; } } /** * 得到0..10000随机数 * @return int */ private String getRandom(){ double number=Math.random(); number = Math.ceil(number*10000); String str = String.valueOf((int)number); while(str.length()<4){ str = "0"+str; } return str; } /** * 判断是否是手机号码 * @param number * @return */ private boolean isMobile(String number){ boolean temp = false; if(number == null || number.equals("")){ temp = false; } else { if((number.indexOf("13") == 0 && number.length() == 11) || (number.indexOf("15") == 0 && number.length() == 11)){ temp = true; }else{ temp = false; } } return temp; } } 调用Service接口,向用户发送验证码 import java.rmi.RemoteException; import javax.xml.namespace.QName; import org.apache.axis.client.Call; import org.apache.axis.client.Service; //发送短信的SoapClient //authod sq public class SoapSendSms { private static String endpoint=""; private static Call call=null; public SoapSendSms() { try { Service service = new Service(); endpoint = "http://"; call=(Call)service.createCall(); call.setTargetEndpointAddress(endpoint); call.setMaintainSession(true); } catch(Exception ex) { } } private static Object setSoap(String OperationName,Object [] params) { try { call.setOperationName(new QName("",OperationName)); //调用服务 Object result; try { result=call.invoke(params); return result; } catch (RemoteException ex) { ex.printStackTrace(); return null; } } catch (Exception ex) { ex.printStackTrace(); // ServerConfig.logger.info(ex.getMessage()); return null; } } /** * 发送短消息 * smsno 手机号码 * machncode 车号 * msgcode 消息的编码 * mtsrc 定制类型 * content 发送的内容 */ public static Object sendSms(String smsno,String machncode,String msgcode,String mtsrc,String content) { Object[] params1=new Object[6]; params1[0]=new Integer(1); params1[1]=smsno; params1[2]=machncode; params1[3]=msgcode; params1[4]=mtsrc; params1[5]=content; return setSoap("sendSmsInfo",params1); } public static void validate(String smsno,String smsinfo) { Object[] params1=new Object[3]; params1[0]=new Integer(1); params1[1]=smsno; params1[2]=smsinfo; setSoap("validateSmsInfo",params1); } public static void main( String[] args ) { SoapSendSms sc=new SoapSendSms(); Object[] params1=new Object[6]; params1[0]=new Integer(1); params1[1]=""; params1[2]="10000"; params1[3]="123"; params1[4]="1"; params1[5]="show me"; //String a=(String)sc.setSoap("sendSmsInfo",params1); System.out.println(sc.sendSms("shouhao","test100001","hyd00","2","123")); } } 完成,有点乱。。。就这这个意思 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-02-24
这样丢出来有意思么
|
|
返回顶楼 | |
发表时间:2009-02-24
叫移动给你个短信网关
|
|
返回顶楼 | |
发表时间:2009-02-25
fjlyxx 写道 叫移动给你个短信网关 怎么要?能不能具体解释一下? |
|
返回顶楼 | |
发表时间:2009-02-25
elfer 写道 fjlyxx 写道 叫移动给你个短信网关
怎么要?能不能具体解释一下? 这是商务的问题,如果你做的是政府应用那很好办,只要移动公司给你开个帐号就OK了.移动有一个移动代理服务器MAS短信接入的接口. 联通也有,电信有企业通. 个人我还真不知道怎么申请. 说白了就是他们的短信网关. |
|
返回顶楼 | |
发表时间:2009-05-14
有没有适合个人的解决方案呢?
|
|
返回顶楼 | |