Showing posts with label Introduction. Show all posts
Showing posts with label Introduction. Show all posts

Thursday, 6 March 2014

JSP <=> Servlets

A servlet is in essence, a Java class that is executed by the server in response to a client request. A JSP file on the other plays many different roles in the context of a web application. For one role, it could simply be a view that renders the server side response in HTML, XML, JSON or other formats. For another, it could be the intermediary code like ASP or PHP that does some kind of input validation and thereafter forwards this input data to a server for persisting the same in permanent storage. Whatever the role, a JSP is compiled into a servlet and the JSP scriptlet code itself constitutes a part of the doGet() or doPost() methods of the servlet. 

What one needs to internalize here is this interesting relationship between JSPs and Servlets for the former eventually gets rendered into the latter. This leads to a critical inference regarding JSPs and Java classes. A servlet being a Java class, the ultimate form that a JSP takes is a Java class. This relationship between JSPs and Java classes leads to yet another interesting result. A class is composed of member functions and data, so a JSP that eventually gets compiled into a class must provide some mechanism to declare and define class members. The answer is of course, yes. Using the declaration tag, one may declare and define class members in the very same way as in a Java class itself. An example is shown below:

<%!

private String name;
private Integer roll;

public void setName (String name) {
this.name = name;
}

%>

Now one may call a function as shown above from a scriptlet as shown below:

<% 
setName ("Joe");
%>

It's important not to miss the forest for the trees. The crux of the idea is that every web servlet is nothing more than a simple class and the information being passed around is nothing more than a simple object. When you learn object-oriented programming for the first time, it is not very uncommon to read that object passing facilitates message communication. While then it appealed to the senses as nothing more than some kind of an abstract fantasy, we are now seeing rather elegant manifestations of that concept.






Monday, 24 February 2014

Insight into Web Services for beginners

There have been a lot of questions I have been asking myself ever since I started studying computers in my undergrad. I got answers to some of them pretty early while some lingered on in my mind. Fortunately, over the last two years, I have slowly started uncovering answers to more of them.

What is a web service?
This is a pretty interesting question, actually. Now, looking at the two words in isolation, "web" and "service"; one would most likely understand that as a service offered over the web. Well! Yes, but not quite complete and convincing! This is with due consideration to the buzz around the terms when you naively start reading tech posts and keep wondering all along as to what justified the writer's extreme excitement and optimism while talking about something that sounds as trivial as a "web service".

That surely made me understand that there was a void in my understanding of "web service" and it surely is something much more than a service offered over the web. Back then, about 4 years back, I happened to ask my professor about the same thing and I could see that even this was not really helping me look at the thing from a wider perspective and attain that big picture I was looking for.

This morning, I happened to stumble upon this fine post, http://www.dummies.com/how-to/content/getting-a-look-at-web-services.html.

As it suggests, a service offered over the web that may be freely used by another application is called a web service. Sometimes, you may want to use a particular service exported by another service in a specific component of your application. Imagine, you would like to provide a search facility in your website and would like to incorporate the Google Search toolbox for the same reason. How the Google search engine works is of no concern to you and all you care about is that the Google Search toolbox accomplishes its job. Now, the search service provided by Google is an example of web service that is freely exported by Google and may be imported by anybody for use in his/her application.

In case you are a web programmer, you may be familiar with REST and SOAP calls. Typically, the web service is queried by the importer application using the REST or SOAP calls and the response of the web service called service response is rendered and shown to the user. These days, a format such as JSON (JavaScript Object Notation) is used to return the service response and this is rendered by a view-level language such as JSP. For those familiar with AJAX, it is a robust framework and technique that may be used to make the web service call and wait on its response.


Sunday, 29 December 2013

Variables, Scope and Parameter passing


This post serves to explain what exactly pass by reference and pass by value mean in the context of programming and as to how heaps and stacks relate to them.

It is a known fact that both C and Java pass by value.
Let us consider the following segment of code:

int main () {
int a = 5;
int b = 4;
swap (&a, &b);
printf ( “%d : ” , a );
printf (“%d : ” , b );
return 0;
}
void swap ( int *x , int *y ) {
int temp = *x;
*x = *y;
*y = temp;
}
Listing 1

The above code swaps the values a and b so that a contains 4 and b contains 5.
The function call, swap (&a, &b); passes the reference of the variables a and b and this facilitates the actual swap of the values contained in the two variables. Note that references, &a and &b are themselves passed as values and this is precisely why we say function calls in C pass by value.

Local variables or Stack variables

Now, if we take a look at the variable temp in Listing 1, we can see that it is a local variable. Variables which are local in scope are allocated on the stack and when a function returns (or when the last instruction in the function has been executed), the local variables on the stack corresponding to that function are invalidated and the memory thus reclaimed is open for reallocation.

In Listing 1, variables a and b defined in the main function are local variables which are local in scope to the main function and thus reside on the stack. These variables remain stack-resident till the time the main does not return (until after return 0). Likewise, temp is a local variable defined in the swap method and its scope is confined to the swap method only. After the final instruction in swap has been executed temp is invalidated and the memory thus claimed may be re-allocated.

It is important to understand just how relevant this is sometimes and understand its relation to pass by value.

Consider Listing 2.
int sum ( int a , int b )
{
          int s = 0;
          s = a + b;
          return s;
}
int main () {
          int a = 4;
          int b = 5;
          int s = sum ( a, b );
          printf ( “sum: %d\n” , s );
}
Listing 2

