AJAX与JSP结合实例,一个很简单实用的例子:身份证号码检验

这个例子用途:判断输入的身份证号码是否正确,这里能够检查出长度错误,日期错误,验证码错误。
先做一个普通网页 ajaxtest.jsp

<%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*” errorPage=”” %>
<!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=gb2312″>
<jsp:useBean id=”bb” scope=”page” class=”lh.LHDBconnection”/>
<title>无标题文档</title>
</head>

<body>

<form name=”form1″ method=”post” action=””>
<table width=”546″ border=”1″ bordercolor=”#6699CC” bordercolordark=”#003399″>
  <tr>
    <td>身份证号码</td>
    <td><input name=”idno” type=”text” id=”idno”>
        <input name=”Submit3″ type=”button” value=”检查是否正确” onClick=”javascript:checkidno();”>
    </td>
  </tr>
</table>
</form>

<script type=”text/javascript” language=’javascript’>
<!–

function checkidno(){

//本来一句话就可以,这里啰嗦一堆都是为了解决浏览器兼容性问题.
var xmlhttp;
try{
    xmlhttp=new ActiveXObject(‘Msxml2.XMLHTTP’);
} catch(e){
                try{
                        xmlhttp=new ActiveXObject(‘Microsoft.XMLHTTP’);
                    } catch(e){
                                    try{
                                            xmlhttp=new XMLHttpRequest();
                                        }catch(e){}
                            }
            }
           
var rrr=””;           
var idno=document.form1.idno.value;
//这里不用刷新整个页面,就可以直接调用jsp文件了,太方便了
xmlhttp.open(“get”,”/comm/checkidno.jsp?_idno=”+idno,true);
xmlhttp.onreadystatechange=function(){

        if (xmlhttp.readyState==4)
        //xmlhttp.status==404 代表 没有发现该文件
        if (xmlhttp.status==200)
        {
            //alert(xmlhttp.status);
            rrr=xmlhttp.responseText;   
            //if(rrr==”ok”) {alert(“ok”)  ;  else alert(rrr);
            //alert(rrr);
        } else
        {
            alert(“发生错误:”+xmlhttp.status);
        }

}

xmlhttp.send(null);
alert(rrr);

return false;
}

//–>
</script>
</body>
</html>

然后是一个被调用的JSP文件 checkidno.jsp
这个验证有点复杂,你可以简化一下,当然里面也可以进行数据库操作

<%@ page contentType=”text/html; charset=gb2312″ language=”java” import=”java.sql.*,java.util.*” errorPage=”” %>
<%

String errormessage=””;
String idno=request.getParameter(“_idno”);
 System.out.println(“hello”);
 //out.write(“hello”);

Calendar now=GregorianCalendar.getInstance();
int iyear=now.get(Calendar.YEAR);
String  year=Integer.toString(iyear);
String  bird=””;
String  birthday=””;

