<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>David&#039;s Blog</title>
	<atom:link href="http://davidsblog.eu/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidsblog.eu</link>
	<description>Web development and programming</description>
	<lastBuildDate>Thu, 05 Jan 2012 12:21:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Remote debugging of Tomcat using Eclipse</title>
		<link>http://davidsblog.eu/remote-debugging-of-tomcat-using-eclipse/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=remote-debugging-of-tomcat-using-eclipse</link>
		<comments>http://davidsblog.eu/remote-debugging-of-tomcat-using-eclipse/#comments</comments>
		<pubDate>Tue, 03 Jan 2012 23:37:54 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Apache Tomcat]]></category>
		<category><![CDATA[debug]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[JEE]]></category>
		<category><![CDATA[JVM]]></category>
		<category><![CDATA[Tomcat]]></category>

		<guid isPermaLink="false">http://davidsblog.eu/?p=215</guid>
		<description><![CDATA[This post shows how to debug an application deployed in Apache Tomcat remotely (Tomcat is a popular Servlet container). This is valid for any other application server or any other Java application since the debug in remote is a feature of the JVM (Java Virtual Machine) over which any Java application is run. For debugging a Java [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">This post shows how to debug an application deployed in <a href="http://tomcat.apache.org">Apache Tomcat</a> remotely (<a href="http://tomcat.apache.org">Tomcat</a> is a popular <a href="http://www.oracle.com/technetwork/java/index-jsp-135475.html">Servlet</a> container). This is valid for any other application server or any other Java application since the debug in remote is a feature of the JVM (Java Virtual Machine) over which any Java application is run.</p>
<p style="text-align: justify;">For debugging a Java application remotely with <a href="http://www.eclipse.org">Eclipse</a> we need three things:</p>
<ol>
<li>Run the application indicating to the JVM that it has to be executed in debug mode.</li>
<li>The source code of the application we want to debug.</li>
<li>Configure <a href="http://www.eclipse.org">Eclipse</a> to debug the application.</li>
</ol>
<p>&nbsp;</p>
<h2>Starting Apache Tomcat in debug mode</h2>
<p style="text-align: justify;">First of all we have to restart the <a href="http://tomcat.apache.org">Tomcat</a> with debug option for this purpose we need to pass to JVM (Java Virtual Machine) the options</p>
<p style="text-align: center;"><a href="http://docs.oracle.com/javase/1.3/docs/tooldocs/solaris/jdb.html">-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n</a></p>
<p style="text-align: justify;">JVM opens a port in current machine allowing external applications (like Eclipse) to connect for debugging purposes.</p>
<p style="text-align: justify;">To do that with <a href="http://tomcat.apache.org">Tomcat</a> we have to set the environment variable JAVA_OPTS that is read by Tomcat in startup.sh file.</p>
<p style="text-align: justify;"><strong>1)</strong> Set variable and make it available. we can choose the port number, in this example is used 8000 number.</p>
<pre class="brush: shell; gutter: true">export JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"</pre>
<p><strong>2)</strong> Start <a href="http://tomcat.apache.org">Tomcat</a> (from <em>bin</em> folder):</p>
<pre class="brush: shell; gutter: true">./startup.sh</pre>
<p><span id="more-215"></span></p>
<p>&nbsp;</p>
<h2>Configuring Eclipse to remote debugging</h2>
<p style="text-align: justify;">First of all, we have to open or import the project with the source code of the application we want to debug. Afterward we have to setup the debug from Eclipse:</p>
<p style="text-align: justify;"><strong>1)</strong> Clicking on the right side (arrow facing to above) of the &#8220;Debug&#8221; button (the button with the bug) that is in the toolbar and the clicking on &#8220;Debug configurations&#8230;&#8221; or in the menu Run &gt; Debug configurations&#8230; Then a new window with the different debug configuration will be opened.</p>
<div id="attachment_219" class="wp-caption aligncenter" style="width: 216px"><a href="http://davidsblog.eu/wp-content/uploads/2012/01/eclipse_debug_configuration.jpeg"><img class="size-full wp-image-219" title="eclipse_debug_configuration" src="http://davidsblog.eu/wp-content/uploads/2012/01/eclipse_debug_configuration.jpeg" alt="" width="206" height="132" /></a><p class="wp-caption-text">Debug configuration</p></div>
<p style="text-align: justify;"><strong>2)</strong> Select &#8220;Remote Java Application&#8221; in the left menu and then press on &#8220;New launch configuration&#8221; button (the button with the folder icon in the top-left corner).</p>
<div id="attachment_221" class="wp-caption aligncenter" style="width: 681px"><a href="http://davidsblog.eu/wp-content/uploads/2012/01/eclipse_new_debug_configuration.jpeg"><img class="size-full wp-image-221" title="eclipse_new_debug_configuration" src="http://davidsblog.eu/wp-content/uploads/2012/01/eclipse_new_debug_configuration.jpeg" alt="" width="671" height="600" /></a><p class="wp-caption-text">New debug configuration</p></div>
<p style="text-align: justify;"><strong>3)</strong> Fill the configuration with the data corresponding with our Tomcat instance:<strong></strong></p>
<ul>
<li style="text-align: justify;"><strong>Project:</strong> The project we want to debug: it has to contain the source code for the application we want to debug.<strong></strong></li>
<li><strong>Connection Type:</strong> Standard (Socket Attach).<strong></strong></li>
<li><strong>Host:</strong> IP of the server in which is Tomcat installed.<strong></strong></li>
<li style="text-align: justify;"><strong>Port:</strong> Port indicated to JVM in <em>address</em> parameter. Port 8000 in our example</li>
</ul>
<div class="mceTemp mceIEcenter" style="text-align: justify;">
<dl id="attachment_220" class="wp-caption aligncenter" style="width: 648px;">
<dt class="wp-caption-dt"><a href="http://davidsblog.eu/wp-content/uploads/2012/01/eclipse_debug_configuration_data.jpeg"><img class="size-full wp-image-220" title="eclipse_debug_configuration_data" src="http://davidsblog.eu/wp-content/uploads/2012/01/eclipse_debug_configuration_data.jpeg" alt="" width="638" height="680" /></a></dt>
<dd class="wp-caption-dd">Debug configuration sample data</dd>
</dl>
</div>
<h2>Remote Debugging of Tomcat with Eclipse</h2>
<p style="text-align: justify;">4) Execute the debug. We have to click on the button &#8220;Debug&#8221; and Eclipse will connect with Tomcat (it had to be previously running as I mentioned in this post). Now we have to put the breakpoints inside the Eclipse project to debug as we do with a regular project running in a local Tomcat.</p>
<p style="text-align: justify;">And that&#8217;s all, if everything works well you can debug your Tomcat remotely. Now there is a long work to do debugging your app. Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://davidsblog.eu/remote-debugging-of-tomcat-using-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to execute a SQL Query from a Shell Script</title>
		<link>http://davidsblog.eu/how-to-execute-a-sql-query-from-a-shell-script/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-execute-a-sql-query-from-a-shell-script</link>
		<comments>http://davidsblog.eu/how-to-execute-a-sql-query-from-a-shell-script/#comments</comments>
		<pubDate>Tue, 27 Dec 2011 23:33:25 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Shell Script]]></category>
		<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[shell script]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[UNIX]]></category>

		<guid isPermaLink="false">http://davidsblog.eu/?p=174</guid>
		<description><![CDATA[MySQL Database For this example we&#8217;re going to use MySQL as database management system. We&#8217;re going to operate over testdb database which contains the people table: And the following tuples: +----+---------+-----+------------+ &#124; id &#124; name &#124; age &#124; bornPlace &#124; +----+---------+-----+------------+ &#124; 1 &#124; John &#124; 34 &#124; London, UK &#124; &#124; 2 &#124; Patrick &#124; 28 &#124; [...]]]></description>
			<content:encoded><![CDATA[<h2>MySQL Database</h2>
<p style="text-align: justify;">For this example we&#8217;re going to use <a title="MySQL" href="http://www.mysql.com">MySQL</a> as database management system. We&#8217;re going to operate over <em>testdb</em> <em>database</em> which contains the <em><em>people</em></em> table:</p>
<p><a href="http://davidsblog.eu/wp-content/uploads/2011/12/test-db.jpg"><img class="size-full wp-image-183 aligncenter" title="people table" src="http://davidsblog.eu/wp-content/uploads/2011/12/test-db.jpg" alt="people table" width="201" height="101" /></a></p>
<p>And the following tuples:</p>
<pre class="brush: text; gutter: true">+----+---------+-----+------------+
| id | name    | age | bornPlace  |
+----+---------+-----+------------+
|  1 | John    |  34 | London, UK |
|  2 | Patrick |  28 | NY, USA    |
+----+---------+-----+------------+</pre>
<p>This is the MySQL SQL script for generating the <em>testdb</em> database:</p>
<pre class="brush: sql; gutter: true">create database testdb;
use testdb;
create table people(id INT, name varchar(100),
                    age INT, bornPlace varchar(100));
insert into people values(1,"John",   34,"London, UK");
insert into people values(2,"Patrick",28,"NY, USA");</pre>
<p>&nbsp;</p>
<h2>Understanding execution of SQL Query from Shell</h2>
<p style="text-align: justify;">MySQL allows user to execute queries from the shell with <em><a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?mysql">MySQL command</a></em><em>. </em>The query can be passed as input using a pipe, like in this example:<span id="more-174"></span></p>
<pre class="brush: shell; gutter: true">echo "select name, age, bornPlace FROM people" | mysql -u david --password=1234 --database=testdb</pre>
<p style="text-align: justify;">The database system returns the data and the fields names of the table as first row. This is the result:</p>
<pre class="brush: text; gutter: true">name    age     bornPlace
John    34      London, UK
Patrick 28      NY, USA</pre>
<p>&nbsp;</p>
<h2>Shell script</h2>
<p style="text-align: justify;">This is the script for retrieving the registries from the database (<a href="http://davidsblog.eu/wp-content/uploads/code/shellscript/test-db.sh">download it</a>):</p>
<pre class="brush: shell; gutter: true">USER_NAME=david
USER_PWD=1234

DB_NAME=test

flag=0
delimiter="a3f4g52"

echo "select name, '"$delimiter"', age, '"$delimiter"', bornPlace FROM people" | \
mysql -u $USER_NAME --password=$USER_PWD --database=$DB_NAME | \
while read row ;
do
  if [ $flag == 0 ]; then
    flag=1
  else
    name=`     echo $row | \
               awk -F "$delimiter" '{print $1}' | \
               sed -e 's/ *$//' | sed 's/^ *//'`
    age=`      echo $row | \
               awk -F "$delimiter" '{print $2}' | \
               sed -e 's/ *$//' | sed 's/^ *//'`
    bornPlace=`echo $row | \
               awk -F "$delimiter" '{print $3}' | \
               sed -e 's/ *$//' | sed 's/^ *//'`

    echo "&gt;$name&lt;"
    echo "&gt;$age&lt;"
    echo "&gt;$bornPlace&lt;"
  fi
done</pre>
<p style="text-align: justify;">To can split later the fields returned by the query, in <em>select</em> query is inserted a delimiter between each field to retrieve. This will help us later to split the line up with this delimiter. This is the <em>select</em> query and the result of its execution with <em><a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?mysql">mysql command</a> </em>command.</p>
<pre class="brush: shell; gutter: true">david@david$ echo "select name, 'a3f4g52', age, 'a3f4g52', bornPlace FROM people" | mysql -u david --password=1234 --database=testdb
name    a3f4g52 age     a3f4g52 bornPlace
John    a3f4g52 34      a3f4g52 London, UK
Patrick a3f4g52 28      a3f4g52 NY, USA</pre>
<p style="text-align: justify;">The <strong><em>whi</em><em>le</em> loop</strong> reads each line:</p>
<ul>
<li style="text-align: justify;"><strong><em>if-else</em> sentence</strong> with <em>flag</em> variable avoid reading the first row which contains the name of the fields</li>
<li style="text-align: justify;"><strong><em><a title="man awk" href="http://unixhelp.ed.ac.uk/CGI/man-cgi?awk">awk</a></em> command</strong> to separate each row by the delimiter.</li>
<li style="text-align: justify;"><strong><em><a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?sed">sed</a></em> command</strong> to remove the spaces in beginning and end of the fields with the <a href="http://www.gnu.org/software/sed/manual/sed.html#The-_0022s_0022-Command">s command</a> <em>&#8216;s/regexp/replacement/flags&#8217;:</em></li>
<ul>
<li style="text-align: justify;">Trailing spaces: <em>sed -e &#8216;s/ *$//&#8217;<br />
</em></li>
<li style="text-align: justify;">Beginning spaces: <em>sed -e &#8216;s/^ *//&#8217;</em></li>
</ul>
</ul>
<p style="text-align: justify;">And this is the output of the shell script, it shows each field in a new line and cleaned of spaces.</p>
<pre class="brush: text; gutter: true">&gt;John&lt;
&gt;34&lt;
&gt;London, UK&lt;
&gt;Patrick&lt;
&gt;28&lt;
&gt;NY, USA&lt;</pre>
<p style="text-align: justify;">In this post is presented how to make a <em>select</em> query an retrieve the results. I hopeyou can adapt this simple example to create most complex programs.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidsblog.eu/how-to-execute-a-sql-query-from-a-shell-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Playground</title>
		<link>http://davidsblog.eu/google-playground/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-playground</link>
		<comments>http://davidsblog.eu/google-playground/#comments</comments>
		<pubDate>Wed, 21 Dec 2011 20:29:48 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Google]]></category>
		<category><![CDATA[Google API]]></category>
		<category><![CDATA[IDE]]></category>
		<category><![CDATA[OAuth]]></category>
		<category><![CDATA[playground]]></category>
		<category><![CDATA[REST services]]></category>

		<guid isPermaLink="false">http://davidsblog.eu/?p=160</guid>
		<description><![CDATA[Today I would like to talk about an amazing tool developed by Google that is not too popular among web developers: Google Playground. Google Code Playground Google Code Playground is very powerful tool provided by Google in order to help developers &#8220;playing&#8221; with its APIs such as Language, Maps, Visualization, etc. You can navigate between a [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Today I would like to talk about an amazing tool developed by Google that is not too popular among web developers: Google Playground.</p>
<h2>Google Code Playground</h2>
<p style="text-align: justify;"><a title="Google Code Playground" href="http://code.google.com/apis/ajax/playground/">Google Code Playground</a> is very powerful tool provided by Google in order to help developers &#8220;playing&#8221; with its APIs such as <a href="http://code.google.com/intl/es/apis/language/">Language</a>, <a href="code.google.com/apis/maps/index.html">Maps</a>, <a href="http://code.google.com/intl/es/apis/chart/index.html">Visualization</a>, etc. You can navigate between a wide quantity of examples for each API, each example comes with the source code for implementing them.</p>
<div id="attachment_165" class="wp-caption aligncenter" style="width: 575px"><a href="http://code.google.com/apis/ajax/playground/"><img class="size-large wp-image-165" title="Google Code Playground" src="http://davidsblog.eu/wp-content/uploads/2011/12/google-code-playground-1024x787.jpg" alt="Google Code Playground" width="565" height="434" /></a><p class="wp-caption-text">Google Code Playground</p></div>
<p style="text-align: justify;">But actually <a title="Google Code Playground" href="http://code.google.com/apis/ajax/playground/">Google Playground</a> is more than this. <span id="more-160"></span>It is a kind of web IDE (Integrated Development Environment), you can edit the example source code and program your own JavaScript snippets for the different APIs. This tools allows you to edit code, run or even debug the code and then see the results from the browser.</p>
<p style="text-align: justify;">You can start from a example, adapt it to your needs and then copy the code to insert in your page. If you are used to use Google APIs I am sure that this tool will be very useful for you.</p>
<h2>Google OAuth 2.0 Playground</h2>
<p style="text-align: justify;"><a title="Gogle OAuth 2.0 Playground" href="https://code.google.com/oauthplayground/">Google OAuth 2.0 Playground</a> is another playground more complex for end users, but developers can to take advantage of it. It lets you play with <a title="OAuth 2.0" href="http://oauth.net/documentation/getting-started/">OAuth 2.0 protocol</a> and Google APIs that supports it, like <a href="http://developers.google.com/+/api/">Google+</a>, <a href="http://code.google.com/apis/contacts/">Google Contacts</a>, <a href="http://code.google.com/apis/spreadsheets/">Google Spreadsheet</a>, etc. You can make HTTP requests and the playground shows you the request header and the response header and body. This playground lets you:</p>
<ul>
<li style="text-align: justify;">Select and authorize Google APIs through OAuth 2.0 protocol.</li>
<li style="text-align: justify;">Exchange authorization code for tokens</li>
<li style="text-align: justify;">Construct your HTTP request and invoke Google services by specifying the URI, HTTP Method, headers, content type and request body.</li>
</ul>
<p style="text-align: justify;">I highly recommend you these playgrounds if you someday you develop some application using the Google APIs.  I hope you enjoy these playgrounds playing like children.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidsblog.eu/google-playground/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSONP with jQuery</title>
		<link>http://davidsblog.eu/jsonp-with-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jsonp-with-jquery</link>
		<comments>http://davidsblog.eu/jsonp-with-jquery/#comments</comments>
		<pubDate>Fri, 02 Dec 2011 22:35:57 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[JSONP]]></category>

		<guid isPermaLink="false">http://davidsblog.eu/?p=135</guid>
		<description><![CDATA[Today I would like to introduce you a faster and easy way for working with JSONP thanks to the jQuery JavaScript library. To demostrate how to use JSONP with jQuery let&#8217;s take the same REST service of my latest post about JSONP, the Google Geocoding API (V2). jQuery has a function called ajax() which allows [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Today I would like to introduce you a faster and easy way for working with JSONP thanks to the <a title="jQuery" href="http://jquery.com">jQuery JavaScript library</a>.</p>
<p style="text-align: justify;">To demostrate how to use JSONP with jQuery let&#8217;s take the same REST service of <a title="JSONP: JSON with padding" href="http://davidsblog.eu/jsonp-json-with-padding#Complex-Example">my latest post about JSONP</a>, the <a title="Google Geocoding API v2" href="http://code.google.com/apis/maps/documentation/geocoding/v2/">Google Geocoding API (V2)</a>.</p>
<p style="text-align: justify;">jQuery has a function called ajax() which allows developers to invoke AJAX for calls between same domain and JSONP for calls between cross domains.</p>
<p style="text-align: justify;">At least four options have to be set as argument of <a href="http://api.jquery.com/jQuery.ajax/">jQuery ajax function</a>:</p>
<ul>
<li style="text-align: justify;"><strong>url:</strong> the URL of the service. This argument is going to be filled dynamically to take the place from the input field of the form. (See more details in the post  <a title="JSONP: JSON with padding" href="http://davidsblog.eu/jsonp-json-with-padding#Complex-Example">JSONP: JSON with padding</a> complex example)</li>
<li style="text-align: justify;"><strong>dataType:</strong> for JSONP calls must always be &#8216;jsonp&#8217;, we are handling JSONP format.</li>
<li style="text-align: justify;"><strong>crossDomain:</strong> for JSONP calls must always be &#8216;true&#8217; if we are making a call to a different domain. (We also can make JSONP calls in the same domain, but in this case is much more simple to use AJAX and JSON as return format).</li>
<li style="text-align: justify;"><strong>success:</strong> the function where the JSON passed as argument of the JSONP is processed. In it should be implemented the logic for processing the JSON.</li>
</ul>
<p><span id="more-135"></span></p>
<p style="text-align: justify;">Another clarifications about the code:</p>
<ul>
<li style="text-align: justify;">Lines 14-26: A function is defined for executing when user makes click on search button.</li>
<li style="text-align: justify;">Line 15: <em>$(&#8220;#place&#8221;).val()</em> extracts the value introduced by the user in the text field. This value is used to construct the URL for invoking the service.</li>
<li style="text-align: justify;">Line 22: In it is selected the coordinates from the JSON returned by the service and is stored in <em>coordinates</em> variable.</li>
<li style="text-align: justify;">Line 23: The coordintates are inserted in the &lt;p&gt; with id <em>coordinates</em> using the function <em>text()</em>.</li>
</ul>
<pre class="brush: html; gutter: true">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Coordinates Search&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Search the coordinates of your favourite place&lt;/h1&gt;
    &lt;input type="text" id="place" value="" /&gt;
    &lt;input type="button" id="search" value="Search "/&gt;
    &lt;p id="coordinates"&gt;&lt;/p&gt;

    &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript" &gt;
      $(document).ready(function() {
        $("#search").click(function() {
          var url = "http://maps.google.com/maps/geo?output=json&amp;sensor=false&amp;q=" + $("#place").val();

          $.ajax({
            url: url,
            dataType: 'jsonp',
            crossDomain: true,
            success: function(response) {
              var coordinates = response["Placemark"][0]["Point"]["coordinates"];
              $("#coordinates").text(coordinates[0] + ", " + coordinates[1]);
            }
          });
        });
      });
    &lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
<p>You can see the example working <a href="http://davidsblog.eu/wp-content/uploads/code/javascript/CoordinatesSearchJQuery.html">here</a>. Enjoy it!</p>
]]></content:encoded>
			<wfw:commentRss>http://davidsblog.eu/jsonp-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSONP: JSON with Padding</title>
		<link>http://davidsblog.eu/jsonp-json-with-padding/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jsonp-json-with-padding</link>
		<comments>http://davidsblog.eu/jsonp-json-with-padding/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 19:22:42 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[JSONP]]></category>
		<category><![CDATA[REST services]]></category>

		<guid isPermaLink="false">http://davidsblog.eu/?p=71</guid>
		<description><![CDATA[Introduction JSONP (JSON with padding) is a method for making asynchronous requests in different domains, it is commonly used for invoking RESTful Services. JSONP is conceived because of the lack of AJAX for performing requests to resources that are in different domain. The same origin policy prevents that AJAX from being used across domain. A [...]]]></description>
			<content:encoded><![CDATA[<h2 style="text-align: justify;">Introduction</h2>
<p style="text-align: justify;">JSONP (JSON with padding) is a method for making asynchronous requests in different domains, it is commonly used for invoking RESTful Services. JSONP is conceived because of the lack of AJAX for performing requests to resources that are in different domain. The same origin policy prevents that AJAX from being used across domain. A web page which is placed under davidsblog.eu can only access (through AJAX) to data which is under the domain davidsblog.eu and in the same port (in this case the port 80).</p>
<h2 style="text-align: justify;">JSON</h2>
<p style="text-align: justify;">For understanding JSONP, the first requirement is to understand JSON. JSON (JavaScript Object Notation) is a lightweight data interchange format. This format is copied from the way for representing associative arrays (objects) in JavaScript.</p>
<p style="text-align: justify;">This is a JSON which represents a person with three properties: name, age and sex.</p>
<pre class="brush: javascript; gutter: true">{
    "name" : "John"
    "age" : 23,
    "sex": "MALE"
}</pre>
<p>In JavaScript this type of structures can be accessed in two ways:</p>
<ul>
<li>Associative array style:</li>
</ul>
<pre class="brush: javascript; gutter: true">var name = json["name"];</pre>
<ul>
<li>Through point operator:</li>
</ul>
<pre class="brush: javascript; gutter: true">var name = json.name;</pre>
<p style="text-align: justify;">Once JSON is understood, JSONP format is just &#8220;function(json)&#8221;, a function whose unique parameter is a JSON. Example:</p>
<pre class="brush: javascript; gutter: true">processPerson({ "name" : "John", "age" : 23, "sex": "MALE" });</pre>
<p>Yes, it is not too complicated at all.</p>
<h2 style="text-align: justify;">How can we do a JSONP call?</h2>
<p style="text-align: justify;">We have to include the URL of the service in a &lt;script&gt; HTML tag:</p>
<p style="text-align: justify;"><span id="more-71"></span></p>
<pre class="brush: javascript; gutter: true">&lt;script type="text/javascript" src="URL"&gt;&lt;/script&gt;</pre>
<p style="text-align: justify;">When the &lt;script&gt; tag is read by the browser, it loads the JavaScript file indicated in the URL, in this case a service which returns JSONP. When the browser interprets the JavaScript, the function indicated in the JSONP code is invoked with the JSON as argument.</p>
<h2 style="text-align: justify;">Simple Example: JSONP for dummies</h2>
<p style="text-align: justify;">This is an example of JSONP (<a href="http://davidsblog.eu/wp-content/uploads/code/javascript/jsonp.js">jsonp.js</a>):</p>
<pre class="brush: javascript; gutter: true">printName({"name":"John"});</pre>
<p style="text-align: justify;">The following example is an HTML page which a JavaScript script. A function called printName is defined. This function retrieves the heading h2 with id &#8220;userName&#8221; and inserts text. This text is extracted from the JSON parameter, the property &#8220;name&#8221; of the object. This is the file index.html</p>
<pre class="brush: html; gutter: true">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;JSONP Example&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;JSONP Example&lt;/h1&gt;
    &lt;h2 id="userName"&gt;&lt;/h2&gt;
    &lt;script type="text/javascript"&gt;
      function printName(json) {
        var el = document.getElementById("userName");
        el.innerHTML = "Hello " + json.name;
      }
    &lt;/script&gt;
    &lt;script type="text/javascript" src="jsonp.js"&gt;&lt;/script&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
<p style="text-align: justify;">You can see the example working <a href="http://davidsblog.eu/wp-content/uploads/code/javascript/JsonpExample.html">here</a>. Enjoy it!</p>
<pre><strong>JSONP Example</strong>
Hello John</pre>
<h2 id="Complex-Example" style="text-align: justify;">Complex Example: JSONP for experts</h2>
<p style="text-align: justify;">This time I am going to use a real REST service from Internet, <a title="Google Geocoding API v2" href="http://code.google.com/apis/maps/documentation/geocoding/v2/">Google Geocoding API (V2)</a>. This service returns a JSON, XML o JSONP with the coordinates of a place given its name.</p>
<p style="text-align: justify;">This is the URL of the service http://maps.google.com/maps/geo, these are the query parameters we are going to use:</p>
<ul>
<li style="text-align: justify;"><strong>output:</strong> output format (json)</li>
<li style="text-align: justify;"><strong>sensor:</strong> indicates whether or not the geocoding request comes from a device with a location sensor (false)</li>
<li style="text-align: justify;"><strong>q:</strong> the place&#8217;s name (this argument is going to be filled dynamically to take it from the input field of the form)</li>
<li style="text-align: justify;"><strong>callback:</strong> for JSONP, the name of the function you want to invoke (showCoordinates)</li>
<li style="text-align: justify;"><strong>key:</strong> key for using Google API. It is not necessary if you test it from localhost.</li>
</ul>
<p>Example: <a href="http://maps.google.com/maps/geo?output=json&amp;sensor=false&amp;q=Madrid&amp;callback=showCoordinates">http://maps.google.com/maps/geo?output=json&amp;sensor=false&amp;q=Madrid&amp;callback=showCoordinates</a></p>
<p>This is the HTML file (<a href="http://davidsblog.eu/wp-content/uploads/code/javascript/CoordinatesSearch.html">download it</a>) with the HTML and JavaScript code:</p>
<pre class="brush: html; gutter: true">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Coordinates Search&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Search the coordinates of your favourite place&lt;/h1&gt;
    &lt;input type="text" id="place" value="" /&gt;
    &lt;input type="button" onclick="searchCoordinates();" value="Search "/&gt;
    &lt;p id="coordinates"&gt;&lt;/p&gt;

    &lt;script type="text/javascript"&gt;
      function showCoordinates(response) {
        var p = document.getElementById("coordinates");
        var coordinates = response["Placemark"][0]["Point"]["coordinates"];
        p.innerHTML = coordinates[0] + ", " + coordinates[1];
      }

      function searchCoordinates() {
        var place = document.getElementById("place").value;
        var url = "http://maps.google.com/maps/geo?output=json&amp;sensor=false&amp;q=" +
                  place + "&amp;callback=showCoordinates";

        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.charset = 'utf-8';
        script.src = url;
        document.getElementById("scripts").appendChild(script);
      }
    &lt;/script&gt;

    &lt;div id="scripts"&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;</pre>
<p>The HTML has mainly these three components:</p>
<ul>
<li style="text-align: justify;">A form with an input field to introduce the place to search its coordinates (line 7).</li>
<li style="text-align: justify;">A button to submit the search request (line 8): calls the searchCoordinates JavaScript function.</li>
<li style="text-align: justify;">A &lt;p&gt; to display the coordinates once they are retrieved with Google&#8217;s service (line 9).</li>
</ul>
<p>The JavaScript contains two functions:</p>
<ul>
<li style="text-align: justify;">searchCoordinates: Invokes the <a title="Google Geocoding API v2" href="http://code.google.com/apis/maps/documentation/geocoding/v2/">Google Geocoding API</a> to retrieve the coordinates:
<ul>
<li style="text-align: justify;">It extracts the text from the input field (line 19)</li>
<li style="text-align: justify;">Uses it to construct the URL to invoke the service (line 20), the callback function is showCoordinates. Notice: You also have to include the Google key if you test it in a server instead of localhost (see <a href="http://davidsblog.eu/wp-content/uploads/code/javascript/CoordinatesSearch.html">complete example</a>).</li>
<li style="text-align: justify;">Inserts a &lt;script&gt; with the URL of the service.</li>
</ul>
</li>
<li style="text-align: justify;">showCoordinates: It selects the coordinates from the JSON returned by the service and inserts it in the &lt;p&gt; of line 9.</li>
</ul>
<p>You can see the example working <a href="http://davidsblog.eu/wp-content/uploads/code/javascript/CoordinatesSearch.html">here</a>. Enjoy it!</p>
]]></content:encoded>
			<wfw:commentRss>http://davidsblog.eu/jsonp-json-with-padding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hello world!</title>
		<link>http://davidsblog.eu/hello-world/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hello-world</link>
		<comments>http://davidsblog.eu/hello-world/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 12:27:45 +0000</pubDate>
		<dc:creator>david</dc:creator>
				<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://davidsblog.eu/?p=1</guid>
		<description><![CDATA[Hello everybody! This is my first post of many (I hope). This blog will be about Computer Science, especially Software Engineering: design and programming. I would also like to write about some of my hobbies like travelling and sports. I hope you like this blog and you visit me sometimes. Welcome!]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Hello everybody! This is my first post of many (I hope). This blog will be about Computer Science, especially Software Engineering: design and programming. I would also like to write about some of my hobbies like travelling and sports. I hope you like this blog and you visit me sometimes. Welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://davidsblog.eu/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
