JSP结合js实现img中src更新请求的方法
本文实例讲述了JSP结合js实现img中src更新请求的方法。分享给大家供大家参考。具体如下:
1.javascript(更新的函数)
<scripttype="text/javascript">
functionchangeImage(){
varimg=document.getElementById("imgVcode");
if(img.name==1){
img.name=2;
img.src="shop.do?method=registerImage2";
}elseif(img.name==2){
img.name=1;
img.src="shop.do?method=registerImage";
}
}
</script>
2.jsp中img和img改变的代码
<td> <imgclass="yzm_img"id='imgVcode'name="1"src="shop.do?method=registerImage"/> <inputname="code"type="text"id="txtVerifyCode" class="yzm_input"onblur="testCode(this)"/> <divclass="text_leftt1"> <pclass="t1"> <spanid="vcodeValidMsg">请输入图片中的四个字母。</span> <spanid="codeInfo"style="color:red"></span> <ahref="#"id="imgchange"onclick="changeImage()">看不清楚?换个图片</a> </p> </div> </td>
3.后台action代码
publicActionForwardregisterImage(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
//将image创建,返回认证码
response.setContentType("image/jpeg");
OutputStreamout=response.getOutputStream();
StringstrEnsure=shopManager.createImageInfo(50,20,out);
//设置到session中
request.getSession().setAttribute("strEnsure",strEnsure);
returnnull;
}
publicActionForwardregisterImage2(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse)
throwsException{
//将image创建,返回认证码
response.setContentType("image/jpeg");
OutputStreamout=response.getOutputStream();
StringstrEnsure=shopManager.createImageInfo(50,20,out);
//设置到session中
request.getSession().setAttribute("strEnsure",strEnsure);
returnnull;
}
希望本文所述对大家的JavaScript程序设计有所帮助。