Question : how to use AJAX here with jsp?

how to use ajax for this sign_up page which i have attached over here?
i want the funcionality to check if username already exists or as soon as mouse is moved to another field,the previous field should be validated.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<%@page language="java" %>
 

 

    
        
        Sign Up Page
        
    
    
        
            

Personal Details:

First name:
Last name:
Gender:
Country:
E-mail:


Account Information:

Username:
Password:
Confirm Password:

in case you forget your password :

Your Question:
Your Answer:
<% String fname1 =request.getParameter("fname"); String lname1=request.getParameter("lname"); String cname1=request.getParameter("cname"); String em1 =request.getParameter("email"); String un=request.getParameter("uname"); String psw1=request.getParameter("psw"); String con_psw1=request.getParameter("con_psw"); String ans1 =request.getParameter("ans"); String u=""; int flag=0; if(un=="") {%> <% } else if(fname1=="" || lname1=="" || cname1=="" || em1=="" || psw1=="" || con_psw1=="" || ans1=="") { %> <% } else if(psw1!=con_psw1) { %> <% } try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String drv = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=C:/Program Files/Apache Software Foundation/Tomcat 5.5/webapps/ROOT/virtual lab/virtual.mdb"; Connection con=DriverManager.getConnection(drv,"",""); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from user"); while(rs.next()) { u = rs.getString("username"); if(un.equals(u)) { flag = 1; break; } } rs.close(); if(flag==1) { %> <% } } catch(Exception e) { // out.println(e); } %>
Open in New Window Select All

Answer : how to use AJAX here with jsp?

Add an onChange or OnBlur event to the input fields you want validated.  In the Javascript functions, make Ajax calls to the server for the validation.

For help with Javascript and onBlur:
http://www.w3schools.com/jsref/jsref_onblur.asp

For help with Ajax:
http://java.sun.com/developer/technicalArticles/J2EE/AJAX/RealtimeValidation/
Random Solutions  
 
programming4us programming4us