Method of Realizing src Update Request in img by Combining JSP with js

  • 2021-07-06 11:33:34
  • OfStack

This paper describes the method of src update request in img by combining JSP with js. Share it for your reference. The details are as follows:

1. javascript (updated function)


<script type="text/javascript"> 
function changeImage(){ 
  var img = document.getElementById("imgVcode");
  if(img.name == 1){ 
    img.name = 2; 
    img.src = "shop.do?method=registerImage2";
  }else if(img.name == 2){ 
    img.name = 1; 
    img.src = "shop.do?method=registerImage";
  } 
} 
</script>

2. Code for img and img changes in jsp


<td> 
  <img class="yzm_img" id='imgVcode' name="1" src="shop.do?method=registerImage" />
  <input name="code" type="text" id="txtVerifyCode" 
    class="yzm_input" onblur="testCode(this)"/>
  <div class="text_left t1">
    <p class="t1">
      <span id="vcodeValidMsg"> Please enter the 4 Three letters. </span> 
      <span id="codeInfo" style="color:red"></span> 
      <a href="#" id="imgchange" onclick="changeImage()"> Can't see clearly? Change the picture </a>
    </p> 
  </div> 
</td>

3. Background action code


public ActionForward registerImage(ActionMapping mapping, ActionForm form, 
   HttpServletRequest request, HttpServletResponse response)
    throws Exception { 
    // Will image Create , Return authentication code  
    response.setContentType("image/jpeg"); 
    OutputStream out = response.getOutputStream(); 
    String strEnsure = shopManager.createImageInfo(50,20,out);
    // Set to session Medium  
    request.getSession().setAttribute("strEnsure", strEnsure);
    return null; 
  } 
  public ActionForward registerImage2(ActionMapping mapping, ActionForm form, 
      HttpServletRequest request, HttpServletResponse response)
      throws Exception { 
    // Will image Create , Return authentication code  
    response.setContentType("image/jpeg"); 
    OutputStream out = response.getOutputStream(); 
    String strEnsure = shopManager.createImageInfo(50,20,out);
    // Set to session Medium  
    request.getSession().setAttribute("strEnsure", strEnsure);
    return null; 
} 

I hope this article is helpful to everyone's JavaScript programming.


Related articles: