Java Web開發如何在註冊和登入時對使用者名稱和密碼進行驗證?
阿新 • • 發佈:2019-01-07
1 註冊時,對註冊的手機號和密碼驗證:
手機號碼為常用的移動,聯通,電訊號
密碼為數字和字母的組合,切長度不能少於8位
方案
在提交註冊資訊時,用Ajax提交手機號和密碼到Servlet進行驗證
程式碼如下:
<form action="user.do" method="post"> <input id = "userPhone" type="text" name="userPhone" placeholder="手機號" style="width:330px;height:40px;margin-top:55px;border:#cfcfcf 1px solid;margin-left:35px;font-size:16px;padding-left:15px"/> <input id = "userPwd" type="password" name="userPwd" placeholder="密碼" style="width:330px;height:40px;margin-top:25px;border:#cfcfcf 1px solid;margin-left:35px;font-size:16px;padding-left:15px"/> </form> <button onclick="checkPhonePost()" style="width:340px;height:40px;background-color:#fb3268;color:#fff;border-radius:5px;margin-left:35px;margin-top:25px">立即註冊</button><br/>
<script type="text/javascript" src="js/jquery-3.1.1.js"></script> <script type="text/javascript"> function checkPhonePost(){ var userPhone=$("#userPhone").val(); var userPwd=$("#userPwd").val(); $.post("register_check.do", { userPhone:userPhone, userPwd:userPwd }, function(data,status){ if("cuccess"==data){ $("form").submit(); }else if("phone is wrong"==data){ alert("手機號格式不對"); }else{ alert("密碼格式不對"); } }); } </script>
public class RegisterCheckServlet extends HttpServlet { /** * */ private static final long serialVersionUID = -3093243729636232148L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String userPhone = req.getParameter("userPhone"); String phone_exp = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8}$"; String userPwd = req.getParameter("userPwd"); String password_exp = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$"; if(userPhone.matches(phone_exp)){ if(userPwd.matches(password_exp)){ resp.getWriter().write("cuccess"); }else{ resp.getWriter().write("password is wrong"); } }else{ resp.getWriter().write("phone is wrong"); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
2 登入時,驗證手機號是否已註冊,密碼是否正確,驗證碼是否正確
方案
和註冊時一樣,登入時也是使用Ajax提交資料到Servlet進行驗證
程式碼如下:
<form action="login.do" method="post">
<input id="userphone" name="userphone" type="text" placeholder="手機號" style="width:330px;height:40px;margin-top:55px;border:#cfcfcf 1px solid;margin-left:35px;font-size:16px;padding-left:15px"/>
<input id ="password" name="password" type="password" placeholder="密碼" style="width:330px;height:40px;margin-top:25px;border:#cfcfcf 1px solid;margin-left:35px;font-size:16px;padding-left:15px"/>
<div style="width:330px;height:40px;margin-top:25px;margin-left:35px;">
<div style="display:inline-block;width:215px;height:40px ">
<input id="code2" name="code1" type="text" placeholder="驗證碼" style="width:215px;height:40px;border:#cfcfcf 1px solid;font-size:16px;padding-left:15px;"/>
</div>
<div style="display:inline-block;width:80px;height:40px;margin-left:30px;vertical-align:top ">
<img id="code" alt="點選重新整理" src="code.do" onclick="refreshcode()" style=""/><br/>
</div>
</div>
<input type="checkbox" style="margin-top:25px;margin-left:35px;"/><span style="font-size:14px;color:#555555;margin-left:15px;">登入即表示同意<span style="color:#009fd7;"><a href="#" style="text-decoration:none">《註冊協議》</a></span></span><br/>
</form>
<button onclick="checkCodePost()" style="width:340px;height:40px;background-color:#fb3268;color:#fff;border-radius:5px;margin-left:35px;margin-top:25px">登入</button><br/>
function checkCodePost(){
var code2=$("#code2").val();
var userphone=$("#userphone").val();
var password=$("#password").val();
$.post("login_check.do",
{
code1:code2,
userphone:userphone,
password:password
},
function(data,status){
console.log(data);
if("cuccess"==data){
$("form").submit();
}else if("password is wrong"==data){
alert("密碼錯誤");
}else if("this user is not exist"==data){
alert("該使用者不存在");
}else{
alert("驗證碼不對");
}
});
}
public class LoginCheckServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -7891104898799649898L;
private IUserService service = new UserService();
private String srcPhone;
private String srcPwd;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userPhone = req.getParameter("userphone");
String passWord = req.getParameter("password");
List<UserBean> userList = service.queryUserList(userPhone);
for(UserBean user:userList){
srcPhone = user.getUser_phone();
srcPwd = user.getUser_password();
}
System.out.println(srcPhone+"----"+srcPwd);
String code1 = req.getParameter("code1");
String codeJ = new String(code1.getBytes("iso-8859-1"),"utf-8");
String code_src=(String) req.getServletContext().getAttribute("code");
if(srcPhone!=null){
if(code_src.equals(codeJ)){
if(srcPhone.equals(userPhone)&&srcPwd.equals(passWord)){
resp.getWriter().write("cuccess");
}else{
resp.getWriter().write("password is wrong");
}
}else{
resp.getWriter().write("code is error");
}
}else{
resp.getWriter().write("this user is not exist");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}