Welcome to my blog, hope you enjoy reading
RSS

Tuesday, 18 December 2012

GWT Interview Questions


GWT Interview Questions

What is Google Web Toolkit?

Google Web Toolkit (GWT) is an open source Java development framework that lets you escape the matrix of 
technologies that make writing AJAXapplications so difficult and error prone. With GWT, you can develop and debug AJAX applications in the Java language using the Java development tools of your choice. When you deploy your application to production, the GWT compiler translates your Java application to browser-compliant JavaScript and HTML.
Here's the GWT development cycle:Use your favorite Java IDE to write and debug an application in the Java language, using as many (or as few) GWT libraries as you find useful.
Use GWT's Java-to-JavaScript compiler to distill your application into a set of JavaScript and HTML files that you can serve with any web server.
Confirm that your application works in each browser that you want to support, which usually takes no additional work.


Write a Sample GWT programm ?

public class Slicr implements EntryPoint {
public void onModuleLoad() {
final Button button = new Button("Click me");
final Label label = new Label();
button.addClickListener(new ClickListener() {
public void onClick(Widget sender) {
if (label.getText().equals(""))
label.setText("Hello World!");
else
label.setText("");
}
});
RootPanel.get("slot1").add(button);
RootPanel.get("slot2").add(label);
}
}


What are the different listeners in GWT ?

Here is the list of listeners HistoryListener,Changes to Browser History,WindowCloseListener,WindowResizeListener,ChangeListener,ClickListener,
FormHandler,FocusListener,KeyBoardListener,LoadListener,MouseListener,PopupListener,
ScrollListener,TableListener,TabListener,TreeListener.

GWT defines event listeners that you can attach to your widgets to monitor browser events. Event Listeners are just interfaces that you can implement in your widget. If you want to trap mouse click events, then your widget should implement the ClickListener interface and define theonClick(Widget sender) method. The code inside this method will be fired every time the userclicks on your widget.

You can create a new anonymous
ClickListener every time you want toassign it to a widget as given in the Hello gwt example.
button.addClickListener(new ClickListener() {
 public void onClick(Widget sender) {
  processData(sender.getText());
  reportData(sender.getText());
  logResults();
 }
});
But this results in localized code and lesser code reusability. Let us say we want to add another widget (a HTML link), which has to run the same code as in the onClick() method. We would be using the addClickListenermethod for this link and would be rewriting the same code again separately. And if we need to add another method in the onClick() method code between processData() and reportData() we will have to change each instance of onClick().

Alternatively, we can implement the listeners in the container widget to handle the events of contained widgets as shown below, making the code more reusable.
public class MainPanel extends Composite 
 implements ClickListener{
 private Button button=new Button("Button");
 public MainPanel(){
 
  //...
  button.addClickListener(this);
 }
 public void onClick(Widget sender){
  if(sender==button){
   processData(sender.getText());
   reportData(sender.getText());
   logResults();
  }
 }
}
With this approach, if we want to add a HTML widget link that implements the same function, we would just have to add the line
link.addClickListener(this);
in MainPanel() and modify one line in the onClick(Widget sender) method
if(sender==button||sender==link)

Given Below is a listing of GWT Event listeners for handling various events:
Listener
Event notifications
Methods Defined
HistoryListener
Changes to Browser History
onHistoryChanged(String historyToken)
WindowCloseListener
Window Closure Events
onWindowClosed()
onWindowClosing()
WindowResizeListener
Window Resizing Events
onWindowResized(int width, int height)
ChangeListener
fires when a widget changes
onChange(Widget sender)
ClickListener
fires when a widget receives a 'click'
onClick(Widget sender)
FormHandler
fires on receiving Form events from the FormPanel to which it is attached
onSubmit(FormSubmitEvent event)
onSubmitComplete( FormSubmitCompleteEvent event)
FocusListener
Focus Events
onFocus(Widget sender)
onLostFocus(Widget sender)
KeyBoardListener
Keyboard Events
onKeyDown(Widget sender, char keycode, int modifiers)
onKeyPress(Widget sender, char keycode, int modifiers)
onKeyUp(Widget sender, char keycode, int modifiers)
LoadListener
listens to 'load' event of a widget
onLoad(Widget Sender)
onError(Widget Sender)
MouseListener
listens to mouse events of a widget
onMouseEnter(Widget sender)
onMouseLeave(Widget sender)
onMouseDown(Widget sender, int x, int y)
onMouseUp(Widget sender, int x, int y)
onMouseMove(Widget sender, int x, int y)
PopupListener
listens to the events of a PopupPanel widget
onPopupClosed(PopupPanel sender, boolean autoClose)
ScrollListener
listens to Scroll events
onScroll(Widget widget, int scrollLeft, int scrollTop)
TableListener
listens to events pertaining to a Table widget
onCellClicked( SourcesTableEvents sources, int row, int cell)
TabListener
listens to the events of a Tab Widget
onBeforeTabSelected( SourcesTabEvents sender, int tabIndex)
onTabSelected( SourcesTabEvents sender, int tabIndex)
TreeListener
listens to the events of a Tree Widget
onTreeItemSelected( TreeItem item)
onTreeItemChanged( TreeItem item)

