Quantcast
Viewing latest article 8
Browse Latest Browse All 9

Ajax – Getting value from Database and file .

This link shows how to get the values from a file (txt file)
====================================================================
Here’s something we have built it in our application .
1 . we have a text field in jsp page , onKeyUp we call a function .
<tr>
<td colspan=”1″>&nbsp;&nbsp;&nbsp;
<input name=”<%=CommonConstants.EMAIL_ID%>” size=”35″ type=”text”
onKeyUp=”getDynamicList(this);” value=”">
</td>
<td colspan=”1″ align=”right”><img src=”<%=WEB_STATIC_CONSTANT_PATH%>Web/images/AddUser.GIF” onClick=”gotoAddNewUser()” ></td>
</tr>
2 . Javascript function on the same page
function getDynamicList(inputObj)
{
var objVal = inputObj.value;
    if(event.keyCode == 13 || (event.keyCode == 8 && inputObj.value == ‘null’ || inputObj.value ==”) ) {
ajax_OptionHide();
// for all small and capital alphabets. // for numbers.
    } else if( !(event.keyCode >= 65 && event.keyCode <= 90) && !(event.keyCode >= 48 && event.keyCode <= 57) && event.keyCode != 8) {
return false;
    } else {
document.documentElement.onclick = autoHideList;
var allInputs = document.getElementsByTagName(‘INPUT’);
for(var no=0;no<allInputs.length;no++){
if(!allInputs[no].onkeyup)
allInputs[no].onfocus = ajax_OptionHide;
}
document.body.onkeydown = function(){
   ajax_option_keyNavigation();
}
//if(! ajax_OptionDiv)
 {
try {    // Firefox, Opera 8.0+, Safari.
   xmlHttpObject = new XMLHttpRequest();
} catch(e) {    // Internet Explorer
   try {
      xmlHttpObject = new ActiveXObject(“Msxml2.XMLHTTP”);      
   }catch (e){
try {
   xmlHttpObject = new ActiveXObject(“Microsoft.XMLHTTP”);        
}catch (e){
   alert(“Your browser does not support AJAX!”);
   return false;
}
   }
 }
xmlHttpObject.onreadystatechange=function() {
      if(xmlHttpObject.readyState == 4)
      {
   response =  xmlHttpObject.responseText;
   if( response != ‘null’ &&  (response != ‘SessionExpired’) )
parseXML(response, inputObj);
   else if ( response != ‘null’ && (response == ‘SessionExpired’) ) {
window.location.href = “Start?<%=CommonConstants.REQUEST_COMMAND%>=<%=CommonConstants.LOGOUT%>”;
window.location.reload(true);
   } else {
ajax_OptionHide();
return;
}
      }
}
if(inputObj.value.length > 2) {
   xmlHttpObject.open(“GET”,”Ajax?<%=CommonConstants.REQUEST_COMMAND%>=<%=CommonConstants.USER%>&<%=CommonConstants.REQUEST_SUB_COMMAND%>=<%=CommonConstants.SEARCH_USER_DATA%>&<%=CommonConstants.SEARCH_STRING%>=”+objVal);
   xmlHttpObject.send(null);
}
    }
 }
}
3. you can see in at the last we go to our request dispathcher which is ‘usersearch’ with the subcommand ‘SEARCH_USER_DATA’ ( look at the end of above func)
4 . in RDO (request dispathcer object )
  } else if (requestSubCommand.equals(CommonConstants.SEARCH_USER_DATA)){
return getSearchUserData(request, response, session);
 .
 .
 .
 .
 .
   and the function
   public String getSearchUserData(HttpServletRequest request,
       HttpServletResponse response,
       HttpSession session)throws DefaultException{
    try {
    UserCredentials_vo userData = (UserCredentials_vo)session.getAttribute(CommonConstants.SESSION_USER_CRED);
    String searchString = (String) request.getParameter(CommonConstants.SEARCH_STRING);
    searchString = searchString + “%”;
    User_bo userHandle = User_bo.getInstance();
    // you will get the data in next step .
    Vector searchUserData  = userHandle.getSearchUserData(userData, searchString);
    return formXML(searchUserData);
    } catch (Exception e){
    throw new DefaultException(e.getMessage());
    }
}
5 . You can see one funtion “formXML(searchUserData);”
  and that function goes like follows .
   public String formXML(Vector vData) {
    StringBuffer sb = new StringBuffer();
    if(vData != null && ! (vData.isEmpty()) ) {
    sb.append(“<Data>”);
    for(Enumeration enum1 = vData.elements();enum1.hasMoreElements();) {
    DBRecord dbRec = (DBRecord) enum1.nextElement();
    String status = dbRec.getFieldValueByName(CommonConstants.STATUS);
    status = status==null?”N”:status;
    String email = dbRec.getFieldValueByName(CommonConstants.EMAIL);
    email = email==null?”-”:email;
    String firstName = dbRec.getFieldValueByName(CommonConstants.FIRST_NAME);
    firstName = firstName==null?”-”:firstName;
    String lastName = dbRec.getFieldValueByName(CommonConstants.LAST_NAME);
    lastName = lastName==null?”-”:lastName;
    String userId = dbRec.getFieldValueByName(CommonConstants.USER_ID);
    userId = userId==null?”N”:userId;
    String daysAgo = dbRec.getFieldValueByName(CommonConstants.DAYS_AGO);
    daysAgo = daysAgo==null?”-”:daysAgo;
    if(daysAgo.equals(“0″)){
    daysAgo=”-”;
    } else {
    daysAgo+= ” days ago”;
    }
    String password = Utility.randomstring();
    String lob = dbRec.getFieldValueByName(“LOB”);
    lob = lob==null?”-”:lob;
    lob = Utility.replaceString(lob, “&”, “&amp;”);
    String componentName = dbRec.getFieldValueByName(CommonConstants.COMPONENT_NAME);
    componentName = componentName==null?”-”:componentName;
    componentName = Utility.replaceString(componentName, “&”, “&amp;”); 
    componentName = Utility.replaceString(componentName, “\”", “&quot;”); 
    componentName = Utility.replaceString(componentName, “‘”, “””); 
    componentName = Utility.replaceString(componentName, “>”, “&gt;”); 
    componentName = Utility.replaceString(componentName, “<”, “&lt;”);
    String jobCode = dbRec.getFieldValueByName(“JOB_CODE_DESCR”);
    jobCode = jobCode==null?”-”:jobCode;
    jobCode = Utility.replaceString(jobCode, “&”, “&amp;”);
    jobCode = Utility.replaceString(jobCode, “\”", “&quot;”);
    jobCode = Utility.replaceString(jobCode, “‘”, “””);
    jobCode = Utility.replaceString(jobCode, “>”, “&gt;”);
    jobCode = Utility.replaceString(jobCode, “<”, “&lt;”);
    sb.append(“<Record>”);
    sb.append(“<EMAIL>” + email + “</EMAIL>”);
    sb.append(“<FIRST_NAME>” + firstName + “</FIRST_NAME>”);
    sb.append(“<LAST_NAME>” + lastName + “</LAST_NAME>”);
    sb.append(“<DAYS_AGO>” + daysAgo + “</DAYS_AGO>”);
    sb.append(“<PASSWORD>” + password + “</PASSWORD>”);
    sb.append(“<LOB>” + lob + “</LOB>”);
    sb.append(“<STATUS>” + status + “</STATUS>”);
    sb.append(“<USER_ID>” + userId + “</USER_ID>”);
    sb.append(“<COMPONENT_NAME>” + componentName + “</COMPONENT_NAME>”);
    sb.append(“<JOB_CODE_DESCR>” + jobCode + “</JOB_CODE_DESCR>”);
    sb.append(“</Record>”);
    }
    sb.append(“</Data>”);
    } else {
    return null;
    }
    return sb.toString();
}
Thank you


// ]]>


// ]]>


Viewing latest article 8
Browse Latest Browse All 9

Trending Articles