IT Certificaions

Google Cloud Platform

Friday, August 28, 2015

Set your application context path in Tomcat 7

Set Context path in Tomcat 7 or more using catalina.properties file,

Step 1)
Add following lines to catalina.properties file under <tomcat-install dir>/conf directory,

#myApp war file version
myApp .war=myAppLive -1.0.0.1.war
myApp .path=/myAppLive

#application deployment location
appBase=webapps

Step 2)
Now update context path in <Host> node in server.xml file in same location,
<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="false">
...
....  
 <Context path="${myApp .path}" docBase="${catalina.base}/${appBase}/${myApp .war}"/>
...
....

</Host>

Custom directive with dynamic attribute using angular JS

Custom directive with dynamic attribute using angular JS

Javascript:

var myApp=angular.module('myApp', []);
myApp.controller('myCtrl', ['$scope', function($scope) { 

  $scope.data= [
               { name: 'A', size: '12' },
               { name: 'C', size: '11' },
               { name: 'B', size: '10' }
              ];

}])
.directive('myOrderList', function(){ 


 function link(scope, element, attrs) {
       var myData=scope.data;    
       console.log(attrs);
   
       var ulElem=element.html('<ol>');
       for(var i=0;i<scope.data.length;i++){              
          ulElem.append('<li>'+scope.data[i][attrs.value]+'</li>');
        }    
   
  }

  return {
    link: link
  };

});

HTML CODE:
<div ng-app="myApp">


<div ng-controller="myCtrl">

 <div my-order-list value="size"></div>
 <div my-order-list value="name"></div>

</div>

</div>

Maven build is failing to include struts-config.xml or web.xml file to target../../WEB-INF directory

 Maven build is failing to include struts-config.xml or web.xml file to target../../WEB-INF directory due to ‘WebContent ‘ as maven needs ‘ webapps ‘

Add this plugin to pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<webResources>
<resource>
<directory>${basedir}/WebContent</directory>
</resource>
</webResources>
<warSourceDirectory>WebContent</warSourceDirectory>
<warSourceExcludes>WebContent/WEB-INF/lib/*.jar</warSourceExcludes>
<archiveClasses>false</archiveClasses>
</configuration>
</plugin>

Dynamic response by java and handle using ajax jquery for all content types

Dynamic response by java and handle using ajax jquery for all content types

Jquery ajax call
             var url="trackDynamicResponse.do";
              $.ajax({                           
                       url: url,
                       type: 'GET',
                       data :{
                             responseType : _responseType
                              
                       },
                       success: function(response, status, xhr){
                            
                               var ct = xhr.getResponseHeader("content-type") || "";
                               if (ct.indexOf('html') > -1) {
                                 console.log("html ...");                              
                               }
                               if (ct.indexOf('json') > -1) {
                                  console.log("json ...");                                 
                               }
                               if (ct.indexOf('xml') > -1) {
                                  console.log("xml ...");                               
                                  }
                               console.log(response);
                              $('#responseDiv').html("response :: "+response.toString());
                       },
              error :function( xhr, status, errorThrown ){                            
                alert(" An error occured: " + errorThrown + ", Status : " + status);           
                             console.log(errorThrown);
                             /* if(errorThrown.indexOf("XML")>-1){
                                    $('#responseDiv').html(errorThrown);
                             } */
              }
         });

---------------------------------------------------------------------------
Servlet :

  public String trackDynamicResponse() throws IOException{
 
    String responseType=request.getParameter("responseType");
   
    
    if(responseType !=null && responseType.equalsIgnoreCase("JSON")){
       
    JSONObject json      = new JSONObject();
    JSONArray  addresses = new JSONArray();
    JSONObject address;
   
      int count = 15;
  for (int i=0 ; i<count ; i++)
  {
      address = new JSONObject();
      address.put("CustomerName"     , "Decepticons" + i);
      address.put("AccountId"        , "1999" + i);
      address.put("SiteId"           , "1888" + i);
      address.put("Number"            , "7" + i);
      address.put("Building"          , "StarScream Skyscraper" + i);
      address.put("Street"            , "Devestator Avenue" + i);
      address.put("City"              , "Megatron City" + i);
      address.put("ZipCode"          , "ZZ00 XX1" + i);
      address.put("Country"           , "CyberTron" + i);
      addresses.add(address);
  }
  json.put("Addresses", addresses);
   
   
    response.setContentType("application/json");
    response.getWriter().write(json.toString());
   
    }else if(responseType !=null && responseType.equalsIgnoreCase("XML")){
    response.setContentType("application/xml");
    String xml = "";
             xml = xml + "<response>";
             xml = xml + " This is XML Response";
             xml = xml + "</response>";
    response.getWriter().write(xml);
    }else if(responseType !=null && responseType.equalsIgnoreCase("HTML")){
   
    String html = "<html><body>";
    html = html + "This is <b>HTML</b> Response";          
    html = html + "</response>";
             
    response.setContentType("text/html");
    response.getWriter().write(html);
    }else{
    response.getWriter().write("default text Response type");
    }
   
    return null;

}


Open popup window with post request data in Javascript

 Open popup window with post request data using javascript:
                              
  function openWindowWithPostRequest() {
           var winName='MyWindow';
           var winURL='search.action';
           var windowoption='resizable=yes,height=600,width=800,location=0,menubar=0,scrollbars=1';
           var params = { 'param1' : '1','param2' :'2'};           
           var form = document.createElement("form");
           form.setAttribute("method", "post");
           form.setAttribute("action", winURL);
           form.setAttribute("target",winName); 
           for (var i in params) {
                 if (params.hasOwnProperty(i)) {
                     var input = document.createElement('input');
                     input.type = 'hidden';
                     input.name = i;
                     input.value = params[i];
                     form.appendChild(input);
                 }
           }             
           document.body.appendChild(form);                                   
           window.open('', winName,windowoption);
           form.target = winName;
           form.submit();                  
           document.body.removeChild(form);          
   }