基于Ajax的用户名可用性检查,然后产生一些用户名在JSP中使用 [英] Ajax based username availablity checking and then generating some username to use in jsp

查看:111
本文介绍了基于Ajax的用户名可用性检查,然后产生一些用户名在JSP中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有句话做了一个基于AJAX JSP代码来检查用户名的可用性。它完美地工作。虽然它可能有一些错误。因此,如果有任何请指出来。现在我的主要问题是,在检查后,我也想产生一些用户名用户的使用就像我们可以在Gmail的登记表看到它。我在这里给我的code ...

i have somewhat done a ajax based jsp coding for checking username availability. It worked perfectly. Though it might have some errors. So if there any please point it out. Now my main question is that after the checking i also want generate some usernames for users to use like we can see it at gmail registration form. I am giving my code here...

一中的index.html ...

First the index.html...

    <html>
<body>
<form action=sample.jsp method=post>
id<input type=text name=id><br>
<input type=submit value=next>
</form>
</body>
</html>

现在sample.jsp ...

Now sample.jsp...

<jsp:useBean id="ob" class="sample.Database" scope="session" />
<jsp:setProperty name="ob" property="*" />
<html>
<head>
var xmlHttp  
    var xmlHttp
    var checkvalue

    function showState(p1){ 
        if (typeof XMLHttpRequest != "undefined"){
            xmlHttp= new XMLHttpRequest();
            }
            else if (window.ActiveXObject){
            xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
            }
        if (xmlHttp==null){
                alert ("Browser does not support XMLHTTP Request")
            return
        } 
        var url="getlist.jsp?name="+p1;
        xmlHttp.onreadystatechange = stateChange;
        xmlHttp.open("GET", url, true);
        xmlHttp.send(null);
    }

    function stateChange(){   
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){   
            checkvalue=document.getElementById("name").innerHTML=xmlHttp.responseText;
            document.f1.checking.value = xmlHttp.responseText; 
        }   
    }

    function conditions(){
        if(!checkvalue.match("Available"))
        {
            alert("no match");
            return false
        }
    }

</script>  
</head>  
<body >  
<center>
<br>  
<form action="sample1.jsp" name="f1" method="post" onsubmit="return conditions()">
name <input type=text name=name onchange="showState(this.value);"><div id='name'></div><br>
checking<input type=text name=checking><br>
    <input type=submit value=next>



</form>
</body> 
</html>

现在getlist.jsp ...

now getlist.jsp...

    <jsp:useBean id="ob" class="sample.Database" scope="session" />
<%
    String n=request.getParameter("name");
    if(ob.checkName(n)==0)
    {
%>
        <font color=green>Available</font>
<%
    }
    else
    {
%>
        <font color=red>Not available</font>
<%
    }
%>

现在sample1.jsp ....

now sample1.jsp....

<jsp:useBean id="ob" class="sample.Database" scope="session" />
<jsp:setProperty name="ob" property="*" />
<%
    if(ob.insertData()==1)
        out.println("Success");
    else

        out.println("Unsuccess");
%>

在包 - >样品filename-> Database.java ...

code for class file in package->sample filename->Database.java...

package sample;

import java.util.*;
import java.sql.*;

public class Database
{
    private String id="",name="";
    private int t=0;
    private Connection con;

    public void setName(String name)
    {
        this.name=name;
    }

    public String getName()
    {
        return name;
    }

    public void setId(String id)
    {
        this.id=id;
    }

    public String getId()
    {
        return id;
    }

    public Connection c1()
    {
        try{
            Class.forName("oracle.jdbc.OracleDriver");
            con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1000:xe","system","12345");
        }
        catch(Exception e){}
        return con;
    }

    public int checkName(String n)
    {
        try{
            con=c1();
            PreparedStatement pst = con.prepareStatement("select name from testani where name='"+n+"'");
            t = pst.executeUpdate();
            con.close();
        }
        catch(Exception e){}
        return t;
    }

    public int insertData()
    {
        try{
            con=c1();
            PreparedStatement pst = con.prepareStatement("insert into testani values('"+name+"','"+id+"')");
            t = pst.executeUpdate();
            con.close();
        }
        catch(Exception e){}
        return t;
    }
}

我已经通过创建带有属性的名称和ID的数据库名为testani MySQL的...

I have created a database named testani with attributes name and id by using mysql...

现在我怎么能修改此code,产生一些名字....在此先感谢。

Now how can i modify this code to generate some names.... Thanks in advance..

推荐答案

我猜你需要使自己的算法名建议。

I guess you need to make your own algorithm for name suggestion.

一旦用户输入自己的姓名,出生年份,等等等等。那么你可以设计一个算法,可以把这些价值观,并做一些处理相结合,要生成建议的用户名(串联)字样或出生年份或任何东西。

Like once the user enters his name,surname, birth year,etc. then you can design an algorithm that can take these values and do some processing by combining(concatenating) words or birth year or anything that you want to generate suggested usernames.

然后,你可以简单地返回该列表中的用户。

Then you can simply return that list to the user.

这篇关于基于Ajax的用户名可用性检查,然后产生一些用户名在JSP中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