往手机发验证码

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

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

完成,有点乱。。。就这这个意思

http://www.ppmy.cn/news/711974.html

相关文章

城市一账通收不到注册短信或者手机收不到短信

每个人手里都是有手机&#xff0c;有很多的app的注册、登录都是用手机号&#xff0c;因为这样更方便&#xff0c;不需要实时记录个人密码&#xff0c;特别是针对自己设置很多密码&#xff0c;容易忘记&#xff0c;只需要收到验证码即可授权登录&#xff0c;但是有一定的风险吧&…

手机验证码获取

<el-form-item label"短信验证码" required><el-input v-model"ruleForm.verificationcode" placeholder"请添加验证码"><el-button v-if"isdisabled" slot"suffix" style"color:#409EFF;" type&…

短信验证码泄露怎么办?

短信验证码泄露看起来不是啥大事&#xff0c;但是如果真的被别人恶意窃取了就不是个小事了&#xff0c;轻则账号丢失&#xff0c;重则账户中的钱财不保&#xff0c;所以对验证码大家一定要加强保护&#xff0c;不要被有心人窃取了。 那么&#xff0c;短信验证码为什么会泄露&a…

手机号码、验证码的处理

1、 验证手机号码是否正确 export function validPhoneNumber(phoneNumber) {return !!phoneNumber && /^1[3-9]\d{9}$/.test(phoneNumber); }2、验证输入密码是否符合规则 注&#xff1a;6到16位&#xff0c;必须包含数字&#xff0c;大小写字母 export function va…

浅谈手机验证码登录

注册和登录&#xff0c;是互联网产品的最基本功能&#xff0c;这里涉及到很多安全问题和用户便捷问题。今天&#xff0c;我们来简要聊一下手机验证码登录。 在之前的文章中&#xff0c;我们聊了注册登录原理及密码安全问题&#xff0c;这种方式是基于账号密码登录的。 然而&…

手机短信验证码发送

使用aliyun的短信发送服务 用户登录名称 sms-1271286242113840900.onaliyun.com AccessKey ID LTAI5t6jFYKGyZp529NCVV9d AccessKey Secret hYXc4KtpFiHELEmU5yuLCfstZM7zhS 签名 : XXX 模版CODE : SMS_186613739 配置文件 aliyun.properties aliyun.sms.regionIdsms-127128…

防止恶意频繁发送短信验证码

短信接口验证码是网站&#xff0c;App&#xff0c;微信端校验用户手机号码真实性的首要途径&#xff0c;在为用户提供便利的同时&#xff0c;手机短信验证功能也会被部分用户进行恶意使用。恶意频繁发送短信验证码&#xff0c;不仅会增加公司的运营成本&#xff0c;增加系统负载…

【SpringCloud-8】sleuth+zipkin分布式链路追踪

一&#xff1a;基本介绍 分布式链路追踪&#xff0c;听起来很高大上&#xff0c;其实也没什么。 说简单点&#xff0c;分布式系统中&#xff0c;一个请求可能会经过很多个服务。使用sleuth可以在这些服务上打一些日志&#xff08;不是我们自己打印的业务日志&#xff09;&#…