这里演示了用户注册界面,手机号注册,通过阿里云短信服务发送短信,接收验证码并进行验证,里面有整个流程的一套代码,运行真实有效,说明请配合具体我写的文章
package com.shao.web.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.shao.Util.RandomNum;import com.shao.Util.SendCode;//import com.shao.pojo.User;//import com.shao.service.UserService;//import com.shao.serviceImpl.UserServiceImpl;/** * Servlet implementation class SendCodeServlet * 找回密码发送验证码 */@WebServlet("/SendCodeServlet")public class SendCodeServlet extends HttpServlet {private static final long serialVersionUID = 1L;/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// TODO Auto-generated method stubrequest.setCharacterEncoding("UTF-8");response.setCharacterEncoding("UTF-8");String phone = request.getParameter("phone");//UserService us = new UserServiceImpl();//User user = us.selectByName(phone);//if(user != null){RandomNum randomNum = new RandomNum();RandomNum.num = randomNum.getRandom();System.out.println("send" RandomNum.num);try {SendCode.sendSms(phone, RandomNum.num,"SMS_110470024"); //调用短信发送接口,三个参数,手机号,验证码,短信模板} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}request.setAttribute("msg", "验证码发送成功!");request.setAttribute("phone", phone);request.getRequestDispatcher("/adduser.jsp?phone=" phone).forward(request, response);System.out.println(phone);//}else{//request.setAttribute("msg", "该用户不存在!请先注册");//request.setAttribute("phone", phone);//request.getRequestDispatcher("/admin/login/vfc.jsp?phone=" phone).forward(request, response);//}}// }/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// TODO Auto-generated method stubdoGet(request, response);}}
评论