Ajax Asynchronous Loading

Ajax Asynchronous Interaction

xmlHttpRequest open (method, url, async) object request background

The Get and Post Get methods of method stitch parameters behind url s.With Post, case 1, you cannot use cached files (update files or databases on the server), 2 send a large amount of data to the server, 3, send user input with unknown strings

Onreadystatechange, requests sent to the server to perform some responsive tasks, and calls the onreadystatechange function whenever the readState property becomes

ResponsseText Gets Response Data as String

  <%
  Object obj = application.getAttribute("count2");
     if(obj==null){
   Integer count = 1;
   application.setAttribute("count2",count);
   
   }else{
   Integer count = (Integer)obj;
   count++;
   application.setAttribute("count2",count);
   }
   %>
   
 	function loadName(){
		var xmlHttp;
		if(window.XMLHttpRequest){
			xmlHttp=new XMLHttpRequest();
		}else{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		alert("readState Status:"+xmlHttp.readyState+";status Status:"+xmlHttp.status);
		xmlHttp.onreadystatechange=function(){
			alert("readState Status:"+xmlHttp.readyState+";status Status:"+xmlHttp.status);
			if(xmlHttp.readyState==4 && xmlHttp.status==200){
				alert(xmlHttp.responseText);
				document.getElementById("name").value=xmlHttp.responseText;
			}
		};
		// xmlHttp.open("get", "getAjaxName?name=jack&age=11", true);
		// xmlHttp.open("post", "getAjaxName?name=jack&age=11", true);
		// xmlHttp.send();
		
	    xmlHttp.open("post", "getAjaxName", true);
	    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	    xmlHttp.send("name=jack&age=11");
	}
	
<div style="padding: 10px;"><input type="button" style="border: 1px solid red;" value="Get Name" onclick="loadName()"/>&nbsp;&nbsp;<input type="text" id="name" name="name" value="${name }" style="border-bottom: 1px solid red"/>&nbsp;&nbsp;Number of page refreshes:<font color="red">${count2 }</font></div>

Backstage

public class GetAjaxNameServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String name=request.getParameter("name");
		String age=request.getParameter("age");
		System.out.println("name="+name);
		System.out.println("age="+age);
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		out.println("ajax Text returned");
		out.flush();
		out.close();
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Added by manlio on Wed, 29 Jan 2020 18:24:40 +0200