David's Blog

Web development and programming

Remote debugging of Tomcat using Eclipse

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 application remotely with Eclipse we need three things:

  1. Run the application indicating to the JVM that it has to be executed in debug mode.
  2. The source code of the application we want to debug.
  3. Configure Eclipse to debug the application.

 

Starting Apache Tomcat in debug mode

First of all we have to restart the Tomcat with debug option for this purpose we need to pass to JVM (Java Virtual Machine) the options

-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n

JVM opens a port in current machine allowing external applications (like Eclipse) to connect for debugging purposes.

To do that with Tomcat we have to set the environment variable JAVA_OPTS that is read by Tomcat in startup.sh file.

1) Set variable and make it available. we can choose the port number, in this example is used 8000 number.

export JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

2) Start Tomcat (from bin folder):

./startup.sh

Continue reading

How to execute a SQL Query from a Shell Script

MySQL Database

For this example we’re going to use MySQL as database management system. We’re going to operate over testdb database which contains the people table:

people table

And the following tuples:

+----+---------+-----+------------+
| id | name    | age | bornPlace  |
+----+---------+-----+------------+
|  1 | John    |  34 | London, UK |
|  2 | Patrick |  28 | NY, USA    |
+----+---------+-----+------------+

This is the MySQL SQL script for generating the testdb database:

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");

 

Understanding execution of SQL Query from Shell

MySQL allows user to execute queries from the shell with MySQL command. The query can be passed as input using a pipe, like in this example: Continue reading

Google Playground

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 “playing” with its APIs such as Language, Maps, Visualization, etc. You can navigate between a wide quantity of examples for each API, each example comes with the source code for implementing them.

Google Code Playground

Google Code Playground

But actually Google Playground is more than this.  Continue reading

JSONP with jQuery

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’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 developers to invoke AJAX for calls between same domain and JSONP for calls between cross domains.

At least four options have to be set as argument of jQuery ajax function:

  • url: 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  JSONP: JSON with padding complex example)
  • dataType: for JSONP calls must always be ‘jsonp’, we are handling JSONP format.
  • crossDomain: for JSONP calls must always be ‘true’ 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).
  • success: the function where the JSON passed as argument of the JSONP is processed. In it should be implemented the logic for processing the JSON.

Continue reading

JSONP: JSON with Padding

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 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).

JSON

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.

This is a JSON which represents a person with three properties: name, age and sex.

{
    "name" : "John"
    "age" : 23,
    "sex": "MALE"
}

In JavaScript this type of structures can be accessed in two ways:

  • Associative array style:
var name = json["name"];
  • Through point operator:
var name = json.name;

Once JSON is understood, JSONP format is just “function(json)”, a function whose unique parameter is a JSON. Example:

processPerson({ "name" : "John", "age" : 23, "sex": "MALE" });

Yes, it is not too complicated at all.

How can we do a JSONP call?

We have to include the URL of the service in a <script> HTML tag:

Continue reading

Hello world!

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!