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…
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>
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>
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>
<%
}
%>
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");
%>
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;
}
}
I have created a database named testani with attributes name and id by using mysql…
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.