24H免费课程咨询  TEL:13401595960   QQ:1870218756  微信:13401595960(李老师)

东方博宜

网站首页 > 软件开发资讯 > Java开发

JQuery Ajax案例(使用jsp + servlet编写)

2017-06-29 16:38:46 东方博宜 阅读

jquery ajax

例子:

index.jsp

        <style type="text/css">

           .error{

              color:#F00;

           }

        </style>

   

        用户名:<input type="text" name="username" />

        <span class="error"></span>

 

 

        <script type="text/javascript" src="js/jquery-1.7.2.js"></script>

        <script type="text/javascript">

            var jq = $.noConflict();

            jq(document).ready(function() {

                jq("[name='username']").blur(function(){

                     var name = jq(this).val();

                    

                     //如果填写了,则ajax校验

                     if(/^[ ]*$/.test(name) == false){

                         //清除可能存在的错误提示信息

                         jq(".error").html("检验中,请稍后……");

                        

                         //检验逻辑

                         jq.ajax({

                             //后台地址

                             url:"CheckNameServlet"

                             ,

                             //提交方式:getpost

                             type:"post"

                             ,

                             //data表示我们需要向后台传递的参数

                             data:{

                                username:name

                                ,

                                r:Math.random()

                             }

                             ,

                             //ajax处理结束(data表示后台的返回值)

                             success:function(data,status){

                                 if(data == "yes"){

                                    jq(".error").html("<span style='color:green'>用户名可用</span>");

                                 }else{

                                    jq(".error").html("用户名被占用,请尝试其他用户");

                                 }

                             }

                         });

                     }else{

                         jq(".error").html("用户名不能为空");

                     }

                });

            });

        </script>

 

CheckNameServlet

public class CheckNameServlet extends HttpServlet {

 

   

    public void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        this.doPost(request, response);

    }

 

   

    public void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

 

        response.setContentType("text/html;charset=utf-8");

        request.setCharacterEncoding("utf-8");

        PrintWriter out = response.getWriter();

       

        //获取参数

        String username = request.getParameter("username");

        System.out.println(username);

       

        if(username != null && (username.equals("管理员") || username.equals("admin"))){

            out.print("no");

        }else{

            out.print("yes");

        }

       

        out.flush();

        out.close();

    }

 

}

图片.png

Powered by 东方博宜教育咨询江苏有限公司  ©2008-2017 www.czos.cn