Ajax with jsp : simple example

HTML Page
<
html>
    <head><title>welcome</title>
    <script language="javascript">
        reqObj=null;
        function varify(){
            document.getElementById("res").innerHTML="Checking";
            if(window.XMLHttpRequest){
                reqObj=new XMLHttpRequest();
            }else {
                reqObj=new ActiveXObject("Microsoft.XMLHTTP");
            }
            
            reqObj.onreadystatechange=process;
            reqObj.open("POST","./a.jsp?id="+document.getElementById("username").value,true);
            reqObj.send(null);
        }
        function process(){
            if(reqObj.readyState==4){
                document.getElementById("res").innerHTML=reqObj.responseText;
            }
        }
        </script>
    
    </head>
    <body>
        <h1>welcome to this application</h1>
    <form method="get" action="first">
        User Name<input type="text" name="t1" id="username" onblur="varify();"><span id="res"></span><br/>
        Password<input type="text" name="t2"><br/>
        <input type ="submit" value="Press"/>
    </form>
    </body>
</html
>


JSP Page
<%
String user=request.getParameter("id");
try{Thread.sleep(5000);}catch(Exception e){}
if(user.equals("abc")){
%>
<font color="red" ><strong>User already exists</strong></font>
<%
}else{
%>
<font color="green" ><strong>User  name available</strong></font>
<%
}
%>

1 comment:

Anonymous said...

You actually make itt appear so easy along with your presentation however I fid this matter to be really something that I feel I would never understand.
It kind of feels too complicated and extremely wide for me.
I am looking ahead in your subsequent post, I will attempt to
get the cling of it!

Popular Posts