采用ajax与jquery结合的方式进行验证处理 js:
<script type="text/javascript"> $(function(){ $(":input[name='btnsubmit']").click(function(){ var flag = true; //1.获取文本框中的值 var obj={}; var username = $("input[name=username]").val(); var password = $("input[name=password]").val(); var url="loginAction_login.action"; obj.username=username; obj.password=password; $.post(url,obj,function(date){ if(date==1){ flag=false; $("input[name=username]").focus(function(){ $("input[name=username]").css("background-color","red"); }).focus(); $("#div1").html("用户名不存在"); return flag; }else if(date==2){ flag=false; $("input[name=password]").focus(function(){ $("input[name=password]").css("background-color","red"); }).focus(); $("#div2").html("密码错误"); return flag; }else{ window.location = date; return flag; } }); }); }); </script>html:
<div class="right_"> <div class="show"> <label>用户名</label><input type="text" class="text" name="username" placeholder="用户名"><span id="div1" style="color: red"></span> </div> <div class="show"> <label>密 码</label><input type="password" name="password" class="text" placeholder="密码" onkeydown="KeyDown(event)"> <span id="div2" style="color: red"></span> </div> <div class="show color"> <label> </label> <input type="checkbox" id="rpwd"><label class="simple" for="rpwd" style="text-align:left;">记住密码</label> <a href="#" style="color: gray">注册</a> <a href="#" style="color: gray">忘记密码</a> </div> <div class="show"> <label> <img src="themes/images/home/barcode.jpg" height="100px"></img> </label> <input type="button" name="btnsubmit" class="submit"value="登录" > </div> </div>“` action:
public String login() throws IOException{ String username = ServletActionContext.getRequest().getParameter(“username”); String password = ServletActionContext.getRequest().getParameter(“password”); ServletActionContext.getRequest().setCharacterEncoding(“UTF-8”); ServletActionContext.getResponse().setContentType(“text/html;charset=UTF-8”); PrintWriter out = ServletActionContext.getResponse().getWriter(); if(userService.getUsername(username)){ if(userService.getPassword(username, password)){ user=userService.findUserByName(username,password); SessionContext.setUser(user); out.print(“loginAction_success.action?type=3”); return null; }else{ out.print(“2”); } }else{ out.print(“1”); } return null; }