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

搞了这么多年DELPHI,看着形势不搞JAVA就落伍了。最近公司搞的全是JAVA项目。
我就狼吞虎咽,一个月把STRUTS,SPRING,JSF,AJAX等东西给吃下了,有的现在还在打嗝呢。
好在现在发现AJAX是最好学的。
本例子参考了《征服AJAX WEB2.0开发技术详解》
这个例子用途:判断输入的身份证号码是否正确,这里能够检查出长度错误,日期错误,验证码错误。
先做一个普通网页 ajaxtest.jsp

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





无标题文档






 
   
   
 
身份证号码
       
   








然后是一个被调用的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的升级版,所以很好学。


你可能感兴趣的:(网站开发)