往手机发验证码

前段时间因为项目的需要涉及到了一个往手机上发验证码的项目,代码贴出来,希望指点。。。。。

首先是页面,此页面是用于输入用户的手机号码: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" οnmοuseοver="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"));
}
}

完成,有点乱。。。就这这个意思
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值