The
Button widget defines the addClickListener method which attaches aClickListener object to it. Similarly, the Tree widget defines theaddTreeListener method. Not all event listeners are available for all widgets - for example, the addMouseListener method is not defined for a DockPanelWidget. One way to add the MouseListener functionality to this DockPanel is to create a composite that includes a FocusPanel widget (which supportsaddMouseListener) and wrap the DockPanel widget within it.

Why doesn't GWT provide a synchronous server connection option?

GWT's network operations are all asynchronous, or non-blocking. That is, they return immediately as soon as called, and require the user to use a callback
method to handle the results when they are eventually returned from the server. Though in some cases asynchronous operators are less convenient to use than synchronous operators, GWT does not provide synchronous operators.

The reason is that most browsers' JavaScript
engines are single-threaded. As a result, blocking on a call to XMLHTTPRequest also blocks the UI thread, making the browser appear to freeze for the duration of the connection to the server. Some browsers provide a way around this, but there is no universal solution. GWT does not implement a synchronous network connection because to do so would be to introduce a feature that does not work on all browsers, violating GWT's commitment to no-compromise, cross-browser AJAX. It would also introduce complexity for developers, who would have to maintain two different versions of their communications code in order to handle all browsers.

What is AsyncCallback Interface?


public interface AsyncCallbackThe primary interface a caller must implement to receive a response from a remote procedure call.

If an RPC is successful, then onSuccess(Object) is called, otherwise onFailure(Throwable) is called.

Each callable asynchronous method corresponds to a method in the correlated service interface. The asynchronous method always takes an AsyncCallback as its last parameter, where T is the return type of the correlated synchronous method.


A call with a typical use of AsyncCallback might look like this:

service.getShapes(dbName, new AsyncCallback() {
public void onSuccess(Shape[] result) {
// It's always safe to downcast to the known return type.
controller.processShapes(result);
}

public void onFailure(Throwable caught) {
// Convenient way to find out which exception was thrown.
try {
throw caught;
} catch (IncompatibleRemoteServiceException e) {
// this client is not compatible with the server; cleanup and refresh the
// browser
} catch (InvocationException e) {
// the call didn't complete cleanly
} catch (ShapeException e) {
// one of the 'throws' from the original method
} catch (DbException e) {
// one of the 'throws' from the original method
} catch (Throwable e) {
// last resort -- a very unexpected exception
}
}
});


What are the language differences between web mode and hosted mode?

Typically, if your code runs as intended in hosted mode and compiles to JavaScript without error, web mode behavior will be equivalent. Occasional different problems can cause subtle bugs to appear in web mode that don't appear in hosted mode. Fortunately those cases are rare.


How do I make a call to the server if I am not using GWT RPC?

The heart of AJAX is making data read/write calls to a server from the JavaScript application running in the browser. GWT is "RPC agnostic" and has no particular requirements about what protocol is used to issue RPC requests, or even what language the server code is written in. Although GWT provides a library of classes that makes the RPC communications with a J2EE server extremely easy, you are not required to use them. Instead you can build custom HTTP requests to retrieve, for example, JSON or XML-formatted data.

To communicate with your server from the browser without using GWT RPC:

Create a connection to the server, using the browser's XMLHTTPRequest feature.
Construct your payload according to whatever protocol you wish to use, convert it to a string, and send it to the server over the connection.
Receive the server's response payload, and parse it according to the protocol.
You must use an asynchronous server connection.


Can I use GWT with my favorite server-side templating tool?

Yes, you are free to use GWT with any server-side templating tool.

With GWT development, your Java client code gets compiled into equivalent JavaScript that is loaded into your host pages. The generated product is totally independent from the server-side technology that you choose to use in your web application.

You can go ahead and use your favorite server-side templating tool and include your template directives into your host pages along with your GWT-generated JavaScript files. The server-side technology you're using to realize the templates is invisible to the browser and works just as it does without your GWT modules.


What is Gwt Sever Side RemoteServiceServlet?

public class RemoteServiceServlet extends javax.servlet.http.HttpServletimplements SerializationPolicyProvider
The servlet base class for your RPC service implementations that automatically deserializes incoming requests from the client and serializes outgoing responses for client/server RPCs.







1 comments: