Application development life cycle

I am a fan of the traditional waterfall model for application development. I know agile development is the current hype within the software development community, and like all programmers, I hate documentation, but there is something elegant in creating a perfect design for an application before you even fire up your favourite IDE. Below is an example of my typical application development life cycle…

Application development life cycle
1. Requirements! Gather requirements from the client and create a requirements specification.

2. Design! From the confirmed requirements, begin to creation a technical design for the application.

3. Implementation! Development includes looking through the current range of applications and applications templates and picking a similar one as a template. Commit the copy into SVN as the trunk of a new project. From here, start designing your new building block to fit with the technical design. This may involve looking through the any APIs, tag libraries and web services as well as any required third party services.

4. Quality Assurance! Code review and testing.

5. Release! Change control (follow any ITIL processes), Communication and eventually deployment into production.

6. Maintenance! Repeat the above steps for enhancements and bug fixes.

Exception hanlding and logging of pl/SQL

pl/SQL Logging
During a batch process or the execution of pl/SQL, you will want to log the output and the success or failure of the process.

Typically, I always find it good practice to output some simple information such as the start and end date/time of the process as well as any errors that may occur. This is really handy if you are wanting to look at how long a process takes and will give you a feel for if something ‘went wrong’ while running the script.

If you run a script as a one off, you will see the output of your code (package, procedure, anonomous block) in the output from your IDE such as TOAD, SQL developer or SQL*Plus.

Example:

set serveroutput on

... your code

or you can redirect it to a file (see below)

Example:

set serveroutput on
spool log.txt

... your code

spool off

If you run a batch process using crontab or some other scheduler, use this to redirect your output into a file. If you do this, it is always a good idea to time stamp the file.

Exception handling
In your batch pl/SQL applications, you will need to have some exception handling.

If anything, you will want to catch some specific errors as well as others.

In my examples below, I am catching a NO_DATA_FOUND exception and outputting a different error to when any other exception occurs.

Example:

BEGIN 

	DBMS_OUTPUT.PUT_LINE('Batch process started - '||SYSDATE);
	
	... your pl/SQL block that does stuff...

	COMMIT; --commit changes 
	DBMS_OUTPUT.PUT_LINE('Batch process ended - '||SYSDATE);
	
EXCEPTION
   WHEN NO_DATA_FOUND THEN
      ROLLBACK;
	  
      DBMS_OUTPUT.PUT_LINE('No data was found - '||SQLCODE||' -ERROR- '||SQLERRM);

