AJAX基础(二)

POST方法查询是否名字重复

jsp代码:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta charset = "utf-8">
  </head>
  
  <body>
     <form>
                     用户名:<input id = "inputID" type="text">
     </form>
     <br/>
     ----------------------------------------
     <br/>
     <span id = "spanID"></span>
     
     <script type="text/javascript">
        function createAJAX(){
           var ajax = null;
           try{
             ajax = new ActiveXObject("microsoft.xmlhttp");
           }catch(e){
             ajax = new XMLHttpRequest();
           }
           return ajax;
        }
     </script>
     
     <script type="text/javascript">
         document.getElementById("inputID").onblur = function(){
            var userName = document.getElementById("inputID").value;
            if(userName.length==0){
               document.getElementById("spanID").innerHTML = "请输入内容".fontcolor("red");
               return;
            }
            var ajax = createAJAX();
            
            var method = "POST";
            var content = "userName="+userName;
            var url = "${pageContext.request.contextPath}/AjaxServlet?time"+new Date().getTime();
            ajax.open(method,url);
            ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
            ajax.send(content);
            ajax.onreadystatechange = function(){
            if(ajax.readyState==4){
               if(ajax.status==200){
                 var str = ajax.responseText;
                 document.getElementById("spanID").innerHTML = str;
               }
            }
            }
         }
     </script>
  </body>
</html>

servlet代码:

package Ajax;

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 AjaxServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String userName = request.getParameter("userName");
		System.out.println(userName);
		String str = "<font color = 'green'>可以注册</span>";
		//模拟,没有从数据库中查询数据
		if("二丑".equals(userName)){
			str = "<font color = 'red'>该用户已经注册</span>";
		}
		PrintWriter writer = response.getWriter();
		writer.write(str);
		writer.flush();
		writer.close();
		System.out.println("执行完毕");
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String userName = request.getParameter("userName");
		System.out.println(userName);
		String str = "<font color = 'green'>可以注册</span>";
		if("二丑".equals(userName)){
			str = "<font color = 'red'>该用户已经注册</span>";
		}
		PrintWriter writer = response.getWriter();
		writer.write(str);
		writer.flush();
		writer.close();
		System.out.println("执行完毕");
	}

}

猜你喜欢

转载自blog.csdn.net/erchouchou/article/details/81459506