UserServlet代碼public?class?UserServlet?extends?BaseServlet?{
/**
?*?登錄的功能
?*/
public?String?login(HttpServletRequest?req,?HttpServletResponse?resp)?{
//?7.驗(yàn)證碼驗(yàn)證
//?7.1獲取驗(yàn)證碼
String?code?=?req.getParameter("code");
//?7.2獲取生成的驗(yàn)證碼
String?rand?=?req.getSession().getAttribute("IdentifyCode").toString();
//?8.檢查驗(yàn)證碼
if?(code?!=?null?&&?!code.trim().equals(""))?{
if?(!code.equalsIgnoreCase(rand))?{
try?{
resp.getWriter().write("codeFalse");
}?catch?(IOException?e)?{
e.printStackTrace();
}
}?else?{
//?1.接收數(shù)據(jù)
Map<String,?String[]>?map?=?req.getParameterMap();
User?user?=?new?User();
System.out.println("獲取數(shù)據(jù):-------"?+?map);
//?2.封裝數(shù)據(jù)
try?{
BeanUtils.populate(user,?map);
//?3.調(diào)用Service層處理數(shù)據(jù)
UserService?us?=?new?UserService();
User?existUser?=?us.login(user);
System.out.println("=======existUser========"?+?existUser);
if?(existUser?==?null)?{
//?用戶登錄失敗
//?req.setAttribute("msg",?"用戶名或密碼錯(cuò)誤!");
resp.getWriter().write("LoginFail");
//?"/login/login2.jsp"
}?else?{
req.getSession().setAttribute("existUser",?existUser);
System.out.println("------用戶登錄成功!?。。?!-------");
resp.getWriter().write("LoginSuc");
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
}
return?null;
}
return?null;
}
}jsp頁面<%@?page?language="java"?import="java.util.*"?pageEncoding="UTF-8"%>
<html>
<head>
<title>登陸操作</title>
<meta?http-equiv="content-type"?content="text/html;?charset=utf8"?/>
<link?rel="stylesheet"?type="text/css"?href="Styles/base.css"?/>
<link?rel="stylesheet"?type="text/css"?href="Styles/admin-all.css"?/>
<link?rel="stylesheet"?type="text/css"?href="Styles/bootstrap.min.css"?/>
<link?rel="stylesheet"?type="text/css"?href="Styles/login.css"?/>
<script?type="text/javascript"?src="Scripts/jquery.js"></script>
<script?type="text/javascript"?src="Scripts/jquery.spritely-0.6.js"></script>
<script?type="text/javascript"?src="Scripts/chur.min.js"></script>
<script?type="text/javascript">
$(document).ready(function(){
$.ajaxSetup({cache:false});//清楚ajax
//刷新驗(yàn)證碼
$("#randCode,#randCode2").click(function(e){
$("#randCode").attr("src","RandCodeServlet?t="+new?Date().getTime());
//阻止超鏈接默認(rèn)行為
e.preventDefault();
});
</script>
</head>
<body>
<div?id="clouds"?class="stage"></div>
<div?class="loginmain"></div>
<div?class="row-fluid">
<form?name="form1"?method="post"
action="${pageContext.request.contextPath?}/user">
<input?type="hidden"?name="method"?value="login"?/>
<h1>聊?天?系?統(tǒng)</h1>
<p>
<label>帳 號(hào):<input?type="text"?id="username"
name="username"?/>?</label>
</p>
<p>
<label>密 碼:<input?type="password"
id="password"?name="password"?/>?</label>
</p>
<p?class="pcode">
<label> 驗(yàn) 證 碼:<input
type="text"?id="txtCode"?name="code"?maxlength="4"?class="code"?/>
<img?id="randCode"?src="RandCodeServlet"?title="看不清換一張"
class="imgcode"?/>?<a?id="randCode2"?href="RandCodeServlet">換一張</a>
</label>
</p>
<p?class="tip"?id="divMsg"> </p>
<hr?/>
<input?id="submit"?type="submit"?value="?登?錄?"
class="btn?btn-primary?btn-large?login"?/>? <input
id="button2"?type="button"?value="?注?冊(cè)?"?class="btn?btn-large"?/>
</form>
</div>
</body>
</html>
<script>
</script>想用ajax+jquery對(duì)登錄檢驗(yàn),登陸成功,登陸失敗,賬號(hào)密碼驗(yàn)證碼不能為空的檢查,然后顯示在頁面上的<p class="tip" id="divMsg"> </p>位置以下是自己寫的ajax+jquery的代碼,可是無法實(shí)現(xiàn),有人幫我修改一下嗎?? //登陸的業(yè)務(wù)操作
$("#submit").click(function(){
var?name=$("#username").val();
var?pass=$("#password").val();
var?code=$("#txtCode").val();
alert("name="+name+"pass"+pass+"code"+code);
if(name!=""?&&?pass!=""?&&?code!=""){
//發(fā)出ajax請(qǐng)求
$.ajax({
type:"post",
url:"${pageContext.request.contextPath?}/user?method=login",
data:"action=Login&d="+(new?Date()).valueOf()+"&name="+name+"&pass="+pass+"&code"+code,
success:function(responseText){
alert("--------"+data+"--------");
if(responseText=="LoginSuc"){
$("#divMsg").show().html("登陸成功!");
//登陸按鈕變色
$("#submit").attr("disabled","disabled");
window.location="chatRoom.jsp";
}else?if(responseText=="LoginFail"){
//登陸失敗
$("#divMsg").show().html("登陸失敗,請(qǐng)重新登陸!");
return?false;
}else?if(responseText=="codeFalse"){
$("#divMsg").show().html("驗(yàn)證碼錯(cuò)誤!");
return?false;
}else?if(responseText=="LoginNot"){
$("#divMsg").show().html("你已經(jīng)登陸過了");
return?false;
}
},
error:function(xmlHttpReq){
$("#divMsg").show().html("網(wǎng)絡(luò)故障:"+xmlHttpReq.status);
}
?
});
}else{
if(name=""){
$("#divMsg").show().html("賬號(hào)不能為空");
return?false;
}
if(pass=""){
$("#divMsg").show().html("密碼不能為空");
return?false;
}
if(code=""){
$("#divMsg").show().html("驗(yàn)證碼不能為空");
return?false;
}
}
});
});等求解,謝謝謝謝?。。〉惹蠼?,謝謝謝謝!??!等求解,謝謝謝謝?。。?
用JavaWeb開發(fā)登錄功能使用學(xué)習(xí)ajax+jquery遇到點(diǎn)麻煩,在線等求解
慕粉3505864
2017-12-07 11:53:15