Use ajax verify whether to repeat the name of the database (only show ajax + servlet code)

script of the key code:

<script>
    //使用ajax验证数据库的名字是否重复
    $(function () {
			
        $('#Id').blur(function () {

            var Id = document.getElementById("Id");

            if (Id.value != "") {

                if (window.XMLHttpRequest) {
                    xml = new XMLHttpRequest();
                } else {
                    xml = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xml.onreadystatechange = function () {
                    if (xml.readyState == 4 && xml.status == 200) {
                        document.getElementById("AjaxInfo").innerHTML = xml.responseText;
                        //alert("ajax输出成功!");
                    }
                };
                xml.open("POST", "InsertServlet?Id=" + Id.value, true);
                xml.send();
            } else if (Id.value == "") {
                document.getElementById("AjaxInfo").innerHTML = "内容不能为空";
            }
        })

    })
</script>

The following are the key code of the servlet:

			request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");

            String Id=request.getParameter("Id");
            Id = new String(productId.getBytes("iso-8859-1"),"gb2312");

            ProductService productService=new ProductServiceImpl();
			ProductInfo productInfo=new ProductInfo();
            try {
                productInfo=productService.find(productId);

                if (productInfo==null){

                    response.getWriter().println("代码可用");

                }else{
                    response.getWriter().println("代码不可用");
                }
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("验证产生异常了");
            }
Published 108 original articles · won praise 46 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_44739706/article/details/104587105