Java Constructor Rules

Posted in Java on May 21st, 2010 by Joey

The following rules must be adhered to when creating Java constructors:

1) The name of the constructor must be the same as the class name for which the constructor is being defined.
2) The constructor must not define a return type.
3) If a constructor is not defined for a class, the compiler creates a default constructor. This default constructor contains no arguments.
4) If a constructor is defined for class, the compiler does not create a default constructor.
5) Overloaded constructors may be created but there cannot be two constructors in the same class with the same argument list.

Add Additional Parameters to an ExtJS Tree Node Click

Posted in ExtJS, JavaScript on May 20th, 2010 by Joey

I recently encountered a situation in which I needed to send additional, dynamic parameters to a URL when an ExtJS tree node was clicked. After some trial and error, I discovered that I needed to add a TreeLoader object to my tree. (I was previously trying to use the dataUrl property of the tree itself.)

I had some parameters that were the same for each node. These I defined in the TreeLoader’s baseParams config.

The dynamic parameters I had to handle differently, since I wouldn’t know what they were until the tree node was actually clicked. To capture this click event, I defined the “beforenodeclick” event of the TreeLoader. Then, within this event, I retrieved individual attributes that had been defined for the node (in my case via a JSON packet) and added these to the TreeLoader’s baseParam config.

Example:

What is a Java Constructor?

Posted in Java on May 18th, 2010 by Joey

In Java, a constructor defines the code that is run when a new object is created.

To create a constructor, define a block of code with the same name as the class. For example, the following defines the construtor for a class called “Painting”.

To invoke a constructor, use the “new” keyword, followed by the name of the class.

When creating a Java constructor, remember the Java Constructor Rules.

How to Get the Web Application Root in a JSP

Posted in JSP, Java on May 16th, 2010 by Joey

To get the web application root in a Java Server Page (JSP), use the getRealPath method of the application class, as demonstrated in the example below:

How to Install jQuery

Posted in JavaScript, jQuery on May 15th, 2010 by Joey

Go to http://docs.jquery.com/Downloading_jQuery

Scroll down to the “Current Version” section.

Right-click on “Uncompressed” and select “Save Link As…”

Save the file to your local web root (somewhere like C:\wwwroot\js\)

Now that the file is downloaded, jQuery is ready to be included in your website. Include it with the standard script tag, then check for the existence of jQuery, as shown in the example below:

ExtJS Expand Tree Node Listener

Posted in ExtJS on May 13th, 2010 by Joey

The listener for expanding a node in ExtJS is “expandnode”. This can be added to the “listeners” config of an ExtJS tree. Any code added to the expandnode section will run whenever a node is expanded in the tree. See example below.

How to Show and Hide an ExtJS Tree Node

Posted in ExtJS on May 11th, 2010 by Joey

Reference the example below for the syntax to show and hide an ExtJS tree node:

Note that “tree” is a variable reference to an ExtJS tree and “nodeID” is a node within that tree.

JSP Model View Controller

Posted in Java on May 10th, 2010 by Joey

The model-view-controller (MVC) is a software architecture design pattern. It aims to separate application logic from input and presentation.  The MVC design pattern can be used when writing JSPs.

The model is the Java classes. This is where the application data lives and where application operations take place. Through the Java classes, the view receives input for presentation.

The view is the JSP pages. The JSP pages are used to create the interface through which the data in the model is presented.

The controller is the Java servlets. The servlets manipulate the model. They pass input data back to the model – the Java classes.

The MVC relationship is summarized in the diagram below:

Source: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

How to Create a JSP File in Eclipse

Posted in Eclipse, Java on May 7th, 2010 by Joey

To create a JSP file in Eclipse, follow the steps below.

In your Java project, right-click on the folder in which you wish to create the JSP file. Select “New” -> “File”

In the resulting popup dialog, input a name for the file and click “Finish”:

Next, put some JSP code in the file. For example:

You should then be able to browse to the JSP file, within your application. In my case, the file exists at http://127.0.0.1:8080/website/myfile.jsp and the result is:

//

How to Get a Date With Java

Posted in Java on May 6th, 2010 by Joey

How do you get a date with Java? No, not that kind of date (you were too going to ask). We’re talking about a date date, of the calendar variety. First, import the Java.util.Date class:

Then, simply create a date object:

The result is: