Ajax与Java交互简单用例

来源:互联网 发布:linux 删除虚拟网卡 编辑:程序博客网 时间:2024/05/17 21:52

Ajax与Java交互简单用例


第一次写博客呀,好激动,现在是凌晨一点五十。开始正题最近在写一个小的项目,需要使用Ajax来将数据从前台页面传送到后台,于是学习了一些Ajax便开始写了,下面是一些总结:**Ajax带着数据请求到服务器后,又会带着数据回到当前页面,所以不能使用servlet进行请求的转发****AJAX 是与服务器交换数据并更新部分网页的艺术,在**不重新加载整个页面**的情况下,通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。和服务器进行**异步通信**

传统的网页(不使用 AJAX)如果需要更新内容,必需重载整个网页面。**
## 1.页面js代码 ##

function onSubmit(){                    var form_data=$('#form表单id').serialize();//获得表单中所有数据                    var option={                            url:'/questionnaire/UserController',                            type:'POST',                            dataType:'json',                            data:form_data,                            success:function(result){                                if(result){                                    window.location.href="页面url";                                }else{                                    alert("登陆失败,重新输入");                                }                            }                    };                    $.ajax(option);                    }    form id="account_info_mobile" action="" method="post" style="display: block;">                            <input type="hidden" name="mathed" value="userLogin">                        <ul>                            <li style="margin-top: 30px;">                                <input type="text" id="mobile" name="mobile" class="requested" placeholder="手机号/邮箱" onblur="onBlur(id)"/>                            </li>                            <div class="font" style="margin-top:0px;margin-bottom: 7px;">                            <span id="userText" class="font1"></span>                            </div>                            <li>                                <input type="password" id="psw" name="password" class="requested" placeholder="密码" onblur="onBlur(id)"/>                            </li>                            <div class="fontt" style="margin-top:3px;">                            <span id="psdText" class="font2"></span>                            </div>                        </ul>                        </form>                </div>                <a href="findPsw.jsp" style="float: right;">忘记密码</a>                <button onclick="onSubmit()" type="button" id="submit" class="btn btn-info" style="margin-left: 50px;width: 140px;height: 50px;text-align: center;font-size: 20px;margin-top: 10px;letter-spacing: 5px;" disabled="disabled">登录</button>

点击按钮触发事件,想url所指向后台地址发起post请求,
2. java后台代码,使用阿里fastjson处理json数据,需要先导入.alibaba.fastjson架包
获得页面数据 request.getParameter("name属性值");

response.setContentType("application/json;charset=utf-8");        boolean b=false;        String res=JSON.toJSONString(b);        if(pwd!=null&&repwd!=null&&pwd.equals(repwd)){            User u=new User();            u.setUserPwd(pwd);            System.out.println("session中userId:"+request.getSession().getAttribute("userId"));            u.setUserId((String)request.getSession().getAttribute("userId"));            ResultDao<Boolean> result=userService.resetPsw(u);            if(result.isSuccess()){                request.getSession().setAttribute("userId", u.getUserId());                b=true;                res=JSON.toJSONString(b);            }        }        PrintWriter out = response.getWriter();        out.write(res);        out.flush();        out.close();

用例到此结束,很简单的一个用例

原创粉丝点击