EXCEPTION
   WHEN OTHERS THEN
	  ROLLBACK;
      DBMS_OUTPUT.PUT_LINE('An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END;

Raising your own exceptions
Another option is when you come across a condition in your code where you want to throw an exception. A really good example of this is if a variable ends up with a negative number you may want to raise an exception rather than let the program continue on.

Example:

BEGIN

	IF input > 0 THEN
		... your pl/SQL block that does stuff...
	ELSIF input = -1
		--Raise no data found exception if 
		RAISE NO_DATA_FOUND; 
	
	ELSE
		--Something wrong has happened. Raise an exception. 
		RAISE;
	END IF:

	EXCEPTION
	   WHEN NO_DATA_FOUND THEN
		  DBMS_OUTPUT.PUT_LINE('Please enter a correct paramater and try again.');

	EXCEPTION
	   WHEN OTHERS THEN
		  DBMS_OUTPUT.PUT_LINE('An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
	END;
END;

Creating your own custom exceptions
It is also possible to create your own exceptions and even allocate them to their own error number within the user-specified range of between -20000 – -20999. All other error numbers are reallocated to standard oracle errors. While this is certainly possible, I’ve never found a real life scenario where I have wanted to create my own exceptions as the Oracle keywords and mapping to error numbers have been sufficient for most needs. (hence why I haven’t included an example)

Tutorial 6: Servlets

Servlets are server side java classes that render into a web page. (Similar to Perl/CGI)

How to create your first servlet

From Eclipse…
1. Right click on project.
2. Click on new
3. Click on other
4. Type in servlet (it is in the wizard section)
5. Click next.
6. Add package, class name. In this case the package is com.informalsemantics, the class is FirstServlet.
7. Click next
8. Click Finish.

You have now created the scafolding for a simple servlet, however you will notice some compile time errors.

All of the imports for javax.servlet.* will be underlined red and have compile time errors.
For this, you will need to add the servlet-api.jar to the build path.
To do this…
1. right click on your project, build path, configure build path
2. Click on the libraries tab.
3. Click on Add external JARs
4. Navigate to where you installed Tomcat. (in my case, it was c:\tomcat)
5. Go the libs folder. (c:\tomcat\libs)
6. And select servlet-api.jar.
7. Click select.
8. Click OK.
Eclipse will now recompile your project and the compile time errors will disappear.

You now have FirstServlet.java under src in the package com.informalsemantics. Lets do the usual, and add some code to display “Hello World!”.

Open FirstServlet.java.
There are 3 default methods in your servlet class.
– FirstServlet() – default constructor
– doGet – is called when the servlet has a get http request made to it
– doPost – is called when the servlet has a post http request made to it.

@WebServlet(description = “My First Servlet”, urlPatterns = { “/FirstServlet” })
This tell the JVm that the servlet is called “My First Servlet” and it can be found at the following URL. “/FirstServlet” (web context)

The params to doGet and doPost are…
– HttpServletRequest – contains anything that comes FROM the browser.
– HttpServletResponse – Contains the stuff the application needs to send back to the browser. Most common is to get the writer from HttpServletResponse for a PrintWriter that then writes data back to the browser.

response.setContentType(“text/html”) tells the browser to expect HTML.

You can then use the out.println statement to build a webpage using standard HTML tags.
e.g. out.println(“

Hello World!

“);
More complex servlets – Hello BLUE world
In the real world, your code will never be this simple, so let’s create a custom helper class to do some work in our new servlet.

In this, I’ve used a helper class to output the title and change the colour to blue. This gives an example of how to interface with objects within your servlet. I’ve created a servlet called TestServlet1 and a custom class called TestServlet1Utilities.

The TestServelet1Utilities class contains a single method that simple returns a string with the HTML doctype and head tags. See below.

	/*
	 * <pre>: Requires the title of the page
	 * <post>: Outputs the required html as a string that defined the title. 
	 */
	public static String getHeadWithTitle(String title){
	    return("<!DOCTYPE html>\n" +
	            "<html>\n" +
	            "<head><title>" + title + "</title></head>\n"); 
		
	}

Annotations VS web.xml
You can now create a servlet by just using an annotation.
e.g. @WebServlet(“/test”)
and this allow, assuming your class extends HttpServlet, is enough to let the web app know to serve your servlet from the specified path.
Prior to this, each servlet has to be specifically specified in the applications web.xml file.
Web.xml lives inside the web-inf directory of your application.
e.g. of a web.xml file defining a servlet:

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.informalsemantics.MyServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>

</web-app>    

While this is how you will see a lot of servlet code written, annotations is an easier and simpler approach.

Tutorial 5: Simple flow control

All programs require some for of flow control. Flow control is typically in the forms of either decision making (e.g. Do I need to run code block A or code block B) or loops (e.g. I have to run this block of code 10 times). In keeping with previous blog posts, all sample code is encompassed by jsp scriptlet tags so you can copy and past directly into your JSP pages for experimenting.

1. IF statements

If statements work by evaluating a given expression, then if true, running a certain code block, and if false skipping the code block and continuing.

1.a In this IF statement, we are going to check if 1 is equal to 1. If true, the code will output the word YES.

<%
if (1==1){
	out.println("YES");
}	
%>

YES

1.b In this IF statement, we are now going to check if 1 is equal to 2, which will return false and no text will be output.

<%
if (1==2){
	out.println("YES");
}
%>

1.c.In this IF statement, we are now going to check if 1 is equal to 2, however we will now return something if the expression evaluates to false and output the work NO.

<%
if (1==2){
	out.println("YES");
}
else
{
	out.println("NO");	
}
%>

NO

2 Nested IF statements

In this statement, we will check 1=1 and if true then check if 2=2. Only then will we output yes.

<%
if (1==1){
	if (2==2){
		out.println("YES");
	}
}	
%>

YES

3. IF statements checking both clauses are true

<%
if ((1==1)&&(2==2)){
		out.println("YES");
}
%>

YES

4. IF statements checking if either clauses is true

<%
if ((1==1)||(1==2)){
		out.println("YES");
}
%>

YES

5. WHILE loops

While loops work by running a particular code block while a certain clause is true. In this example, the counter variable is incremented by 1 and output with each run of the loop until the counter is less than or equal to 5 clause equates to false.

*The loop invariant is what causes the loop to exit. In this case, the loop will always exit as the counter will always count up to 6 and cause the clause to return false. If you don’t increment the variable, you will create an infinite loop that will eventually cause your application to run out of memory and crash.

<%
int counter = 0;
while (counter<=5){
	out.println(counter +"<br>");
	counter++;
}
%>

0
1
2
3
4
5

6. FOR loops

For loops work just like while loops, but have some nice clean syntax that is easier to code and helps prevent issues like infinite loops.

For loops are in the format of

for(initialisation; termination; increment){ statements };

When using this version of the for statement, keep in mind that:

  • The initialization expression initializes the loop; it’s executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.
<%
for (int i = 0; i<=5; i++){
	out.println(i +"<br>");
}
%>

0
1
2
3
4
5

Tutorial 4: Importing classes to use within your JSP pages

This tutorial shows how to use an external class within your JSP pages to perform complex logic.

Why import classes?
Many beginners, including myself, feel tempted to put all of our code within code blocks directly on the JSP page in scritplets. While this does work, soon your JSP files will become enormous and hard to manage. The beauty of Object Orientated languages is that you can modularise all of your code into objects and then import them into many different parts of your applications. There are a range of benefits to doing this, including limiting code reuse (you can just import and call the same object) and abstraction (you can use other peoples objects, and assuming they are well documented, you don’t necessarily have to understand the complex innerworkings of the object, but just what it requires in its methods.)

For this example, I have imported the java.util.Random class which is used to generate a random number. java.util.Random is part of the core java API and can be imported into any of your classes or JSP files where needed.

An import statement in JSP looks like this:

<%@ page import="java.util.Random" %>

1. Below is an example of using this class to create a single random number. Simply put this into your own JSP page and include the above import and you should also be able to output a random number.

<%
Random random = new Random();
int randomNumber = random.nextInt(100);
%>

One random number:<%= randomNumber %>

2. To make things a little more interesting, the below example generates 50 random numbers by using a for loop.

<%
Random random2 = new Random();

for(int i=0;i<50;i++){
	int randomNumbers = random2.nextInt(100);
	out.print(i+") "+randomNumbers+"<br>");
	
}
%>

Full source code is below to provide a little more detail on where to include the import statement in the context of a full JSP page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.Random" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test03</title>
</head>
<body>

1. below will generate ONE random number<br>


<%
Random random = new Random();
int randomNumber = random.nextInt(100);
%>

One random number:<%= randomNumber %>


<br><br>

2. below will generate FIFTY random numbers<br>

<%
Random random2 = new Random();

for(int i=0;i<50;i++){
	int randomNumbers = random2.nextInt(100);
	out.print(i+") "+randomNumbers+"<br>");
	
}
%>

</body>
</html>

Tutorial 3: JSP expressions and scriptlets

This tutorial will explain how to use both expressions and scriptlets to put dynamic content within your jsp pages.

Expressions:
Expressions provide the ability to include simple java within your page that is evaluated at run time with the result being output to the page. The character sequences <%= and %> to enclose Java expressions. Examples 1 and 2 from the source code below demonstrate this below.

1. Having an expression print the current date using java.util.Date()

Date:<%= new java.util.Date() %>2

2. Having an expression print “Hello World!” from a java String called helloWorld

<% String helloWorld = "Hello World!"; %>
<%= helloWorld %> <br>

Scriplets:
For more complicated java, scriptlets can be used. Scriplets provide more complicated blocks of java code to be inlucded within the page. Example 2 above shows a scriptlet created a local variable called helloWorld. Examples 4 and 5 demonstrate this further below.

3. Having an expression print true or false when comparing variables on the server side from a previous scriptlet code block.

<%
String result = "false";
int a = 1;
int b = 1; 
int c = 2;
if (a==b){
	result = "true";
}; 
%>

a(<%=a %>)==b(<%=b %>) = result<br>
result-> <%= result %> <br>

<% 
String result2 = "false";
if (a==c){
	result2 = "true";
}; %>

a(<%=a %>)==c(<%=c %>) = result2<br>
result2-> <%= result2 %> <br>

<br>

4. Using the out variable to have the scriplet generate HTML. (using out, the output will appear in the HTML page. In the below exampl, you should see “out – test” displayed in the html)

<% out.println("out - test"); %>

5. Using the System.out variable to have the scriptlet gerneate console outs. (using System.out, the output will appear within the eclipse console. In the below example, you should see “System.out – test appear in the eclipse console.” )

<% System.out.println("System.out - test"); %>

Eclipse “Failed to load the JNI shared library” error on open

When trying to open Eclipse today, I received the error:

Failed to load the JNI shared library "C:/JDK/bin/client/jvm.dll".

The cause of this error in my case was that when installing the JDK, I pointed the environment PATH variable in Windows to the 32 bit version instead of the 64bit version. As I was running 64bit Eclipse and 64bit Windows, the PATH variable needed to refer to the 64bit version or applications using this path to find the java executables would fail.

To solve this, I changed the java reference in my Windows PATH enviornment variable from:

C:\Program Files (x86)\Java\jdk1.7.0_10\bin

to:

C:\Program Files\Java\jdk1.7.0_13\bin

 

 

Tutorial 2: Creating your first Hello World eclipse project using JSP

Creating a new project in Eclipse

1. Run eclipse.exe from the installation folder.

2. Select a workspace. (if this is the first time running eclipse, just choose the default workspace which will create a folder called ‘workspace’ inside the user folder within windows.)

Select a workspace

3. Close the ‘Welcome’ page.

Welcome

(You can get this page back again by selecting help-> welcome)

3. Go to file -> new -> project

New Project Wizard

4. Select Web -> Dynamic Web Project.

New Project Wizard

5. Enter a project title. For my project, I’ve named it webTest01. Click next.

 New Project Wizard
6.  This page allows you to configure the structure of your project. For now, lets leave everything as default and just click next.
New Project Wizard
7. This page allows you to make more changes to the folder structure of your project. For now, lets leave everything as default and just click finish.

New Project Wizard

8. You should now see your newly created java project as webtest01 in the project explorer to the left of the screen.

Project Explorer

Now that we have created a new project, lets create our first JSP page.(see below)

Adding a new JSP file to an existing project

1. Go to file -> new -> other. Then select web -> JSP file

Creating a new JSP file

2. Once you have created your JSP file, add the text ‘Hello World’ into your project with h1 tags.

Hello World!

3. You have now created your first JSP file. To test this, we now need to install a server and run the project. (see below)

Adding a server and running your project

Setting up a server. Before being able to run your application and access it via a browser, you will first need to install a java web server. You will be prompted to do this the first time you try and run your project, so do this now.

1. Run -> Run.

Run On Server

As we have created a web project, but don’t currently have a web server set up, the above screen should appear.

2. Choose Tomcat v7.0 Server from within the Apache folder. (you won’t be able to choose the others as they do not support the latest version of Java JDK that you have installed. To use older/other servers, you will need to download and compile your project with an older version of the JDK.) Click next.

a2

3. Installing Tomcat.

a3

4. As we don’t have Apache Tomcat installed, click on the download and install… button and have eclipse do this automatically for you.

a4

5. Once complete, you should see your selected install location (in my case c:\tomcat\ in the Tomcat installation directory. Once tomcat is installed, click on the next button.

6. Make sure your project is on the configured side of the page. a5

7. Once your screen looks similar to above, click on the finish button. You will see a some red text appear as the server starts up. This is all good and is just INFO output on tomcat starting and is not typically errors.

8. Once the server has started, you can open a browser and navigate to your now running application.

http://localhost:8080/webtest01

a6

Your application will always run on localhost (the keyword for your local machine) and underneath the folder of your project name. The default port your application should run on is 8080.

Troubleshooting:

If your server doesn’t start or has errors:

–          Investigate if something else is currently using port 8080. In my case, I had another application using the port, which I configured to use a different port and restarted. After this my server worked correctly.

If you receive 404 page not found errors:

–          Java is case sensitive. Make sure /webTest01/ is typed in case sensitive as any variance of the case will cause the page to not load.

–         If you didn’t use index.jsp, you will need to type the full path to your jsp file.

E.g. http://localhost:8080/webtest01/myFile.jsp

Tutorial 1: Installing the JDK and an IDE

First things first, in order to do some java development, you will need to download some software. The main 2 items you will need to download and install are the Java JDK and an IDE.

The Java JDK

This is the programming libraries and underlying runtime/compiler you will need to code in java. Importantly, there are 2 different versions of java available from the download site which are the JDK and JRE. Be sure to download the JDK version, as you will not be able to actually compile your newly written java code with the JRE version

JDK = Java Developmenet Kit

JRE = Java Runtime Environment

Download link:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

The download you will be looking for will be something along the lines of “Java SE 7u13 JDK”. (7u13 was the latest version at the time of writing.)

Installation steps (Windows):

  1. Download the appropriate installer for your operator system from the link above. (For the purposes of this tutorial, I will give instructions for a Windows install)
  2. Once downloaded, simply run the installer and select all of the default options. (Important note: Oracle has now started bundling the Java installers and updaters with the Ask toolbar. Under no circumstances should you install the Ask toolbar as it is essentially malware. Shame on Oracle for bundling this with java)
  3. Set your environment variables. To do this, right click on “My Computer” and click on properties
  4. Click on “Advanced System Settings”.
  5. From the System properties window, select the ‘Advanced’ tab.
  6. Select the Environment Variables button.
  7. In the “System Variables” box, scroll down to PATH and select edit.
  8. At the FRONT of all of the existing variables, insert your java installation directory followed by a semi-colan. e.g.
    C:\Program Files (x86)\Java\jdk1.7.0_10\bin;
    UPDATE: for the 64bit version of Eclipse and Windows, you will need to point the PATH variable to: C:\Program Files\Java\jdk1.7.0_13\bin and not the 32bit version.
  9. Hit OK and close all of the open windows.

Verify your installation (Windows):

  1. Press the start button and type cmd and press enter
  2. From the command prompt, type
    java -version
    If you see a console output, the JRE has installed successfully.
    e.g.
    C:\>java -version
    java version “1.7.0_10”
    Java(TM) SE Runtime Environment (build 1.7.0_10-b18)
    Java HotSpot(TM) Client VM (build 23.6-b04, mixed mode, sharing)
  3. From the command prompt, type
    javac -version
    if you see a console output, the JDK has installed successfully.
    e.g.
    C:\>javac -version
    javac 1.7.0_10

 

An IDE

An IDE (Integrated Development Environment) is the tool you will be using to write code. While it is possible to write all of your java code in notepad, an IDE will provide a range of handy tools to assist you, such as syntax colouring and code autocomplete.

For this blog, I have chosen Eclipse as my IDE of choice as it is the one I am most familiar with.

Download link:
http://www.eclipse.org/downloads/

The download link you will be looking for will be something along the lines of “eclipse-jee-juno-SR1-win32-x86_64.zip” which was the latest x64 windows release at the time of writing.

Installation steps (Windows):

  1. Simple open the zip file and extract to contents of the zip into a new folder
    e.g. c:\eclipse\
  2. To run eclipse, simple execute c:\eclipse\eclipse.exe
  3. Create a shortcut for eclipse.exe on your desktop if needed.

 

Other possible IDEs include:

Netbeans:
http://netbeans.org/downloads/ 

JDeveloper:
http://www.oracle.com/technetwork/developer-tools/jdev/overview/index.html