Stop browser scripts caching in GWT App - gwt

I have a GWT app deployed onto our client's machines. As an ongoing
development alongside, we have to release new improved versions of the
application fron time to time. Everytime we release a new version we
often run into the problem where the client's browser has cached the
old scripts scriptsand for a while it behaves strangly as the data it
is trying to work with is not quite compatible with it. What is the
best way to overcome this problem. Currently I have to tell the users
to clear their browser's cache for a new release but it would be nice
they don't have to do this.

Possible solution depends on the way you are hosting your application. If you are hosting directly from servlet container, then you can use servlet filter like the one described here:
http://seewah.blogspot.com/2009/02/gwt-tips-2-nocachejs-getting-cached-in.html
Here are appropriate filters from tadedon library:
http://code.google.com/p/tadedon/source/browse/tadedon-servlet/src/main/java/com/xemantic/tadedon/servlet/CacheDisablingFilter.java
http://code.google.com/p/tadedon/source/browse/tadedon-servlet/src/main/java/com/xemantic/tadedon/servlet/CacheForcingFilter.java
And here is guice ServletModule which enables them for the whole guice web application:
http://code.google.com/p/tadedon/source/browse/tadedon-gwt/src/main/java/com/xemantic/tadedon/gwt/http/GwtHttpCachingModule.java
If you are using some reverse proxy in front of tomcat it would be even simpler. In case of apache (e.g. mod_proxy, mod_jk), and assuming that all the application resources (html, graphics, java scripts, css, etc.) are put on apache, just set these options in apache configuration:
<Files *.nocache.*>
ExpiresDefault "access"
</Files>
<Files *.cache.*>
ExpiresDefault "now plus 1 year"
</Files>
It is described here:
http://code.google.com/webtoolkit/doc/latest/DevGuideCompilingAndDebugging.html
in "Perfect Caching" section. Such deployment scenario assumes that only rpc requests should go through reverse proxy to tomcat. If for some reasons all the application context is proxied to tomcat you can still use apache's LocationMatch directive instead of Files directive.

By default, the bulk of your app should be cached by the browser until a new version of it is generated by your build process.
It might help to understand the GWT bootstrapping model to understand how this works.
The first script your client requests, your-app-name.nocache.js, is not cached, and it does nothing except check the browser's user agent and capabilities, and make a second request for the relevant app JS.
At this point, the script it requests should be cached by the browser if it's been requested before. This is a {indistinguisable-numbers-and-letters}.cache.html file.
When you redeploy your app, the nocache.js file will be executed and ask for a different cache.html file from the server, which will not already be present in the cache, but which will get cached by the browser once it is downloaded.
Are you doing anything unusual with deferred binding, or with caching headers on your server? This might potentially be causing your nocache.js file to get cached after all, which would make it request old cache.htmls from the browser cache.

Related

Deploying a GWT web application on Github

Can a GWT web application be deployed on Github?
For example, a GWT web application is created, and it works on a server intalled with Tomcat. It's known that a web page can be created on Github, like http://help.github.com/articles/creating-pages-with-the-automatic-generator/ Can a GWT web application also be deployed on Github? If it's possible, how to deploy it?
On Github Pages you can only use/host client-side technology like JavaScript, CSS and HTML. So your app would not have an back-end which can handle your RPCs. But it's possible on GitHub Pages to make Ajax calls (http://blog.teamtreehouse.com/code-a-simple-github-api-webapp-using-jquery-ajax ) , which are also the base for GWT-RPCs.
When the fron-end is running, you need a server for your backend. Afaik there are libraries to use php as an back-end (I guess most are not maintained anymore), or you could use the JsonpRequestBuilder to make HTTP-calls to a server of your choice. JSONP would be necessary to overcome the cross-domain restrictions imposed by browsers same-origin policy, because your backend would be on a different server.
So all in all this is not the way to go. As I mentioned in the comments you can try the GAE (Google App Engine) to host your application without recreating your back-end, because the other solution would require to rewrite your back-end (eg. PHP) and to host it somewhere
One last tip: Before you move definitely to GAE, check that you have all necessary libraries for you backed.
If you are using servlets and stuff : certainly no
If you are only using client stuff, my guess is also no. I don't think github even allows javascript, or even html ?

Java Servlets + JDBC + Postgres: How does it all interact?

I'm having trouble wrapping my head around how to use servlets properly
I've set up a postgres database, and downloaded a JDBC driver for it.
What I want to have is my webpages post to the servlet, and the servlet get info from the database. I understand how to code everything (eg add library for driver, open connections, execute queries), but I think I'm lacking knowledge in how to set up the file structure.
I have the postgresql database running on pgAdmin. Do I also need to have a server running to make the servlets work as well? Can't I just make a web.xml file that maps to the servlets, and open the webpages to use the website? If I run the project through an IDE with a server running (glassfish) everything works. If I close the IDE and go to open the webpages on my browser again, I get 404's whenever I submit to a servlet.
Can someone give me a bit of guidance on the big picture of how everything is supposed to interact (with details on servers please). I've been searching the web and I havent found anything that explains the big picture very well.
Thanks
A Java web application is a set of files obeying a well-defined structure, and which can be packaged in a war file.
This web application is deployed into a server (also called container), which understands the file structure, listens to HTTP requests, and calls the appropriate servlet of the appropriate deployed web application when it receives one.
And of course, if you shut down the server, nothing listens to the HTTP requests anymore, so you won't get any response.
You could read the Java EE tutorial for more explanations.

