Search This Blog

Saturday, July 30, 2011

Thinking ahead - GWT server side : Java or PHP

   I think this is very important topic, because I struggled many times with server communication, because it wasn't well explained in documentation.
   Working with my project, I had to decide what will be used on server side. As it was recommended on GWT web page to use Java, I thought to give it a try (but changed later to PHP). GWT has great interface for using Java on server side. http://code.google.com/webtoolkit/doc/latest/tutorial/RPC.html You just have to write server side classes on the same project...and that's all. Then it is very easy to use these functions...because you don't have to worry about data sending types, names, response parsing... it is just like calling simple function. BUT, because I am not a Java guy I couldn't do many things that had to be accomplish on server (e.g. file uploading with JumpLoader, sessions). And of course there is the problem with Java servers JBoss, GlassFish a.o. --- I just think they are hard to configure. So then I decided to use PHP with Apache...easy and proven value.
 
   Application development - debug mode


   Before I continue, something has to be explained - how application development works.
   When you are writing code and want to test it, you have to do it in Debug mode - because the compilation to JavaScript takes some time. There are two ways how you can launch the debug mode :

  •  normal Web application - then some kind of GWT's Java server (named Jetty) is started and server side code is executed on it - great if you are developing with Java server code. And can be used if there is no communication with server, just a static web page with widgets or something else. I guess Jetty server doesn't support PHP.
  • Web application (running on external server) - then Jetty isn't launched, and you don't need one, because the communication is done with PHP via JSON or XML (recommend JSON). BUT then arises one big problem I struggled with for many hours.... 
    If you put project in default Eclipse workspace path and launch normal Web application mode, then 1)you have to use full url for server requests (e.g. http://localhost/ManagementSystem/php/login.php) 2)You can't make normal AJAX requests, because browsers implement SOP (Same Origin Policy). In short - browsers forbid to use data from different host or port (in our case Jetty and Apache ports are different). 
  There are workarounds explained here http://code.google.com/webtoolkit/doc/latest/tutorial/Xsite.html , but they are just confusing everything. At the beginning of project I used JSONP requests and everything worked, but the code was complicated and that way you can send data only with GET method. I think IE has some restrictions with url (GET) length. There are situations when you really need to do such a request, i.e. for some kind of a Yahoo service, but that wasn't my case. I will later write an article about how to implement this JSONP with some source code.... the one on GWT's web page is missing something.  
  IF you launch web application (running on external server) then it must be accessible for Apache. 1)You can configure Apache to give access to pages on Eclipse's default workspace directories. 2) Put your project on Apache's public directory (what i did). When creating a project, I just put it in xampp/htdocs/ directory ( Linux would be /var/www/ ).    And...yeah I am using Windows for GWT development, because Linux's Eclipse hurts my eyes :) 
  This way all your project is accessible via browser http://localhost/ProjectName/war/ProjectName.html . You can make a php directory and then requests can be made by  php/requestFile.php . Simple, no problems. And when you are making your application publicly available, just take the war directory and put it on server.

2 comments:

  1. thank you, nice posts.

    ReplyDelete
  2. will you please give me stepts for setting up gwt with php to print only hellow world..

    I am fully confused how to communicate via json/xml and what will be the project structure and where to put php file.

    Will you please tell me step by step.

    Regards
    Shakir husain

    ReplyDelete