JavaScript events and Ajax required in JSP pages
1 <Scripttype= "Text/javascript">2 functionCheckempno (ID) {3 varEmpnovalue=document.getElementById (ID). value;4 $.ajax ({5 Type:'Post',6 URL:'employee_checkempnoandempname.htm',7 Data:'empno='+Empnovalue,8 Success:function(Result) {9 if(Result=="the user name does not exist! "){Ten Alert ("the user name does not exist! "); One document.getElementById (ID). Value=""; A document.getElementById ("EmpName"). Value=""; - document.getElementById (ID). focus (); - }Else{ the document.getElementById ("EmpName"). Value=result; - document.getElementById ("Entrancestart"). focus (); - } - }); + </Script> - <form> + <Table> A <TR> at <TD><inputID= "EmpNo"onblur= "Checkempno (this.id)"type= "text"name= "EmpNo" /></TD> - <TD><inputID= "EmpName"type= "text"name= "EmpName" /></TD> - </TR> - </Table> - </form>
The code required for the background action:
PublicClasee empactionextendsactionsupport{ PublicString Checkempnoandempname ()throwsException {httpservletrequest request=servletactioncontext.getrequest (); HttpServletResponse respons=Servletactioncontext.getresponse (); String EmpNo= Request.getparameter ("EmpNo")///Gets the empno Respons.setcontenttype of the action that the page submits to the background ("Text/html;charset=utf-8");//Set String SENDSTR for the character set of the response= ""; List<EmployeeInfo> employeeinfolist = This. Empservice.checkempno (EmpNo);//Call the business to query this user based on the EMPNO layer Try{PrintWriter out=respons.getwriter ();//More To print objectsif(Employeeinfolist.size () >0) {Sendstr= Employeeinfolist.get (0). Getempname (); } Else{sendstring= "The user name does not exist!" "; } out.print (SENDSTR);//The background dynamic SENDSTR output to the JSP page Out.flush (); Out.close (); } Catch(Exception e) {e.printstacktrace (); } return NULL; }}
The AJAX submission data determines whether the employee number exists and automatically fills in the employee name corresponding to the employee number.