Sunday, October 22, 2017

JavaScirpt Objects


A JS object is basically an associative array containing a series of key-value pair.  To create a JS object, use the new keyword.  For example,

Var obj1 = new Object();
Obj1.member1 = “this is member 1”;
Obj1.member2 = 3

Another way to create members of an object is to use square bracket (array?) notation:

Obj1[“member1”] = “this is member 1”

The square bracket notation has an advantage that it can use character not allowed for variable name because the member name is specified as string.  For example, Obj1[“1”] works as single digit 1 is not allowed in variable name.  Obj1.1 will flag as error by JS.

Array can be generalized (viewed alternatively) as an object with numeric keys (0, 1, 2, ..).

Sunday, June 18, 2017

GenericServlet

To implement the Servlet interface, one needs to provide classes for all methods specified in the interface, even if the method is not used by the servlet.  To simplify coding, GenericServlet is an abstract class that implemented the Servlet inteface. GenericServlet supplies implementation for the method in the interface so that the real servlet class only needs to override those method it uses.

GenericServlet also saves the ServletConfig object passed by the servlet container to the init method in a persistent variable so the real servlet can access it later on.  GenericServlet adds another init method with no argument for the real servlet to override with its specific code.

GeneicServlet is implemented as abstract class as it expects the real servlet implementation override one of more methods before it is used.

Servlet Interface

The Servlet interface specified s contract between the servlet container and the servlet.  Servlet container calls the init method of the servlet one time during start up.  When a request comes in, servlet engine calls the service method of the servlet to process it.

Servlet engine will also pass in a ServletConfig object to the servlet application's init method.  ServletConfig contains initialization parameters. ServletConfig also provide a SrvletContext object. The ServleCcontext contains global data accessible by all classes in the servlet. In a distributed environment, each servlet instance can access the same ServletContext data.

Servlet also defines two objects - ServletRequest and ServletResponse that used by service method.  ServletRequest defines methods to retrieve parameters submitted in a request.  For example, "id" is the parameter which the method returns as string value.

http:::.../servlet?id=1

ServletResponse simplifies the returning of response to the client. It defines methods to let the application to write the response to a certain stream and let servlet container to handle the rest.

To implement a serlvet, one create a class implement the Servlet interface or extend a class that implemented the Servlet interface.

Monday, May 1, 2017

Function Pointer

Pointer to a function is declared as follows

void (*fn)()

Void is the return type
fn is the function name
() is the parameter types

For example

int (*abc)(int, int) is a function pointer for a function abc which accepts 2 integer arguments and returns an integer result

To use a function pointer

int sum(int a, int b)
{
    return a + b;
)

abc = sum;
z = abc(x, y);


Pointer Arithmatic.

Adding an integer, say i, to a pointer is actually advancing the pointer to i unit of the underlying data type the pointer points to.  For example,

int *p;
p = &j
p = p + 3;  /* p = p + 3*4 as integer is 4 bytes in length */

Likewise, for subtraction, the pointer is deducted by i units of the underlying data type.

When subtracting 2 pointers, the result is the number of units of the underlying data type.  A minus result indicate the first pointer is less than the second pointer in the expression.

Constant and Ponter

const int *p is a pointer to a constant integer.  We cannot change the variable that the pointer is pointing to but we can change the pointer to point to another variable.  However, even if the new variable it points to is not a constant, C runtime will not allow us to change it as it thinks the pointer is pointing to a constant as declared.

For example

const int *p;
const int i = 100
int j = 20

p = &i;  /* ok */
p = &j;  /* ok */
*p = 300;  /* error */

int *const p is a constant point pointing to an integer variable.  The pointer value cannot be change but we can change the variable it points to.

*p = 100;  /* ok */
p = &j;  /* error */

const int *const p is a constant pointer pointing to a constant variable. When the pointer is declard, it must be initialized.

const int *const p = &i

const int *const *p is a pointer to a constant pointer to a constant integer variable

Reading C Pointer Declaration

Reading the pointer declaration backward is easier.

For example, int * p

p = p is a variable
* = p is a pointer variable
int = p is a pointer variable points to an integer

const int * p = a pointer to an constant intege