使用ajax来进行登录验证

servlet:

@WebServlet("/login.do")
public class AjaxLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        ResultMsg rm = new ResultMsg();
        PrintWriter out = response.getWriter();
        Gson gson = new Gson();

        String name = request.getParameter("userName");
        String pass = request.getParameter("userPass");
    
        //非空校验
        if(isempty(name)||isempty(pass)){
            rm.setMsg("user pass is null");
            rm.setResult("0002");
            
            out.println(gson.toJson(rm));
            return;
        }
        
        //去数据库确认一下用户是不是存在,并且登录成功
        if("dabu".equals(name)&&"123".equals(pass)){
            rm.setResult("0000");
        }else {
            rm.setMsg("user pass is wrong");
            rm.setResult("0001");
        }
        
        out.println(gson.toJson(rm));
        
        //form表单 a标签;  转发或者重定向
        //ajax的请求; out.println(json对象);
    }

    //判断一个字符串是不是为空
    private boolean isempty(String str){
        return null==str||"".equals(str.trim());
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

jsp里面的是重点:

<body>

    <!-- 提交的方式; get  post -->


    <form action="login.do" method="post">

        <!-- name : 对应我们servlet去获取前台文本框的值的 key -->
        用户名:<input id="nm" name="userName" type="text" value="${userName}" /> 
        密码:<input name="userPass" type="password" />
        <button type="button">登录</button>
        <label id="lab1"></label>

    </form>
</body>
<script type="text/javascript">

    $(function(){
        $("button").click(function(){
            
            //使用jQuery发送一个ajax请求
            var name=$("#nm").val();
            var pass=$("input[name=‘userPass‘]").val();
            
            
            
            /* 
            使用get请求
            $.get("login.love?userName="+name+"&userPass="+pass,function(obj){ //obj:后台返回给我的哪个json字符串
            
            var json = JSON.parse(obj);
        
            if(json.result == "0000"){
                location.href="http://www.baidu.com";
            }else if(json.result == "0001"){
                alert(json.msg);
            }else{
                alert(json.msg);
            }
        }); */
        
        /* 
        使用post请求
        $.post("login.love",{"userName":name,"userPass":pass},function(obj){ //obj:后台返回给我的哪个json字符串
            
            var json = JSON.parse(obj);
        
            if(json.result == "0000"){
                location.href="http://www.baidu.com";
            }else if(json.result == "0001"){
                alert(json.msg);
            }else{
                alert(json.msg);
            }
        }) */
        
            
            
            $.ajax({
                
                
                url:"login.do",//目标地址
                type:"post",//请求方式
                data:{"userName":name,"userPass":pass},//请求参数
                success:function(obj){//obj:后台返回给我们的一个json的字符串
                    
                    var json=JSON.parse(obj);
                
                if(json.result=="0000"){
                    location.href="http//www.baidu.com";
                }else if (json.result=="0001") {
                    alert(json.msg);
                }else{
                    alert(json.msg);
                }
                    
                }
                
            });
            
            
        });
    });

</script>

使用ajax来进行登录验证