Showing posts with label newbie blues. Show all posts
Showing posts with label newbie blues. Show all posts

Monday, January 21, 2008

JSON and HashMap

DWR is used for easy 'AJAX for JAVA' and is employed to call Java methods from Javascript (version 2.0 + supports reverse-AJAX too).

Working on the ShopprStream(earlier known as WEB122) has helped me get acquainted with many facets of web-development.


Now I have a Java method that returns a generic HashMap object ;
For this example lets consider that the key is of type String
And the elements are instances of the class CustomClass with name (of type String) as instance variable

The DWR call will return a JSON object. This is how we can access the dat
a


for (var key in hashMap)
{
//if not using Firebug, comment out console.log statements

//'key' is the key in the HashMap
console.log("key : "+ key);

//this will give the no. of elements stored corresponding to the 'key'
console.log("hashMap[key].length: "+ hashMap[key].length);

//Access individual elements corresponding to the 'key'
for(var i=0; i<hashMap[key].length; i++)
{
console.log("hashMap[key][i].name: " + hashMap[key][i].name);
}
}

If the HashMap contains custom objects, make sure it has a corresponding converter entry in the dwr.xml file


for example, if my class is called CustomClass, then write as

<convert converter="bean" match="com.pramati.web122.imsupport.ContactDetail"/>

Here, abc.xyz is the package structure

If object returned is hashmap, then values can be accessed as:
hasMap[key]

If object returned is hashmap, then values can be accessed as:
hasMap[key][0],
hasMap[key][1], hasMap[key][2] . . . . .

Sunday, December 2, 2007

Derby

I had a project work in which I needed to port an existing web-application running on MySQL toDerby. The SQL and datatypes of both are fairly same with minor variations (like syntax for triggers), though Derby does NOT support a few features like 'limit'.

TIPS:

If you are using embedded derby, remember, only ONE connection can be made to the database at a time. If applications running on different JVM's try to access the same database resource at the same time, then except the first one, none will be able to get access. So, go for Network server, if you have any such requirements.

----------------------------------------------

Username & Password

While creating the derby database, if username and password were not specified, and the application you are using requires one, then use the following if it doesn't accept blank/null fields

username: app
password: app

----------------------------------------------

Derby Network Server (for Windows)

Goto
%DERBY_INSTALL%\frameworks\NetworkServer\bin
and type "startNetworkServer.bat"

This will start the server and by default it will listen on port 1527

example:

In my machine Derby is installed in
C:\Program Files\Apache Software Foundation

So, value of the DERBY_INSTALL variable is "C:\Program Files\Apache Software Foundation\db-derby-10.2.1.6-bin"

So, the command "cd %DERBY_INSTALL%\frameworks\NetworkServer\bin"
opens up the following:
C:\Program Files\Apache Software Foundation\db-derby-10.2.1.6-bin\frameworks\Net
workServer\bin>

Now entering "startNetworkServer.bat" starts the server on port 1527

Remember, your derby database files must be located in the
"C:\> cd %DERBY_INSTALL%\frameworks\NetworkServer\bin" directory,
if you are run the server following the aforesaid.

From the program through which you wish access the database, give the following path of the database as:

"jdbc:derby://localhost:1527/dbname"

where 'dbname' is the name of the database.

If the database is located in some other folder, say D:/dbfolder, then modify the above as follows:

"jdbc:derby://localhost:1527/D:/dbfolder/dbname"

----------------------------------------------

Resources: