gwt

GWT Interview Questions and Answers

In this article we will learn about the most common interview questions asked in GWT domain. We will start with the basic ones then move on to more tricky ones.

1. What is GWT?

Google Web Toolkit (GWT) is a development toolkit for building ajax applications using Java. The programmer writes code in Java then GWT compiler converts this code to JavaScript. With GWT, we can develop and debug AJAX applications in the Java language using the Java development tools of our choice

GWT provides two modes:

  • Development Mode: allows to debug the Java code of the application directly via the standard Java debugger.
  • Web mode: the application is translated into HTML and Javascript code and can be deployed to a webserver.

2. What is a module descriptor in GWT application?

A module descriptor is a configuration file used to set-up a GWT application.

3. What is a GWT Module?

A GWT module is simply an encapsulation of functionality. It shares some similarities with a Java package but is not the same thing. A GWT module is named similarly to a Java package in that it follows the usual dotted-path naming convention. For example, most of the standard GWT modules are located underneath “com.google.gwt” However, the similarity between GWT modules and Java packages ends with this naming convention.

A module is defined by an XML descriptor file ending with the extension “.gwt.xml”, and the name of that file determines the name of the module. For example, if we have a file named src/com/mycompany/apps/MyApplication.gwt.xml, then that will create a GWT module named com.mycompany.apps.MyApplication. The contents of the .gwt.xml file specify the precise list of Java classes and other resources that are included in the GWT module.

4. What is an entry point class?

A module entry-point is any class that is assignable to EntryPoint and that can be constructed without parameters. When a module is loaded, every entry point class is instantiated and its EntryPoint.onModuleLoad() method gets called.

5. Which method of the Entry point class is called when the GWT application is loaded? What happens if there are multiple Entry point classes?

onModuleLoad(). If there are more than one Entry point classes then each of then gets called in the sequence in which they are defined in the configuration file.

6. How do I enable assertions?

The GWT compiler recognizes the -ea flag to generate code for assertions in the compiled JavaScript. Only use assertions for debugging purposes, not production logic because assertions will only work under GWT’s development mode. By default, they are compiled away by the GWT compiler so do not have any effect in production mode unless we explicitly enable them.

7. What is the default style name of any GWT widget?

By default, the class name for each component is gwt-<classname>. For example, the Button widget has a default style of gwt-Button and similar way TextBox widgest has a default style of gwt-TextBox.

8. What is internationalization?

Internationalization is changing the language of the text based on the locale. For example the browser should display the website content in Hindi for a user sitting in India and in French for the user accessing the website from France.

9. What is the difference between TextResource and ExternalTextResource

The related resource types TextResource and ExternalTextResource provide access to static text content. The main difference between these two types is that the former interns the text into the compiled JavaScript, while the latter bundles related text resources into a single file, which is accessed asynchronously.

10. How can you set Browser targeted Compilation in GWT?

To reduce the compilation time, choose favorite browser and add the user.agent property in the module configuration file.

11. 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 operations are less convenient to use than synchronous operations, GWT does not provide synchronous operations.

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.

12. What is GWT ClientBundle?

The resources in a deployed GWT application can be roughly categorized into resources to never cache (.nocache.js), to cache forever (.cache.html), and everything else (myapp.css). The ClientBundle interface moves entries from the everything-else category into the cache-forever category.

13. What is DataResource in GWT?

A DataResource is the simplest of the resource types, offering a URL by which the contents of a file can be retrieved at runtime. The main optimization offered is to automatically rename files based on their contents in order to make the resulting URL strongly-cacheable by the browser. Very small files may be converted into data: URLs on those browsers that support them.

14. How to create custom widgets in GWT?

There are three general strategies to follow:

Create a widget that is a composite of existing widgets. The most effective way to create new widgets is to extend the Composite class. A composite is a specialized widget that can contain another component (typically, a Panel) but behaves as if it were its contained widget. We can easily combine groups of existing widgets into a composite that is itself a reusable widget. Some of the UI components provided in GWT are composites: for example, the TabPanel (a composite of a TabBar and a DeckPanel) and the SuggestBox. Rather than create complex widgets by subclassing Panel or another Widget type, it’s better to create a composite because a composite usually wants to control which methods are publicly accessible without exposing those methods that it would inherit from its Panel superclass.

Create an entirely new widget written in the Java language. It is also possible to create a widget from scratch, although it is trickier since we have to write code at a lower level. Many of the basic widgets are written this way, such as Button and TextBox.

Create a widget that wraps JavaScript using JSNI methods. When implementing a custom widget that derives directly from the Widget base class, we may also write some of the widget’s methods using JavaScript. This should generally only be done as a last resort, as it becomes necessary to consider the cross-browser implications of the native methods that we write, and also becomes more difficult to debug.

15. What is a UiBinder?

UiBinder provides a declarative way of defining User Interface. It helps to separate the programming logic from the UI.

16. What is the Same Origin Policy, and how does it affect GWT?

