Ajax 五种方式 实现异步刷新

先创建一个Servlet来充当服务器

package com.xbj.demo;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/moblie_text")
public class moblie_text extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.setCharacterEncoding("utf-8");
	response.setCharacterEncoding("utf-8");
	response.setContentType("text/html;charset=utf-8");
		String mobile = request.getParameter("mobile");
		System.out.println(mobile);
		PrintWriter writer = response.getWriter();
		if ("123456789".equals(mobile)) {
			//System.out.println("true");
			writer.write("true");
			//writer.write("此号码已经存在");
			
		}else {
			//writer.write("注册成功");
			writer.write("false");
			//System.out.println("false");
		}
	
	writer.close();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

第一种:用js实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here



手机号:


第二种 也是最常用的一种 用JQuery实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here
//不要忘记导jquery




手机号:


第三种方式:用get 实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here




手机号:


第四种方式 用post 和get 雷同 名字换一下即可

第五种方式 用load 方式实现

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here




手机号:

 

你可能感兴趣的:(Ajax)