Friday, May 30, 2014

How to read JSON data (json request) from http request in a servlet or controller


If you use a javascript AJAX request which sends the http request in JSON data format and not in normal key value pair, you might need this.
One such example is the ExtJS store's, api config. For all the write operations the proxy for create/update/delete (use AJAX) sends the records in json form, in case the reader is defined as json.

Code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException {
  
 PrintWriter out = response.getWriter();
 StringBuilder jsonString = new StringBuilder();
 
 String jsonToken = null;
 
 BufferedReader reader = request.getReader();
 while ((jsonToken = reader.readLine()) != null){
  jsonString.append(line);
 } 
 // converting the JSON String into a JSONObject, which almost work like a Map  
 try {
  JSONObject requestJsonObject = new JSONObject(jsonString); 
  // JSONObject from JSON Java API from json.org
  System.out.println(requestJsonObject.toString());
 }catch(JSONException jsone){
  jsone.printStackTrace();
 } 
  
 // further processing goes here....
} 

Interesting reads:

Tuesday, May 27, 2014

ExtJS: Panel Vs ViewPort

Working with ExtJS apps, we often get advice from the senior programmers that we should use ViewPort instead of Panel.
So, the question that we arise is when to use a ViewPort and when to use a Panel. Or, why to use ViewPort if Panel suffice the need.

Well, ViewPort is particularly useful in case you are looking to create a single web page application.
I think designers of ExtJS have put together all the essential components very nicely which allows you to develop Single Page WebApp quite elegantly.

Using viewport you can utilize availbale height and width of browser. You can use screen.availWidth and screen.availHeight to give you component proper dimension.

When I tried to do a little bit of search on google and forms, couple of interesting points came out which can help understanding answer when and why:
  1. There may be only one ViewPort for a page for obvious reason that it is specialized container which give you the entire view area of browser. But of course you can use a layout like border layout and add Panels as child component.So, in case you have a
  2. And, in cases where you do not have the liberty of using the entire view of the document/web page, use Panel instead. For example, in case your entire web page is using JQuery, but you want to keep a ExtJS grid component, you can do so using a grid panel.
     
The below forum links might useful to further reading:
The only tricky thing that I found with ViewPort is when you want to have the scrolling option, meaning the scrollbars should appear when you reduce the browser window size.
There is  autoScorll config, which is by default false. Although there implies that if you make it true it should show you the scrollbars. But, that does not happen often. The reason is something interesting to know.

If you take a look at the docs for ExtJS (4.x), it clearly states, "The Viewport does not provide scrolling, so child Panels within the Viewport should provide for scrolling if needed using the autoScroll config."


All this is little contradictory. But the problem gets solved once you provide the width/height or both for the child Panels. The scrollbar does appear after that.

So, in case you want to keep the scrolling, you need to take care of child Panels.
-----------------------------------------------------------------------------
  1. Please note the properties in EtxJS are case-sensitive, so "autscroll" won't work, it must be "autoScroll".
  2. screen.availWidth and screen.availHeight are provided by java script. You can alternatively use Ext.container.Viewport.getWidth() and Ext.container.Viewport.getHeight() as well.

Prototype

Prototype is another creation pattern. Intent:  - Intent is to create objects by cloning existing instance  - Specify the kinds of obj...