Friday 31 October 2014

plain html file which parses request URL params and use them in post form

Right before Melbourne Cup 2014, there is a requirement to redirect end users of our customers to external URL.

The end user would see 'intro.html'. Within this page, the user can click on a button in a form. Clicking this button would redirect the end user to an external URL.

The external URL would be passed in URL request, something like
intro.html?userId=123456&cancelUrl=http://www.google.com

The form needs to extract 'userId' and 'cancelUrl' from the URL.





As for the front end html file, the only tricky bit is to use javascript to extract some parameters from URL


 <form id="cancelurl-form" name="retry-form" method="get" onsubmit="get_action(this);">  
   <input id="userid-input" type="hidden" name="userId" />  
   <input class="submit" value="Skip identification" type="submit" />  
 </form>  
 <script type="text/javascript">  
 function getUrlParameter(sParam)  
 {  
   var sPageURL = window.location.search.substring(1);  
   var sURLVariables = sPageURL.split('&');  
   for (var i = 0; i < sURLVariables.length; i++)  
   {  
     var sParameterName = sURLVariables[i].split('=');  
     if (sParameterName[0] == sParam)  
     {  
       return sParameterName[1];  
     }  
   }  
 }  
 function get_action(form) {  
  if (!getUrlParameter('cancelUrl'))  
  {  
  form.action = '/verification/prefuse/intro.html';   
  }  
  else  
  {  
  form.action = getUrlParameter('cancelUrl');  
  }  
 }  
 </script>  
 <script type="text/javascript">  
  var elem = document.getElementById("userid-input");  
  elem.value = getUrlParameter('userId');  
 </script>  



We normally use org.jboss.seam.faces.Redirect object to redirect to our JSF xhtml page.
Since the destination URL is a pure html, the back end JAVA code need to be like below.



 FacesContext context = FacesContext.getCurrentInstance();  
 ExternalContext externalContext = context.getExternalContext();  
 HttpServletResponse response = (HttpServletResponse)externalContext.getResponse();  
 response.sendRedirect("/verification/prefuse/intro.html");  
 context.responseComplete();