>  기사  >  Java  >  AJAX 비동기 제출

AJAX 비동기 제출

巴扎黑
巴扎黑원래의
2016-12-02 09:20:081134검색

사용자 이름 존재 여부를 ajax 실시간 확인

1. jsp 페이지

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ajax验证用户名是否可用</title>
<script type="text/javascript">
var xmlHttp = null;
function checkUserName(){
var userName = document.getElementById("userName").value;
if(window.ActiveXObject){
// 申明XMLHttpRequest对象,针对较低版本的IE(5,6,7)
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
}else{
// 申明XMLHttpRequest对象,针对较高版本的IE(8+,以及主流浏览器)
xmlHttp = new XMLHttpRequest();
}
// get方式提交,带参数,异步
xmlHttp.open("get","ajax?userName="+userName,true);
// 调用回调函数
xmlHttp.onreadystatechange = callback;
xmlHttp.send();
}
// 回调函数
function callback(){
if(xmlHttp.readyState == 4){
            if(xmlHttp.status == 200){
                // xmlHttp.responseText,获取后台返回信息
                document.getElementById("label").innerHTML = xmlHttp.responseText;
            }else{
                alert("AJAX服务器返回错误!");
            }    
        }
}
function checkForm(){
if(document.getElementById("label").innerHTML != "<font color=\"green\">用户名可用</font>"){
             document.getElementById("userName").focus();
             return false;
         }
return true;
}
</script>
</head>
<body>
<form action="login" method="post" onsubmit="return checkForm()">
用户名:<input type="text" onblur="checkUserName()" id="userName" name="userName">
<label id="label"></label><br>
密码:<input type="password"><br>
<input type="submit">
</form>
</body>
</html>

2. web.xml

  <!-- ajax获取信息 -->
  <servlet>
    <servlet-name>AjaxServlet</servlet-name>
    <servlet-class>com.servlet.AjaxServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>AjaxServlet</servlet-name>
    <url-pattern>/ajax</url-pattern>
  </servlet-mapping>

3. 🎜>

package com.servlet;
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 {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
if(userName.equals("admin")){
out.print("<font color=&#39;red&#39;>用户名已经存在</font>");
}
else if (userName.length() == 0) {
out.print("<font color=&#39;red&#39;>用户不能为空</font>");
}
else if (userName.indexOf(" ") > 0) {
out.print("<font color=&#39;red&#39;>用户不能含有空格</font>");
}
else if (userName.length() > 10 || userName.length() < 4) {
out.print("<font color=&#39;red&#39;>用户长度在4-10之间</font>");
}
else {
out.print("<font color=&#39;green&#39;>用户名可用</font>");
}
out.flush();
out.close();
}
}


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:스트링 인턴 방식다음 기사:스트링 인턴 방식