Now, we can see that the values contained in a and b are passed to the method sum. It is important to understand here that in C, copies of the values contained in a and b are created and passed as function arguments, whereas in Java, the references of a and b are themselves passed as values. While this may be suggestive of pass by reference, the references are themselves treated as values and it is because of that we say both C and Java pass values.

Now, when the control returns from the function sum, variable s is out of scope and the memory reclaimed and after printf prints sum: 9, variables a and b will also be out of scope. Thus, we can see that stack-resident variables have a scope that is confined to the block where they have been defined.

Variables allocated on the Heap

In C, variables declared outside function body and the variables created using malloc are allocated on the Heap. In C, the variables declared outside function body have file-level scope and may be accessed by all methods contained in that file. Now, variables allocated using malloc remain allocated in the heap till the time you do not explicitly free them using ‘free’ method. In Java, objects allocated using the ‘new’ keyword are allocated on the heap and they are automatically garbage- collected by the JVM when they are no longer used.

Stack and Heap distribution and impact on variable scope

Each thread in a program has its own stack space while all the threads share the same heap space.

Consider the following block of code in Listing 3:
public String foo ( String city1 , String city2 ) {
String city3 = city1 + “,” + city2;
save (city3);
return city3;
}
Listing 3

As it may be apparent, each thread invokes the method foo with its own values for ‘city1’ and ‘city2’ and the method stores the concatenated result in a database using the save function and the reference to the result is also returned. Now, it is important to understand a few things here:

·       city3 is an object of String type and the statement,
String city3 = city1 + “,” + city2; initializes it.

·     Upon initialization, the object that city3 refers to resides on the heap while the reference to it (the name, city3) resides on the stack corresponding to the calling thread.

·   The heap, as mentioned above is shared by all threads but each thread has access to its own local portion of heap, called arena.
·     Now, the stack space corresponding to a thread may in itself be divided into multiple frames, each frame corresponding to a different function or method and bearing variables that are local in scope to that method.

Bullet 3 tells us that the object for a thread, Thread1 referred by ‘city3’ is stored on the heap arena corresponding to Thread1 and thus local in scope to Thread1. From bullet 4, we can see that the reference itself, that is, the name, ‘city3’ is local in scope to the function foo and thereby resident on the stack frame corresponding to foo in the stack space for Thread1.

As the value of the reference or address to the referred object is returned by foo, the copy of the reference, ‘city3’ is in itself invalidated whereas the object itself will remain existent till the time it is not reclaimed by JVM. This is to re-iterate that Java also exchanges data between methods by passing their values which may be data values or values of addresses.

Key things to remember:

1. Threads have their own stack space

2. Heap space is shared between threads but each thread has access to its own portion of the heap space where objects initialized by that thread (using ‘new’ or ‘malloc’ keywords or by any other method of object initialization such as the method shown above for String initialization) are resident until they are not freed explicitly by the user (e.g. using free method for malloc) or automatically (e.g. JVM garbage collection).

3. Methods in a Thread stack have their own stack frames which contain the variables local to those methods.

4. C and Java pass by value.
Two separate C programs have been given below that help us understand the above mentioned ideas. The first one is unreliable in that it passes values of references to local variables resident on its stack outside of its scope while the other works reliably by allocating the values to be passed on the heap by using the malloc keyword.

UNRELIABLE CODE:
#include <stdio.h>
  long power ( int *a );
  long doubler ( int *a );
 
  int main ( int argc , char *argv[] ) { 
    printf ( "Command line argument: %s\n" , argv[1] );
    int operand = *argv[1] - '0';
    printf ( "Entered cmdline operand :%d\n" , operand );
     
    long *result1;
    long *result2;
   
    result1 = power (&operand);
    printf ( "Power of %d: %lu\n", operand, *result1 );
   
    result2 = doubler (&operand);
    printf ( "Double of %d: %d\n", operand, *result2 );
    return NULL;
  }

 long power ( int *a ) {
    printf ( "*a : %d\n" , *a );
    long result1;
    result1 = (*a) * (*a);
    printf ( "*result1 : %lu\n" , result1 );
    return &result1;
  } 

  long doubler ( int *a ) {
    printf ( "*a : %d\n" , *a );
    int result2;
    result2 = (*a) * 2;
    printf ( "*result2 : %d\n" , result2 );
    return &result2;   
  }

RELIABLE CODE:

#include <stdio.h>

  long *power ( int *a );
  int *doubler ( int *a );
 
  int main ( int argc , char *argv[] ) {
 
    printf ( "Command line argument: %c\n" , *argv[1] );
    int operand = *argv[1] - '0';
    printf ( "Entered cmdline operand :%d\n" , operand );
     
    long *result1;
    int *result2;
   
    result1 = power (&operand);
    printf ( "Power of %d: %lu\n", operand, *result1 );
   
    result2 = doubler (&operand);
    printf ( "Double of %d: %d\n", operand, *result2 );
      
    return NULL;
  }
 
  long *power ( int *a ) {
    printf ( "*a : %d\n" , *a );
    long *result1 = (long *) malloc( sizeof (long *));
    *result1 = (*a) * (*a);
    printf ( "*result1 : %lu\n" , *result1 );
    return result1;
  }
 
  int *doubler ( int *a ) {
    printf ( "*a : %d\n" , *a );
    int *result2 = (long *) malloc( sizeof (int *));
    *result2 = (*a) * 2;
    printf ( "*result2 : %d\n" , *result2 );
    return result2;
   }