Monday, January 27, 2014

RESTful Web Services With JSON response

With Jersey + Jackson + Jersey Java Client


Jersey uses Jackson to convert object to / form JSON. In this tutorial, we show you how to convert a “TTInformation” object into JSON format, and return it back to user.

1) Dependencies

To make Jersey support JSON mapping, use “jersey-json_version.jar” 
To use Jersey client APIs, declares “jersey-client_version.jar

Please look at selected ones.


2) Integrate JSON with Jersey

In web.xml, declares “com.sun.jersey.api.json.POJOMappingFeature” as “init-param” in Jersey mapped servlet. It will make Jersey support JSON/object mapping.

<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature
<param-value>true
</init-param>

File : web.xml - full example.



3) POJO

A simple “TTInformation” object, later Jersey will convert it into JSON format.




4) JAX-RS with Jersey - GET and POST service

Annotate the method with @Produces(MediaType.APPLICATION_JSON). Jersey will use Jackson to handle the JSON conversion automatically.


5) Want to see OutPut     .... :)

6) Standalone Jersey GET Client
Jersey client to send a “GET” request and print out the returned json data.

Output

7) Standalone Jersey POST Client
Jersey client to send a “POST” request, with json data and print out the returned output.

Output

For source code -> sac10nikam@gmail.com

Tuesday, January 21, 2014

jQuery – Object Oriented Programming

Let us create a simple class.
var simpleClass = { };
Thats it?. Attributes? Methods ?. Where are they?. Lets see how can we fit those in inside this empty class.

Attributes and Behaviours

var simpleClass = {

 testAttribute : 'test', // atttribute

 testMethod : function() //method
 { return testAttribute; } 

 };
Now we instantiate this Object.
var simpleClassObj = new simpleClass();
Perfect!!!!.

Conditions Apply

Private Variable

There aint any private variables here. To access the testAttribute, just use
simpleClassObj.testAttriute;
Enough!!

Dynamic Attribute Insertion

Suppose, i use the statement
simpleClassObj.testAttribute2 = 'test2';
Do you think, it will work. compilation error??. Duh!. It will still work. Our exceptional system will modify the source class for us ( Amazing huh!!).
You can clearly see, jQuery has sold the encapsulation feature for duck typing. Only god knows, the actual state of an object here.

Why OOPS in jQuery

Now, the question. If not for encapsulation, why do we need to go for OOPS approach in jQuery. The answer is Organization.
Consider a simple example HTML;
<input type="text" name="test-name" id="test-id" class="test-class" style="test-style"/>
If you want to manipulate this dom object in jQuery , you go to do this
$('#test-id').val()        // get value
$('#test-id').attr('name') // get name
$('test-id').html()        //get inner HTML
Can you see, these three simple statements have absolutely no similarity (except the object part). Imagine a similar style of code , in a page which has plenty of dom objects and widgets. Do you think its easy to understand??.
Now lets convert this to a simple object
Lets create a simple abstract class ( Dont search for the abstract keyword. We dont have one. if you want an abstract class, better YOU dont instantiate it)
var syndrome = {

         doHide : function(){
                      this.hide('slow');
                  },

         doShow : function(){
                      this.show( 'slow' );
                  },

         doDisable : function(){
                       this.attr('disabled', 'disabled');
                      },

            toggle : function(){
                         this.toggle();
                      },

           getStyle : function(){
                         return this.attr('style');
                      },

      setForeground : function(color){
                          var style = 'color:'+color+';';
                          this.attr('style', style+this.getStyle());
                       },

       setBackground : function(color){
                          var style = 'background-color:'+color+';';
                          this.attr('style', style+this.getStyle());
                        },

              getName : function(){
                           return this.attr('name');
                         },

              getValue : function(){
                            return this.attr('value');
                          },

              getClass : function() {
                             return this.attr('class');
                          },

              getContent : function(){
                              return this.html();
                           }
       };
Its easily readable, you can see what am doing. Just put some common methods. Now , lets see how can we use this in our above example.
var testObj = $('#test-id'); //get the dom object

$.Extend( testObj, syndrome ); //extending the abstract class. So , the  method should be available here.
testObjtestObj.getValue();   // Get value
testObj.getName();    // Get Name
testObj.getContent(); // Get content
Now this looks neat and clean. Now Imagine the same scenario, a lot of components and widgets. What will you have . Just a list of $.Extend statements with pure object interaction.

Wednesday, January 1, 2014

Wells Fargo Java, J2ee Interview Questions

Interview Questions asked at Wells Fargo for Java / J2EE Position

Generally at Wells Fargo there will be multiple people interviewing you at the same moment.
Most of the times we have seen 5 to 6 people (includes manager, multiple technical guys).
And they asked questions from almost every area in J2EE.
  • Firstly, they asked me to write a SQL query on board
  • Then they asked me some design questions(design patterns)
  • Also asked couple of questions in struts and Servlets.
Most of the time it was a discussion so can't remember few of the questions.
Anywz its very good and tough interview so prepare well technically and be kind, confident and never get into the pressure, because seeing 6 people in a room with a board is enough to make anybody nervous.
I would say that they generally look more into the right attitude rather than the right answers.

Below are couple of technical questions that they asked:
  1. Drawbacks of struts framework
  2. Spring versus Factory pattern
  3. What is JDBC and explain JDBC architecture
  4. Was asked to write a query which involved a join, group by clause, order by stuff
  5. What is outer join
  6. explain life cycle of Servlet
  7. explain life cycle of Action servlet
  8. What j2ee design patterns in used in a servlet?
  9. some questions related to jdbc problems encountered in a j2ee application and a stand alone java swing application ? Not Sure what that means (not sure if I have framed the question correctly ) but it was some what like that...
  10. What is a prototype in uml?
  11. What happens when a request comes in how does the container handle it?
  12. Version control tool CVS, basic commands
  13. question related to exceptions in Java (do not recall entirely)
  14. Difference between stateless & statefull session bean in ejb
  15. again something on connection pooling do not remember exactly.
  16. question related to hibernate configuration files
  17. Difference between aggregation and composition
  18. what are some of the exceptions related to string...
  19. questions like... your skill sets, why should we hire you, refactoring approach, would you like to work old/new development... general questions, etc
  20. What if you are added on a support project and consider if everyone goes on leave and clients reports a issue, how will you handle that?
  1. Web Development JavaScript questions:
    - Difference between HTML vs XTHML
    - What does the "position" attribute do in CSS
    - Explain "closures" in javascript
    - Explain inheritence in javascript