书签 分享 收藏 举报 版权申诉 / 8

类型Ajax+jsp无刷新验证码实例.docx

  • 文档编号:10339629
  • 上传时间:2023-02-10
  • 格式:DOCX
  • 页数:8
  • 大小:18.13KB

2,num.jsp,反馈xmlhttp请求的页面

<%@pagecontentType="text/html;charset=GBK"language="java"import="java.sql.*"errorPage=""pageEncoding="GBK"%>

<%

//setChineseChar

//CodybyJarryLi@;

//homepage:

request.setCharacterEncoding("GBK");

response.setCharacterEncoding("GBK");

response.setContentType("text/html;charset=GBK");

%>

<%

Stringnum=request.getParameter("num");

Stringrandom=(String)session.getAttribute("random");

if(num!

=null&&random!

=null)

{

 if(!

num.equals(random))

 {

 /*

 out.println("");

 out.println("");

 //response.sendRedirect("img.jsp");

 */

 out.print("validate_failed:

"+random);

 }

 else

 {

  //out.println("

验证成功!

");

 out.print("validate_successful:

"+random);

 }

}

%>

3,random.jsp,生成验证码图片的页面

<%@pageautoFlush="false" import="java.util.*,java.awt.*,java.awt.image.*,com.sun.image.codec.jpeg.*,java.util.*"%>

<%

//setChineseChar

//CodybyJarryLi@;

//homepage:

request.setCharacterEncoding("GBK");

response.setCharacterEncoding("GBK");

response.setContentType("text/html;charset=GBK");

%>

<%

Stringchose="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

chardisplay[]={'0','','0','','0','','0'},ran[]={'0','0','0','0'},temp;

Randomrand=newRandom();

for(inti=0;i<4;i++)

{

 temp=chose.charAt(rand.nextInt(chose.length()));

 display[i*2]=temp;

 ran[i]=temp;

 }

 

Stringrandom=String.valueOf(display);

session.setAttribute("random",String.valueOf(ran));

 %>

<%

       out.clear();

       response.setContentType("image/jpeg");

       response.addHeader("pragma","NO-cache");

       response.addHeader("Cache-Control","no-cache");

       response.addDateHeader("Expries",0);

       intwidth=80,height=30;

       BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

       Graphicsg=image.getGraphics();

       //以下填充背景颜色

       g.setColor(Color.GREEN);

       g.fillRect(0,0,width,height);

      //设置字体颜色

       g.setColor(Color.RED);

       Fontfont=newFont("Arial",Font.PLAIN,20);

       g.setFont(font);

       //g.drawString(random,5,14);

       g.drawString(random,5,20);

       g.dispose();

       ServletOutputStreamoutStream=response.getOutputStream();

       JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(outStream);

       encoder.encode(image);

       outStream.close();

 response.flushBuffer();

 out.clear();

 out=pageContext.pushBody();

  %>

4,net.js,封装好的xmlhttp对象,可以很方便的调用

/*namespacingobject*/

varnet=newObject();

 

net.READY_STATE_UNINITIALIZED=0;

net.READY_STATE_LOADING=1;

net.READY_STATE_LOADED=2;

net.READY_STATE_INTERACTIVE=3;

net.READY_STATE_COMPLETE=4;

/*---contentloaderobjectforcross-browserrequests---*/

net.ContentLoader=function(url,on_load,on_error,method,params,contentType){

 this.req=null;

 this.on_load=on_load;

 this.on_error=(on_error)?

on_error:

this.defaultError;

 this.loadXMLDoc(url,method,params,contentType);

}

net.ContentLoader.prototype.loadXMLDoc=function(url,method,params,contentType){

 if(!

method)

 {

 method="GET";

 }

 if(!

contentType&&method=="POST")

 {

 contentType='application/x-www-form-urlencoded';

 }

 if(window.XMLHttpRequest)

 {

 this.req=newXMLHttpRequest();

 }

 elseif(window.ActiveXObject)

 {

  //addtrycatch;

  try{

     this.req=newActiveXObject("Msxml2.XMLHTTP");

  }catch(e1){

    try{

     this.req=newActiveXObject("Microsoft.XMLHTTP");    

     }catch(e2){

   }

  }

    //

 //this.req=newActiveXObject("Microsoft.XMLHTTP");

 }

 if(this.req)

 {

 try

 {

  varloader=this;

  this.req.onreadystatechange=function()

  {

 net.ContentLoader.onReadyState.call(loader);

  }

  this.req.open(method,url,true);

  if(contentType)

  {

   this.req.setRequestHeader('Content-Type',contentType);

  }

  this.req.send(params);

 }

 catch(err)

 {

  this.on_error.call(this);

 }

 }

}

net.ContentLoader.onReadyState=function(){

 varreq=this.req;

 varready=req.readyState;

 if(ready==net.READY_STATE_COMPLETE){

 varhttpStatus=req.status;

 if(httpStatus==200||httpStatus==0){

  this.on_load.call(this);

 }else{

  this.on_error.call(this);

 }

 }

}

net.ContentLoader.prototype.defaultError=function(){

 alert("errorfetchingdata!

"

 +"\n\nreadyState:

"+this.req.readyState

 +"\nstatus:

"+this.req.status

 +"\nheaders:

"+this.req.getAllResponseHeaders());

}

配套讲稿:

如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

特殊限制:

部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

关 键  词:
Ajax jsp 刷新 验证 实例
提示  冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:Ajax+jsp无刷新验证码实例.docx
链接地址:https://www.bdocx.com/doc/10339629.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2022 冰点文档网站版权所有

经营许可证编号:鄂ICP备2022015515号-1

收起
展开