if (idno.length()!=15&&idno.length()!=18) { errormessage=errormessage+”身份证号长度错误!”;}
else
{
        String addr=idno.substring(0,6);
        String last=””;
       
        if (idno.length()==15) {     
              bird=idno.substring(6,12);
              birthday=”19″+bird.substring(0,2)+”-“+bird.substring(2,4)+”-“+bird.substring(4,6);
              last=idno.substring(12,15);
                 
              if ((Integer.parseInt(“19″+bird.substring(0,2))<=1900) ||  (Integer.parseInt(“19″+bird.substring(0,2))>=2000) ) errormessage=errormessage+”年份错误!”;
              if ((Integer.parseInt(bird.substring(2,4))<1) ||  (Integer.parseInt(bird.substring(2,4))>12) ) errormessage=errormessage+”月份部分错误!”;
              if ((Integer.parseInt(bird.substring(4,6))<1) ||  (Integer.parseInt(bird.substring(4,6))>31) ) errormessage=errormessage+”日期部分错误!”;
        }
       
        if (idno.length()==18) {
              bird=idno.substring(6,14);
              birthday=bird.substring(0,4)+”-“+bird.substring(4,6)+”-“+bird.substring(6,8);
              last=idno.substring(14,17);
              //System.out.println(“birthday==”+bird.substring(4,6));
             
              if ((Integer.parseInt(bird.substring(0,4))<=1900) ||  (Integer.parseInt(bird.substring(0,4))>=iyear) ) errormessage=errormessage+”年份错误!”;
              if ((Integer.parseInt(bird.substring(4,6))<1) ||  (Integer.parseInt(bird.substring(4,6))>12) ) errormessage=errormessage+”月份部分错误!”;
              if ((Integer.parseInt(bird.substring(6,8))<1) ||  (Integer.parseInt(bird.substring(6,8))>31) ) errormessage=errormessage+”日期部分错误!”;
             
         }
       
        //ResultSet sqlrst=bb.executeQuery(“select * from hr_idnosource where ccodeid=”+”‘”+idno.substring(0,5)+”‘”;
       
        if (errormessage.equals(“”)){
                try {
                   // 如果输入日期不是8位的,判定为false.
                   //  || !birthday.matches(“[0-9]{8}”)
                    if (null == birthday || “”.equals(birthday) ) {
                         errormessage=errormessage+”日期非法!”;
                    }
                    
                   
                        
                    //1978-02-06
                    System.out.println(“test:=”+birthday+” “+birthday.substring(8, 10));
                    iyear = Integer.parseInt(birthday.substring(0, 4));
                    int month = Integer.parseInt(birthday.substring(5, 7)) – 1;
                    int day = Integer.parseInt(birthday.substring(8,10));
                    Calendar calendar = GregorianCalendar.getInstance();
                    // 当 Calendar 处于 non-lenient 模式时,如果其日历字段中存在任何不一致性,它都会抛出一个异常。
                    calendar.setLenient(false);
                    calendar.set(Calendar.YEAR, iyear);
                    calendar.set(Calendar.MONTH, month);
                    calendar.set(Calendar.DATE, day);
                   // 如果日期错误,执行该语句,必定抛出异常.
                    calendar.get(Calendar.YEAR);
                } catch (IllegalArgumentException e) {
                    errormessage=errormessage+”非法日期!”;
                }
               
                //验证码
                if (idno.length()==18){
       
                    String newidno=addr+bird+last ;               
                    int sum=
                     Integer.parseInt(newidno.substring(0,1))*7
                    +Integer.parseInt(newidno.substring(1,2))*9
                    +Integer.parseInt(newidno.substring(2,3))*10
                    +Integer.parseInt(newidno.substring(3,4))*5
                    +Integer.parseInt(newidno.substring(4,5))*8
                    +Integer.parseInt(newidno.substring(5,6))*4
                    +Integer.parseInt(newidno.substring(6,7))*2
                    +Integer.parseInt(newidno.substring(7,8))*1
                    +Integer.parseInt(newidno.substring(8,9))*6
                    +Integer.parseInt(newidno.substring(9,10))*3
                    +Integer.parseInt(newidno.substring(10,11))*7
                    +Integer.parseInt(newidno.substring(11,12))*9
                    +Integer.parseInt(newidno.substring(12,13))*10
                    +Integer.parseInt(newidno.substring(13,14))*5
                    +Integer.parseInt(newidno.substring(14,15))*8
                    +Integer.parseInt(newidno.substring(15,16))*4
                    +Integer.parseInt(newidno.substring(16,17))*2 ;
               
                    sum=sum % 11;
               
                    String r=””;
                    if  (sum==0)    r=”1″ ;
                    if  (sum==1)    r=”0″;
                    if  (sum==2)    r=”X”;
                    if  (sum==3)    r=”9″;
                    if  (sum==4)    r=”8″;
                    if  (sum==5)    r=”7″;
                    if  (sum==6)    r=”6″;
                    if  (sum==7)    r=”5″;
                    if  (sum==8)    r=”4″;
                    if  (sum==9)    r=”3″;
                    if  (sum==10)   r=”2″;
               
                    //r:=newidno+r ;
                    if (idno.substring(17,18).equals(r)){}else {errormessage=errormessage+”验证码错误!”;}
                           
                }
        }
}
if (errormessage.equals(“”)){errormessage=”ok”;} 
//out.write(errormessage);
//这个就是返回值
out.print(errormessage);

%>

运行 ajaxtest.jsp,点<检查是否正确>,系统就可以自动调用checkidno.jsp,然后返回判断结果。
可能有点BUG,因时间关系,还没有解决,欢迎大家批评指正。
注意,这里并没有刷新ajaxtest.jsp这个整个页面,这就是AJAX技术的价值所在。
AJAX就是JAVASCRIPT的升级版,所以很好学。

    原文作者:一把编程的菜刀
    原文地址: https://blog.csdn.net/chinmo/article/details/2145600
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