run GWT on non java server

I think answer on my question is NO. But still, I'm wondering is it possible to run gwt applicaton as frontend for example on Apache HTTPD server. The idea that compiled gwt is pure javascript and don't need java backend (if we don't use java based RPC)
There's nothing stopping you. GWT code breaks down into two parts; server-side and client-side code. As you say the client-side code compiles down into pure javascript which can be easily served up by httpd.
The main advantage of using gwt's classes on the server is that data you request via RPC will arrive in your client code as java objects with no work on your part. But you can easily make calls to any old service using the RequestBuilder class, or XMLHttpRequest if you need more control.
Edit: There is one special bit of configuration you should do to make sure httpd works well with your client-side gwt code:
<Files *.nocache.*>
ExpiresDefault "access"
</Files>
<Files *.cache.*>
ExpiresDefault "now plus 6 months"
</Files>
This makes sure that when you upload a new version of the app users' browsers will download it automatically, but also makes sure that they won't download the entire app every time they visit your website. Very useful.
Your opinion is wrong. You can create a gwt application which is designed only for front-end.
To test that you can do the following
Create a sample gwt application which has only front end content
Compile and build the application
Place the build content folder in your Apache web directory.
Ee : if you created a project called test-gwt, the JS and HTML contents are in test-gwt directory created inside the war directory.
Access the new application through a web browser, like http://localhost/test-gwt
Hope this would help.
That is possible and works like a charm as long as you don't write your server component with gwt.
Here is an simple gwt client only htaccess password app (currently only german) as an example, wich i've coded for fun.
"I'm wondering is it possible to run gwt applicaton as frontend for example on Apache HTTPD server".
The answer is NO. GWT UI frontend does not run on any server at all. It runs only on a browser with Javascript enabled.
But if I ignore the language semantics of the question and answering the gist of it - the answer is Yes. Pardon me, but I think the language you should have written is
"is it possible to SERVE the gwt applicaton frontend from an Apache HTTPD server".
Just deploy your servlet-less war onto the HTTPD (removing the WEB-INF folder).
In fact, you could even write RequestBuilder requests to request for static json streams from files placed in the GWT module development "public" folder. Where the GWT compiler would copy the contents of the "public" folder to the deployed application root.
Or you could use script-include to get your GWT client request for dynamic content jsonp from another server - which would not create any servlet in your app. I documented the script-include technique here: http://h2g2java.blessedgeek.com/2011/06/gwt-requestbuilder-vs-rpc-vs-script.html.

Speeding up code changes in Eclipse Web Browser?

I'm developing an application using the Vaadin framework in Eclipse. I'm using the Tomcat v6.0 servlet and run the application in the Eclipse Web Browser. A problem I've been having though is to have recent changes show in the browser when I test the application.
No matter how many times I restart Tomcat, clean all published resources and restart the Eclipse Web Browser the changes still won't take effect. The changes seem to take effect randomly where time is the biggest factor, which is of great frustration when developing...
So my question is if anyone else has noticed this problem and have any ideas of how to solve it, if there is a configuration I can do or if I'm missing a step in the restart which blocks the changes from taking effect..?
Any help would be greatly appreciated!
In Vaadin most of the code runs in the server and is contained in normal Java files. There are three levels of resource/class changes:
The runtime "hot code replacement". If running Tomcat in debug mode some Java class changes can be published without redeploying the web application. However, if the Tomcat is configured to "auto publish" (check your server settings in Eclipse), the redeployment is automatically done whenever classes change and this causes full context reload and sessions serialization (see #2) . Hot code replacement can be enhanced using tools like JRebel.
Web application deployment. This is essentially deploying a new war file to the server. Causes the previous version to be undeployed and deploys the new version of all classes and resources. Sometimes there are some resources left in the servers work directory or classes are not reloaded, in which case the server restart (#3) is needed.
Server restart. This makes the whole JVM to reload and all the classes and web applications are also reloaded. Still cleaning the work directory separately is needed to make sure everything is reloaded.
In addition to this there is the client-side part of Vaadin (essentially a JavaScript compiled with GWT), which is treated as a static resource by Tomcat. If you modify the client-side Java code the GWT is used to recompile the JavaScript. Deployment should be simply file copying. The browsers cache the generated HTML/JS files, but GWT includes mechanism to avoid this.
You should first try to change the server settings for automatic publishing and see if that helps. Also, I've noticed that different Tomcat version behave differently. This is unfortunate, but the only thing you can do is to try to find the versions/set-up that works for you.
Just to make sure: you have been adding ?restartApplication in the URL to force application to restart on page reload, haven't you?

GWT server part?

I'm thinking about an application where in some cases both client and server would run on customer's computer. Concerning the client's resource usage I've found this question, concerning the general disadvantages of GWT I've found this, but I can't find anything about the overhead of the server part. I need no application server there, anything capable of running the server part of GWT would do.
What is needed to run the server part of GWT and how many resources it consumes?
If you use a ServiceImpl w/ your GWT app, you need to deploy it into a servlet container, like Tomcat or Jetty (or many others). Otherwise, it can be deployed on any web server, as it will only consist of javascript, HTML, and CSS.