Modern browsers implement a security model known as the Same Origin Policy (SOP). Conceptually, it is very simple, but the limitations it applies to JavaScript applications can be quite subtle. Simply stated, the SOP states that JavaScript code running on a web page may not interact with any resource not originating from the same web site. The reason this security policy exists is to prevent malicious web coders from creating pages that steal web users’ information or compromise their privacy. While very necessary, this policy also has the side effect of making web developers’ lives difficult.

It’s important to note that the SOP issues are not specific to GWT; they are true of any AJAX application or framework.

17. Which class is the superclass of all UI widgets?

com.google.gwt.user.client.ui.UIObject

18. What is GWT RPC

The GWT RPC framework makes it easy for the client and server components of web application to exchange Java objects over HTTP. The server-side code that gets invoked from the client is often referred to as a service. The implementation of the GWT RPC service is based on a Servlet architecture. Within a client code, we will use a automatically generated proxy class to make calls to the service. GWT will handle serialization of the Java objects. GWT RPC service is different from SOAP and REST.

19. What are Layout Panels?

Layout Panels can contain other widgets. These panels controls the way widget is displayed on User Interface. Every Panel widget inherits properties from Panel class which in turn inherits properties from Widget class and which in turn inherits properties from UIObject class.

20. How is GWT different from other frameworks?

GWT provides a set of ready-to-use user interface widgets that we can immediately utilize to create new applications. It also provides a simple way to create innovative widgets by combining the existing ones. We can use IDE to create, debug, and unit-test our AJAX applications. We can build RPC services to provide certain functionalities that can be accessed asynchronously from the web applications easily using the GWT RPC framework.

GWT enables us to integrate easily with servers written in other languages, so we can quickly enhance our applications to provide a much better user experience by utilizing the AJAX framework. GWT has the Java-to-JavaScript compiler to distill our application into a set of JavaScript and HTML files that we can serve with any web server. This gives us a great feature browser compatibility.

21. What are the features of GWT

Google Web Toolkit (GWT) is a development toolkit to create RICH Internet Application. GWT provides developers option to write client side application in Java. Application written in GWT is cross-browser compliant. GWT automatically generates javascript code suitable for each browser

22. What can I do to make images and borders appear to load more quickly the first time they are used?

Use Image.prefetch()

23. What is Deferred Binding?

Deferred Binding is GWT’s answer to Java reflection. Every web browser has its own idiosyncrasies, usually lots of them. The standard Java way of dealing with idiosyncrasies would be to encapsulate the custom code into subclasses, with one subclass for each supported browser. At runtime, the application would use reflection and dynamic classloading to select the appropriate subclass for the current environment, load the class, create an instance, and then use that instance as the service provider for the duration of the program.

This is indeed what GWT does. However, the JavaScript environment in which GWT applications ultimately run simply does not support dynamic classloading (also known as dynamic binding.) Because dynamic binding is unavailable as a technique to GWT, GWT instead uses deferred binding. One way to think of this is as “dynamic class-loading that occurs at compile time instead of execution time.” When the GWT Compiler compiles the Java application, it determines all the different “idiosyncrasies” that it must support, and generates a separate, tightly streamlined version of the application for that specific configuration. For example, it generates a different version of the application file for Firefox than it does for Opera.

The GWT Compiler uses Deferred Binding to generate a completely separate version of the application for each language.

24. How do I create an app that fills the page vertically when the browser window resizes?

As of GWT 2.0, creating an application that fills the browser is easy using Layout Panels. LayoutPanels such as DockLayoutPanel and SplitLayoutPanel automatically resize to the size of the window when the browser resizes.

25. How do you make a call to the server if you are not using GWT RPC?

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

  • Create a connection to the server, using the browser’s XMLHTTPRequest feature.
  • Construct a payload, 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.

26. How can I dynamically fetch JSON feeds from other web domains?

Like all AJAX tools, GWT’s HTTP client and RPC libraries are restricted to only accessing data from the same site where the application was loaded, due to the browser Same Origin Policy. If the application is using JSON, there is a  work around to this limitation using a <script> tag (aka JSON-P).

First, we need an external JSON service which can invoke user defined callback functions with the JSON data as argument. An example of such a service is GData’s “alt=json-in-script& callback=myCallback” support. Then, we can use JsonpRequestBuilder to make our call, in a way similar to a RequestBuilder when we’re not making a cross-site request.

Conclusion

In this article we saw some of the GWT related questions which are quite popular in Interviews. GWT is open source, completely free, and used by thousands of enthusiastic developers around the world. Its goal is to enable productive development of high-performance web applications without the developer having to be an expert in browser quirks, XMLHttpRequest, and JavaScript. The GWT SDK provides a set of core Java APIs and Widgets. These allow you to write AJAX applications in Java and then compile the source to highly optimized JavaScript that runs across all browsers, including mobile browsers for Android and the iPhone. Most of the time the interviewer is more interested to know whether the person understands the concept of GWT, very rarely they are interested to know about the APIs.

Mohammad Meraj Zia

Senior Java Developer
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button