The
ajax local refresh has been introduced in previous articles, and the following is a login example to introduce its specific use
Apart, directly to the code, believe that need is the code rather than nonsense a bunch of ...... 1.java code: code as follows: Package tt; Import java.io.IOException; Import java.io.printwriter; Import javax.servlet.servletexception; Import javax.servlet.http.httpservlet; Import javax.servlet.http.httpservletrequest; Import javax.servlet.http.httpservletresponse; public class Testa extends HttpServlet { Private Static final Long Serialversionuid = -7999241892841130740l; protected void doget (HttpServletRequest request , httpservletresponse response) throws Servletexception, IOException { request.setcharacterencoding (" Utf-8 "); String name = Request.getparameter (" name "); Response.setcontenttype (" Text/xml; Charset=utf-8 "); response.setheader (" Cache-control "," No-cache "); printwriter out = Response.getWriter () ; out.println ("<pront>"); if (Name.equals ("Yangjinde")) { out.println ("<content>" +"Sorry, ' Yangjinde ' This name has been registered" + "</content>"); } else { out.println ("<content>" + "can register Oh" + "</ Content> "); } out.println (" </pront> "); out.close (); } } 2.jsp code: & nbsp The code is as follows: <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> <% String Path = Request . Getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" + Request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > <html> <head> <base href= "<%=basePath%>" > <title>my JSP ' index.jsp ' starting page</title> < Meta http-equiv= "Pragma" content= "No-cache" > <meta http-equiv= "Cache-control" content= "No-cache" > <meta http-equiv= "expires" content= "0" > <meta http-equiv= "keywords" content= "keyword1,keyword2" , Keyword3 "> <meta http-equiv= "description" content= "This are my page" > <script> //Set a variable var xmlhttpreq =false; //Create a XMLHttpRequest object function createxmlhttprequest () { if window. XMLHttpRequest) {//mozilla xmlhttpreq=new XMLHttpRequest (); } else if (window. ActiveXObject) { try{ xmlhttpreq=new activexobject ("Msxml2.xmlhttp"); }catch (e) { try{ Xmlhttpreq=new ActiveXObject ("Microsoft.XMLHTTP"); }catch (e) {} } } //Send request function function Send (URL) { createxmlhttprequest (); xmlhttpreq.open ("Get", Url,true); Xmlhttpreq.onreadystatechange=proce; Specifies the function of the response xmlhttpreq.send (NULL); Send request } function proce () { if (xmlhttpreq.readystate==4) {//Object state if (xmlhttpreq.status==200) {/ /information successfully returned, started processing information var res=xmlhttpreq.responsexml.getelementsbytagname ("content") [0].firstchild.data; Window.alert (res); document.getElementById ("Data"). InnerHTML = res; document.getElementById ("name"). Value = res; }else{ window.alert ("Sorry, the requested page has an exception"); }& nbsp } } //authentication function check () { var Name=document.getelementbyid ("name") .value; if (name==) ") { alert (" Please enter the content "); return false; } else{ send (' login?name= ' +name); } } </script> </head> <body> <form action= "Login" method= "post "> <tr><td> Name: <input id=" name "type=" text "name=" name "/> <input" button " Value= "I'll try it." Onclick= "Check ()"/> </td> </tr> <tr><td><div id= "data "> I will change!!" </div></td></tr> </form> </body> </html> 3. The XML configuration file code: copy code code as follows: <?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "2.4" xmlns= " Http://java.sun.com/xml/ns/j2ee " xmlns:xsi=" http://www.w3.org/2001/xmlschema-instance " xsi:schemalocation=" http://java.sun.com/xml/ns/j2ee http:// Java.sun.com/xml/ns/j2ee/web-app_2_4.xsd "> <servlet> <servlet-name>login</ servlet-name> <servlet-class>tt. testa</servlet-class> </servlet> <servlet-mapping> <servlet-name> login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </ welcome-file-list> </web-app>