GWT Interview Questions and Answers
Freshers / Beginner level questions & answers
Ques 1. What is Google Web Toolkit (GWT)?
The Google Web Toolkit (GWT) is a toolkit to develop Ajax web application with Java. The programmer writes Java code and this code is translated into HTML and Javascript via the GWT compiler.
The compiler creates browser specific HTML and JavaScript to support all the major browsers correctly. GWT supports a standard set of UI widgets, has build in support for the browser back button and a JUnit based test framework.
GWT provides two modes:
Development Mode: allows to debug the Java code of your 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.
Ques 2. How many modes are provided by GWT to execute application?
There are two modes: Development mode and Web mode.
Development Mode: allows to debug the Java code of your 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.
Ques 3. What are Modules, Entry Points and HTML Pages in GWT?
GWT applications are described as modules. A module "modulename" is described by a configuration file "modulename.gwt.xml". Each module can define one or more Entry point classes.
An entry point is the starting point for a GWT application, similar to the main method in a standard Java program. A Java class which is an entry point must implement the interface "com.google.gwt.core.client.EntryPoint" which defines the method onModuleLoad().
The module is connected to a HTML page, which is called "host page". The code for a GWT web application executes within this HTML document.
The HTML page can define "div" containers to which the GWT application can assign UI components or the GWT UI components are simply assigned to the body tag of the HTML page.
Ques 4. Can we set CSS style in GWT Java code?
The look and feel of a GWT application can be customized via CSS files. Each widget in GWT can be given a HTML "div" container and can therefore be individually styled by CSS. You use the Java method setStyle(String s) for this.
Ques 5. How can we run GWT application?
Two ways of running the application:
1) Create a war file and deploy in any app server or
2) Run ant hosted in the command prompt. And click on the ‘Launch Default Browser’.
Ques 6. 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.
Values: ie6,ie8,gecko1_8,safari,opera
module.gwt.xml:
<module..>
<inherits...>
<set property name="user.agent" value="ie8,opera">
</module..>
Ques 7. How to compile the GWT application?
Run ant build in the root folder of the application
•By default gwt compiler creates optimized JS files for some browsers.
–Disadvantage is it takes a lot of time to compile each change because of the permutations
Steps:
1) Open command prompt
2) Go to the application root folder
3) Run command as "ant build"
Then you can see compile logs are coming. If you see BUILD SUCCESSFUL it means no error in your application. If BUILD FAILED it means there is an error in your application.
Ques 8. How to create a GWT application?
•Create a folder for the application (e.g. withoutbook)
•Run webAppCreator in the newly created folder
–webAppCreator takes a module name (here 'withoutbook' is the module name with a package prefix)
Ques 9. What are the features in GWT?
To simply put ‘Write JavaScript using JAVA’
And in a little more detail:
Compiles JAVA code into JavaScript code
Takes care of cross browser compatibility by generating a browser specific code
Not client side alone. Has a deployable server side components (which is not compiled into JavaScript)
Client communicates to server via GWT-RPC call (if services are written using GWT),
Supports http calls using JSON and XML format
Supports AJAX calls
With JSNI (Javascript native interface) can mix javascript code with java code
java can call javascript method
javascript can call java method
Ques 10. Why to use GWT / what are the advantages to use GWT?
You're using Java - it means that your webdevs' proficiency in javascript won't come in handy as much (it will be helpful if you dabble in JSNI)
It's easy (especially for a beginner in GWT, especially when that person comes from a HTML/JS background - without too much object-oriented experience) to go all "wow, these 'object' things are so cool, let me make all my <div>s into separate objects, that will make the code all nice and neat". Of course, I'm over-exaggerating it, but you get the point - it's easy to imagine that an unexperienced programmer could put a full-blown Widget with lots of Handlers in every cell of a FlexTable... And then (s)he'll waste a lot of time wondering why the application feels sluggish ;) tl;dr: it's easy for beginners in GWT to make their applications "bloaty" by writing code in java.
The Compiler - now, most people talked with about GWT doesn't realize just how amazing this part of GWT is - for starters try this presentation from last year's Google IO. The compiler has the view of the whole application.
One nifty thing about GWT is that you get performance gains and new features for free with almost every new release of the framework. Since it's Java compiled to JavaScript.
Debugging - it is worth mentioning that you can (and should :)) debug your GWT apps just like any other Java application, using your IDE's debugger. And, in general, the Java debuggers I've seen are more advanced then their JavaScript counterparts.
UiBinder - while it's still not "perfect", UiBinder let's you design your Widgets in an easy and intuitive way using XML (as opposed to the pre-2.0 way that forced you to do this in Java). Mixing HTML and GWT's Widgets has never been so easy and fun.
Working with CSS - GWT has always, of course, embraced CSS, but with the introduction of GWT 2.0 (and UiBinder) they took it to another level. Let's look at a CSS file from a "normal" web application - hundreds, if not thousands of lines, hard to navigate, some styles are redundant but it's hard to notice that, some aren't used at all, add to this mix the need to please IE6/7 and you get yourself a nightmare. With GWT, you can instruct it to perform the similar tasks it did for the JS code for CSS - so it will prune all the unused CSS styles, merge where appropriate, minimize and obfuscate the class names, and many more (including conditionals, constants, etc in your CSS files). You are encouraged to keep your styles in their respective UiBinder's XML files - makes organizing and finding them so much easier. Last but not least, you get an error when you misspell a CSS style name - less hassle then trying to do the same via Firebug or a similar tool.
OOPHM - Out of Process Hosted Mode, with this, they fixed one of the biggest disadvantages of GWT - now, you get to use Hosted Mode in the browser of your choice (if that choice is Firefox, Safari, IE or Chrome, but at least you can use any version you want). The design of OOPHM also allows you to do cool stuff like run Windows in a VM, and connect from the IE there to the Hosted Mode running on the host OS (Linux/MacOS) - no need for hacks, copying files after every compile, etc.
The out-of-box collection of Widgets is kept small and simple. Now, if you're coming from some full-blown GUI framework (be it web or desktop), you might be surprised at how relatively small the number of Widgets GWT has. But according to the GWT's devs, it's kept like this on purpose - the basic Widgets are all the tools/"blocks" you need to build your own, customized to your needs Widgets.
Ques 11. What is GWT Compiler?
GWT Compiler used to convert the client side Java code to Javascript at the time of building the source files.
Intermediate / 1 to 5 years experienced level questions & answers
Ques 12. What are the disadvantages of GWT?
Problems with indexing by search engines - IMHO, this should be the biggest disadvantage of using GWT, or pure JS web applications in general. Since the content, layout, everything is created "on-the-fly" with JS, the search engine will only see a very short HTML page and that's that - you have to take care of this somehow yourself (for example, using cloaking). Google has finally started to work on a solution for this, however it doesn't seem to attractive to me.
Update: Google has finally addressed this problem. However, I'll leave this as a trade-off because making the application crawable still requires more effort than in other frameworks. At least now we have a "standard" to follow and don't have to use some dubious techniques (like cloaking).
Need lot of memory to run it in dev mode.
High compile time.
Every server call will be ajax.
You will loose control on your javascript.
Ques 13. Is GWT works in all the browsers? If yes why?
Yes GWT works in all the browsers because GWT compiler creates javascript code per browser and per localization.
I meant, if you configure module.xml with Firefox and IE then GWT compiler will create javascript code for both browsers seperately.
And when you load the application it first check from which browser you are opening the application and it will load the corresponding javascript.
It means GWT supports Cross browser functionality.
Ques 14. What is Bootstrap process in GWT or How an applications loads in GWT?
HTML
|
nocache.js ( Bootstrap javascript file )
|
|->( Loads the browser specific files)
|
module.xml ( *.gwt.xml )
|
|->( which loads the entry point class )
|
onModuleLoad() method
|
|
|
Loads the application
Ques 15. What are the GWT Panels?
RootPanel is the top level panel.
For layout the page, we could use following layout panels, finally we need to add all those layout panels in RootPanel.
DockLayoutPanel
SplitLayoutPanel
StackLayoutPanel
TabLayoutPanel
Use below panels inside the Layout panels for holding the widgets.
HorizontalPanel
VerticalPanel ( Prefer using FlowPanel )
Ques 16. How to work with Server side communication?
Using GWT RPC
To user GWT RPC we need to create couple of interfaces and one implementation class.
1. Synchronous interface
2. Asynchronous interface
3. Implementation class implements Synchronous interface.
Ques 17. How are we going to get the response from server side?
For this purpose only GWT has one Asynchronous interface which has one parameter called AsyncCallBack, using this callback object it will call the client side code once the server completed the execution.
So basically all the calls to server side method will have two call back methods
onSuccess()
onFailure()
If the server side method executed properly then onSuccess() method will be called otherwise onFailure() method will be called, so based on this we can proceed with our functionality.
Ques 18. How GWT Navigation works?
In GWT we can handle page navigation using couple of ways.
1. Using History tokens
2. Clearing the content panel and load the new page in the content panel.
Experienced / Expert level questions & answers
Ques 19. What is GWT - Java Emulation library?
Google Web Toolkit includes a library that emulates a subset of the Java run-time library. The list below shows the set of JRE packages, types and methods that GWT can translate automatically. Note that in some cases, only a subset of methods is supported for a given type.
java.lang
java.lang.annotation
java.math
java.io
java.sql
java.util
java.util.logging
Ques 20. How to create custom widgets in GWT?
Create a class that should extends Composite class of GWT.
Inside the constructor you can write you logic to create a widget and call the initWidget method().
Then you can use this class in anywhere in the application and add this widget in any panels.
private static class OptionalTextBox extends Composite implements
ClickHandler {
private TextBox textBox = new TextBox();
private CheckBox checkBox = new CheckBox();
--------
initWidget(panel);
-------
}
Ques 21. Code of entry point with onModuleLoad where split out is working.
To split your code, simply insert calls to the method GWT.runAsync at the places where you want the program to be able to pause for downloading more code. These locations are called split points. For this you should use runAsync.
public class Hello implements EntryPoint {
public void onModuleLoad() {
Button b = new Button("Click me", new ClickHandler() {
public void onClick(ClickEvent event) {
GWT.runAsync(new RunAsyncCallback() {
public void onFailure(Throwable caught) {
Window.alert("Code download failed");
}
public void onSuccess() {
Window.alert("Hello, AJAX");
}
});
}
});
RootPanel.get().add(b);
}
}
Ques 22. What is AsyncDataProvider in GWT cell widgets?
When you want to display dynamic data or a range of data, not just a static list, then we should use AsyncDataProvider.
Ques 23. What is GWT ClientBundle?
In GWT we can use ClientBundle to load all the images and CSS files in one single file and use it anywhere in the application. It provides flexible way to get the resources.
1. It creates a single image from the collection of images at compile time to reduce the image size.
2. Enable more aggressive caching headers for program resources. It will go to server for every-time to get the images.
3.Eliminate mismatches between physical file names and constants in Java code by performing consistency checks during the compile.
Ques 24. How to use RequestBuilder?
To use RequestBuilder we need to inherit HTTP Module in module.xml file
<inherits name="com.google.gwt.http.HTTP"/>
Ques 25. What is the purpose of the IsSerializable interface in GWT?
A user-defined class is serializable if all of the following apply:
It is assignable to IsSerializable or Serializable, either because it directly implements one of these interfaces or because it derives from a superclass that does
All non-final, non-transient instance fields are themselves serializable, and
As of GWT 1.5, it must have a default (zero argument) constructor (with any access modifier) or no constructor at all.
One key difference though is that , for security reasons, all Serializable classes must be included in a serialization policy, which is generated at compile time, while IsSerializable classes do not have that requirement.
If your interest is purely in GWT, and you don't e.g. share your model classes between the web application and another application, I suggest you have your model classes/DTOs implement IsSerializable.
Ques 26. What is the use GWT generator?
Generators allow the GWT coder to generate Java code at compile time and have it then be compiled along with the rest of the project into JavaScript.
Ques 27. How to make GWT Custom Event Handler?
This is an event that is triggered when the user becomes happy.
Define a new event class. You can add arbitrary metadata in the event class. For simplicity, we will not include any here though.
public class HappyEvent extends GwtEvent {
...
}
Define a new handler and marker interface for the event class.
interface HappyHandler extends EventHandler {
public void onHappiness(HappyEvent event);
}
interface HasHappyEvents {
public HandlerRegistration addHappyHandler(HappyHandler handler);
}
Add a unique event type
class HappyEvent extends AbstractEvent{
public static AbstractEvent.Key KEY = new AbstractEvent.Key(){...}
public GwtEvent.Key getKey(){
return KEY;
}
...
}
Wire up the handler's fire method
class HappyEvent extends GwtEvent {
static Key
protected void fire(HappyHandler handler, HappyEvent event) {
handler.onHappiness(event);
};
...
}
Most helpful rated by users:
- What is Google Web Toolkit (GWT)?
- How many modes are provided by GWT to execute application?
- What are Modules, Entry Points and HTML Pages in GWT?
- How can you set Browser targeted Compilation in GWT?
- How can we run GWT application?