使用dwr框架实现登录用户名验证

1 篇文章 0 订阅

使用dwr框架实现Ajax(登录用户名验证)

 

1.        导入dwr.jar

 

2.        修改web.xml,添加代码

 <servlet>

        <servlet-name>dwr</servlet-name>

        <servlet-class>

            org.directwebremoting.servlet.DwrServlet

        </servlet-class>

        <init-param>

            <param-name>debug</param-name>

            <param-value>true</param-value>

        </init-param>

    </servlet>

    <servlet-mapping>

        <servlet-name>dwr</servlet-name>

        <url-pattern>/dwr/*</url-pattern>

    </servlet-mapping>

3.        /WEB-INF/下创建dwr.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

<dwr>

    <allow>

        <create creator="new" javascript="JUserChecker">

            <param name="class" value="t.UserManager" />

      <include method="check" />

        </create>      

    </allow>

</dwr>

 代码:    

package t;

publicclassUserManager {

publicboolean check(String uname){

    if(uname.equals("f")) returntrue;

    elsereturnfalse;

}

}

 

4.        jsp页面中使用

<%@ page language="java" pageEncoding="GBK"%>

<html>

    <head>

        <title>JSP for LoginForm form</title>

        <script type='text/javascript' src='dwr/interface/JUserChecker.js'></script>

        <script type='text/javascript' src='dwr/engine.js'></script>

        <script type='text/javascript' src='dwr/util.js'></script>

        <script>

            function checkU(oCtl){

                var uname=oCtl.value;

                JUserChecker.check(uname,haolejiaowo);

            }

            function haolejiaowo(isExist){

                if(isExist==1){

                alert("hao");

                }

                else if(isExist==0){

                alert("error");

                }

            }

</script>

    </head>

    <body>

        <form action="" name="form1">

       <input name="uname" onblur="checkU(this)">

       </form>

    </body>

</html>

 

使用Ajax技术实现页面动态添加下拉列表的值

1.        导入dwr.jar

 

2.        修改web.xml,添加代码

 

3.        /WEB-INF/下创建dwr.xml配置文件

同上:

  

package t;

 

import java.util.ArrayList;

 

publicclass UserManager {

    public String check(String uname){

//     if(uname.equals("f")) return "f用户";

//     else return "不是";

      

       ArrayList list=new ArrayList();

       list.add("1");

       list.add("2");

       list.add("3");      

       StringBuffer sb=new StringBuffer("");

       sb.append("<select name='mynum'>");

       for(int i=0;i<list.size();i++){

           sb.append("<option value="+(String)list.get(i)+">"+(String)list.get(i)+"</option>");

          

       }

       sb.append("</select>");

       return sb.toString();

    }

}

4.        jsp页面中使用

<%@ page language="java" pageEncoding="GBK"%>

<html>

    <head>

       <title>JSP for LoginForm form</title>

       <script type='text/javascript' src='dwr/interface/JUserChecker.js'></script>

       <script type='text/javascript' src='dwr/engine.js'></script>

       <script type='text/javascript' src='dwr/util.js'></script>

       <script>

           function checkU(oCtl){

              var uname=oCtl.value;

              JUserChecker.check(uname,haolejiaowo);

           }

           function haolejiaowo(isExist){

              document.getElementById("num").innerHTML=isExist;

           }

</script>

    </head>

    <body>

       <form action="" name="form1">

       <input name="uname" onblur="checkU(this)">

       <div id="num"></div>

       </form>

    </body>

</html>

 

使用Ajax技术传递List对象

 

1.        导入dwr.jar

 

2.        修改web.xml,添加代码

 

3.        /WEB-INF/下创建dwr.xml配置文件

 同上

package t;

 

import java.util.ArrayList;

import java.util.List;

 

publicclass UserManager {

    public List check(String uname){

      

       ArrayList list=new ArrayList();

       list.add("1");

       list.add("2");

       list.add("3");      

       return list;

    }

}

 

 

 4.        jsp页面中使用

<%@ page language="java" pageEncoding="GBK"%>

<html>

    <head>

       <title>JSP for LoginForm form</title>

       <script type='text/javascript' src='dwr/interface/JUserChecker.js'></script>

       <script type='text/javascript' src='dwr/engine.js'></script>

       <script type='text/javascript' src='dwr/util.js'></script>

       <script>

           function checkU(oCtl){

              var uname=oCtl.value;

              JUserChecker.check(uname,haolejiaowo);

           }

           function haolejiaowo(isExist){

              for(i=0;i<isExist.length;i++){

                  alert(isExist[i]);

              }

           }

</script>

    </head>

    <body>

       <form action="" name="form1">

       <input name="uname" onblur="checkU(this)">

       <div id="num"></div>

       </form>

    </body>

</html>

 

使用Ajax技术传递List对象,其中List封装了三个Person类的实例

 

1.        导入dwr.jar

 

2.        修改web.xml,添加代码

 

3.        /WEB-INF/下创建dwr.xml配置文件

 

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

<dwr>

    <allow>

       <create creator="new" javascript="JUserChecker">

           <param name="class" value="t.UserManager" />

           <include method="check" />

       </create>

<convert match="t.Person" javascript="Person" converter="bean" />

    </allow>

</dwr>

package t;

import java.util.*;

publicclass UserManager {

    public List check(String uname,String upass){

       ArrayList list=new ArrayList();

       list.add(new Person("name1","pass1"));

       list.add(new Person("name2","pass2"));

       list.add(new Person("name3","pass3"));

       return list;

    }

}


 

4.        jsp页面中使用

<%@ page language="java" pageEncoding="GBK"%>

<html>

    <head>

       <title>JSP for LoginForm form</title>

       <script type='text/javascript' src='dwr/interface/JUserChecker.js'></script>

       <script type='text/javascript' src='dwr/engine.js'></script>

       <script type='text/javascript' src='dwr/util.js'></script>

       <script>

           function checkU(oCtl){

              var uname=oCtl.value;

              JUserChecker.check(uname,haolejiaowo);

           }

           function haolejiaowo(isExist){

               for(i=0;i<isExist.length;i++){

                  alert(isExist[i].uname);

                  }

           }

</script>

    </head>

    <body>

       <form action="" name="form1">

       <input name="uname" onblur="checkU(this)">

       <div id="num"></div>

       </form>

    </body>

</html>

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值