Debugging Oracle PL/SQL

The purpose of this article is to cover techniques that are useful in debugging PL/SQL code, especially if you don’t have access to a debugger such as the one that comes packaged with SQL Developer (this has been the case for me a few times).

This article will cover the following topics:

  • Using DBMS_DBMS_OUTPUT + PUT_LINE.
  • Using SELECT FROM DUAL.
  • Using raise_application_error + SQLCODE + SQLERRM.
  • Using the EXEC command.

Setting up an Oracle 11G Sandbox Development Environment

This article will cover the following topics:

  • Downloading, Installing & Running the Oracle 11G Virtual Machine.
  • Starting up Oracle 11G and launching Enterprise Manager.
  • Logging in with SQL Developer 3.1 EA.
  • Working with the Demo SCHEMA.

Note:  I’m working on an IBM Thinkpad with Windows XP SP2 running at 1.6GHZ with 3.0GB of RAM. The VM is running on RedHat Linux.

Downloading, Installing & Running the Oracle 11G Virtual Machine.

Step 1: You will want to download Oracle VM VirtualBox from here (avaliable for Windows/Mac/Solaris/Linux): Oracle VM VirtualBox Download.

If you don’t have an Oracle account you will have to create one for free.

Once the download is complete continue with the installation process. I used the default installation options throughout the install. Once the instillation is complete run the application and you should see a screen similar to this:

Oracle VM VirtualBox Manager

Figure 1: Oracle VM VirtualBox Manager

Step 2: Next you will need to download the Virtual Image (Oracle Developer Day.ova) from here: Oracle Developer Day.ova

Step 3: Once the download is complete, we are ready to launch the VM.

  • Using VM VirtualBox import your VM: File -> Import Appliance to launch Appliance Import Wizard.
  • Click Choose… browse to the which contains Oracle Developer Days.ova (select the file) then click Next> to begin importing the virtual machine.

Step 4: Now we can launch the Oracle Dev Virtual Machine. Double-click the OTN Developer Days VM. Click OK to close the Virtualbox Information dialogs. The VM will launch and boot into RedHat Enterprise Linux 5 (this will take few minutes), once booted in you can login using the following credentials:
User Name: oracle
Password: oracle

Oracle VM Virtual Box Manager Running

Figure 2: Oracle VM VirtualBox Manager Running

Oracle Virtual Machine logged-in

Figure 3: Oracle Virtual Machine logged-in

Starting up Oracle 11G and launching Enterprise Manager.

Now that the Oracle VM is up and running we can start Oracle and login to the Enterprise Manager. In order to stat Oracle simply type “emctl start dbconsole” into the console which appeared on your desktop when you logged into GNOME and hit enter. This will launch Oracle Enterprise Manager 11G. It took a few minutes to bootup for me, but once it is booted up you can log-in by going to the folling URL in your VM’s web browser:

https://localhost.localdomain:1158/em/console/aboutApplication

User name: system
Password: oracle
Connect As: SYSDBA

Oracle Enterprise Manager 11G

Figure 4: Oracle Enterprise Manager 11G

Logging in with SQL Developer 3.1 EA

The VM comes with oracle SQL Developer EA (the icon is on the desktop). I’ve been using SQL Developer for a while now and find it just as powerful as TOAD or any of the other mainstream Database IDE’s.

You can launch SQL Developer and login to the System account, you should see Auto-Generated Local Connections in the Connections pane on the left. Expand the Auto-Generated Local Connections branch, this will allow you to login to the system-oracle account (password: oracle).

Once connected to system-oracle, right click on system-oracle and select Scheme Browser, this will allow you to select the Demo schema which I will be referring to in other articles related to Oracle and PL/SQL.

Oracle SQL Developer EA

Figure 5: Oracle SQL Developer EA

At this point you should have everything you need to start learning and experimenting with Oracle and PL/SQL in a consolidated Virtual Machine Environment!

IOC and Dependency Injection

Topics Covered:

  • What is Inversion of Control and Dependency Injection?
  • Lets write our own simple IOC container form scratch using Java and XML.
  • What are some of the practical uses of IOC and DI?
Figure 1: IOC and Dependency Injection

Figure 1: IOC and Dependency Injection

In figure 1 we have an arbitrary object called “AnObject” (item 1 in the image) and an arbitrary XML configuration defining our object attributes (item 2 in the image).  Inversion of Control describes an object creation flow/process in which objects are created and their attributes set or loaded dynamically at run-time via an IOC container. This is achieved using Dependency Injection (a design pattern).

There are three types of Dependency Injection:

  • Interface injection: Is used when an object is defined by an interface that it must implemented in order to inject dependencies at runtime.
  • Setter injection: Is used to refer to an object exposing a setter method(s) to inject dependencies at run-time.
  • Constructor injection: Generally the same concept as setter injection except we utilize the constructor instead (this is what we use in our example below).

The example below has been written using all three techniques (setter, constructor, and interface) so you can get a better idea of the differences.  The displayed and discussed code is the one which uses Constructor Injection, but all three versions are available for download at the end of the article.

For our example we will use Dependency Injection to inject object attributes defined in an XML configuration file (IOC-application-context.xml).

DEVHUB Example IOC Container using Java and XML

Figure 2: DEVHUB Example IOC Container using Java and XML

Figure 2 above contains the components of our simple IOC Container application. We have the container itself (DevHubContainer.java), our objects Band.java and BandMember.java, and the configuration file defining our object dependencies and attributes (IOC-application-context.xml).

IOC-application-context.xml

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<beans>
    <bean id="Robert" class="fm.devhub.objects.BandMember">
        <property name="name" value="Robert Plant" />
    </bean>
    <bean id="Jimmy" class="fm.devhub.objects.BandMember">
        <property name="name" value="Jimmy Page" />
    </bean>
    <bean id="John" class="fm.devhub.objects.BandMember">
        <property name="name" value="John Bonham" />
    </bean>
    <bean id="Jones" class="fm.devhub.objects.BandMember">
        <property name="name" value="John Paul Jones" />
    </bean>
    <bean id="Band" class="fm.devhub.objects.Band">
        <property name="name" value="Led Zeppelin" />
        <property name="label" value="Atlantic" />
        <property name="genere" value="Rock" />
        <property name="singer" bean-reference="Robert" ref-class="fm.devhub.objects.BandMember"/>
        <property name="bass" bean-reference="Jones" ref-class="fm.devhub.objects.BandMember" />
        <property name="guitarist" bean-reference="Jimmy" ref-class="fm.devhub.objects.BandMember"/>
        <property name="drummer" bean-reference="John" ref-class="fm.devhub.objects.BandMember"/>
    </bean>    
</beans>

Lines 2-13:
Here we define our simple Objects, we have members of a musical band belonging to the package fm.devhub.objects.BandMember, and one attribute defined which is the name of the band member.

Lines 14-22:
We define our band Object. Its basically is composed of some attributes defining the name of the band etc. But also notice it defines some Object references, band-members. So when we construct our Band object, the IOC Container will know to include Objecs of type BandMember as well.

BandMember.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package fm.devhub.objects;
import java.util.ArrayList;
/*
* This class resembles a Band Member Bean or POJO.
* Properties for our Band Member are injected using
* our IOC Container & Constructor Injection.
*/
public class BandMember {
    private String name;
    
    public BandMember(){
    }
    
    public BandMember ( ArrayList<String> name ){
        
        this.name = (String) name.get(0);
    }
    public String toString() {
        return "Member Name: " + name;
    }
}

Here we have a simple Object called BandMember, it has one attribute which defines the name of the band member. Because this example is using Constructor Injection, I didnt include any set or get methods. The object is constructed by our IOC container which injects the constructor value at runtime based on what is defined in IOC-application-context.xml.

Band.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package fm.devhub.objects;
import java.util.ArrayList;
/*
* This class resembles a Band Bean or POJO.
* Properties for our Band Member are injected using
* our IOC Containe and Constructor
* Injection.
*/
public class Band {
    private String name;
    private String label;
    private String genere;
    private BandMember singer;
    private BandMember bass;
    private BandMember guitarist;
    private BandMember drummer;
    public Band() {
    }
    public Band( ArrayList<Object> params ) {
        this.name = params.get(0).toString();
        this.label = params.get(1).toString();
        this.genere = params.get(2).toString();
        this.singer = (BandMember) params.get(3);
        this.bass = (BandMember) params.get(4);
        this.guitarist = (BandMember) params.get(5);
        this.drummer = (BandMember) params.get(6);
    }
    public String toString() {
        return "Name: " + name + "\nLabel: " + label + "\nGenre: " + genere + "\nSinger " + singer
                + "\nBass " + bass + "\nGuitarist " + guitarist + "\nDrummer "
                + drummer;
    }
}

Band is very similar to BandMember. The only difference is that there are more attributes being set. Once again our IOC Container will inject those values at runtime based on the relationships defined in IOC-application-context.xml.

DevHubContainer.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package fm.devhub.ioccontainer;
//Import the Java classes
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
//Import the Reflection classes
import java.lang.reflect.Constructor;
//Import the JDOM classes
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
/*
* This is our simple IOC Container, it parses
* IOC-application-context.xml and uses DI
* to create and assemble the beans.
*/
public class DevHubContainer {
    /**
  * Keeps track of all our bean nodes.
  */
    private List<Object> beanList;
    
    /**
  * Creates a new DevHubContainer that is configured by the specified XML
  * filename, we use JDOM SAX to traverse through our XML document.
  */
    public DevHubContainer(String application_context) {
        try {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(application_context);
            Element root = doc.getRootElement();
            this.beanList = root.getChildren("bean");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
  * Returns a configured bean based on the name for example: <bean id="Band"
  * class="fm.devhub.objects.Band"> has the name 'Band'
  */
    public Object getBean(String name) {
        // Iterate over our bean list, this was
        // populated during the instantiation of our
        // IOC container.
        for (Iterator i = this.beanList.iterator(); i.hasNext();) {
            Element bean = (Element) i.next();
            String id = bean.getAttributeValue("id");
            if (id.equals(name)) {
                try {
                    String className = bean.getAttributeValue("class");
                    List propertyList = bean.getChildren("property");
                    ArrayList elementValue = new ArrayList();
                    if (propertyList.size() > 0) {
                        for (Iterator it = propertyList.iterator(); it.hasNext();) {
                            Element propertyElement = (Element) it.next();
                            String propertyValue = propertyElement
                                    .getAttributeValue("value");
                            String propertyReference = null;
                            if (propertyValue == null) {
                                propertyReference = propertyElement.getAttributeValue("bean-reference");
                                Class refbeanClass = Class.forName( propertyElement.getAttributeValue("ref-class") );                                
                                Constructor[] refconstruct = refbeanClass.getConstructors();
                                ArrayList<String> propertyReferenceValue = new ArrayList();
                                propertyReferenceValue.add( propertyReference );
                                Object refbeanInstance = refconstruct[1].newInstance(propertyReferenceValue);
                                elementValue.add(refbeanInstance);
                            } else {
                                elementValue.add(propertyValue);
                            }
                        }
                    }
                    Class beanClass = Class.forName(className);
                    Constructor[] construct = beanClass.getConstructors();
                    Object beanInstance = construct[1].newInstance(elementValue);
                    return beanInstance;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        // If no matching objects are found, then return null
        return null;
    }
}

Lines 35-44:
We build a parser using JDOM and pass it our IOC-application-context xml.

Lines 60-90:
Here is the core of our IOC Container, we iterate over our application context, and create the objects defined. The objects are created very simply as follows:

     Class beanClass = Class.forName(className);
     Constructor[] construct = beanClass.getConstructors();
     Object beanInstance = construct[1].newInstance(elementValue);

elementValue is an ArrayList containing all of the values that need to be set in the objects constructor.

IOCContainerTest.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package fm.devhub.test;
import fm.devhub.ioccontainer.DevHubContainer;
import fm.devhub.objects.Band;
import fm.devhub.objects.BandMember;
public class IOCContainerTest {
    public static void main( String[] args ) {
       /*
       * We instantiate our IOC Container, and load our application-context
       * Then we use IOCContainer.getBean() to inject our Band and BandMember bean
       * configuration attributes.
       */
       DevHubContainer IOCContainer = new DevHubContainer( "C:\\IOC-application-context.xml" );
        
       BandMember Jimmy = ( BandMember )IOCContainer.getBean( "Jimmy" );
       BandMember Robert = ( BandMember )IOCContainer.getBean( "Robert" );
       BandMember John = ( BandMember )IOCContainer.getBean( "John" );
       BandMember Jones = ( BandMember )IOCContainer.getBean( "Jones" );
              
       System.out.println( "**** Test Band Member Beans****" );
       System.out.println( Jimmy.toString() );
       System.out.println( Robert.toString() );
       System.out.println( John.toString() );
       System.out.println( Jones.toString() );
                   
       Band LedZepplin = (Band) IOCContainer.getBean("Band");
       System.out.println( "**** Test Band Bean****" );
       System.out.println(LedZepplin);
      
    }
}

The test case simply tests all of the objects defined in IOC-application-context.xml. Our simplest object definitions are the BandMembers where we only a name parameter is set. Slightly more complex is the Band object which is composed of a few attributes and a reference to the BandMembers.

Below is the output that should be generated when you run the test case.

Test case output:

Member Name: Jimmy Page
Member Name: Robert Plant
Member Name: John Bonham
Member Name: John Paul Jones
**** Test Band Bean****
Name: Led Zeppelin
Label: Atlantic
Genre: Rock
Singer Member Name: Robert Plant
Bass Member Name: John Paul Jones
Guitarist Member Name: Jimmy Page
Drummer Member Name: John Bonham

Errors I ran into while writing the example:

java.lang.InstantiationException: fm.devhub.objects.BandMember
at java.lang.Class.newInstance0(Class.java:340)
at java.lang.Class.newInstance(Class.java:308)
at fm.devhub.objects.BandMember.main(BandMember.java:29)
Exception in thread "main" java.lang.NullPointerException
at fm.devhub.objects.BandMember.main(BandMember.java:30)

I ran into this error because i didn't use a default
constructor in my BandMember class.
The default constructor is automatically created
by java only when no other constructors
are present.

When is an IOC Container needed?

The answer to this question is going to vary depending on what you are trying to accomplish as well as your personal programming style and requirements.

Generally you should consider adding Dependency Injection and IOC Container functionality in the following scenarios:

  • You need to inject configuration data into an object (for example database connection configuration).
  • You need to inject different implementations of the same object.
  • You need to inject the same dependency into objects

Also, a very powerful side effect using an IOC Container and DI is that it caninherently simplify testing. Envision being able to mock up objects as needed for testing in your configuration (application context) file vs. having to connect to a database or call/stub a service. Also testing objects under multiple configurations is simple using DI and an IOC Container.

You do not need Dependency Injection in the following scenarios:

  • You will not need different configurations for an object.
  • You will not need different implementations for an object.

That said, IOC containers such as Spring IOC offer many useful features which are enabled by DI. After reading this article I would advise familiarizing yourself with Spring IOC (or any other main stream IOC framework) simply because it will give you better insight into best practices and industry standards for IOC implementations and testing approaches.

What are some popular IOC Containers?

  • Spring IOC
  • Pico Container

Download Example Source:
Click here to download example source.

Additional Reading:
Factory Pattern vs. Dependency Injection
Why do i need an IOC container as opposed to straightforward DI code
Martin Fowler: Injection

Working with Oracle PL/SQL – Cursors

Cursors

A Cursor in PL/SQL is used to return rows of data from an SQL query (a ResultSet). You can iterate over a Cursor in a PL/SQL using a LOOP or return a Cursor to a calling entity such as a Java application. There are two types of Cursors in PL/SQL. Implicit and Explicit, the differences are as follows:

Implicit Cursors
Implicit Cursors are indirectly created by Oracle when an INSERT, UPDATE, DELETE, or SELECT statement is executed. However, in the case of a SELECT statement implicit Cursors are only created when one row is returned, if more than one row is returned we must Explicitly declare a Cursor.

Explicit Cursors
Explicit Cursors are explicitly declared in PL/SQL code and they return multiple rows from a SELECT statement. Once these rows are returned, the can be itereated over one at time using a LOOP construct inside of a Stored Procedure, Package, Function, etc. Or they can be returned to a calling application or procedure.

Here is an example of an Explicit Cursor being who’s resulset is be iterated over with a loop construct:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- Declaring the a Cursor
CURSOR MY_CORSOR is
SELECT DISTINCT
userid, username, first_name, last_name
FROM T_USER WHERE
username like '%o%'
-- Looping through and executing when no records
-- are found
FOR MY_CURSOR_rec in MY_CORSOR
LOOP
EXIT WHEN MY_CORSOR%NOTFOUND;
-- Execute some logic here for example
IF MY_CURSOR_rec.username = 'mo' THEN
-- Do Something
END IF;
END LOOP;

If the Cursor above were executed. A ResultSet similar to what is displayed below (Figure 1) could be returned, and with that ResultSet come a number of attributes that can be used to interact with the Cursor.

Figure 1: Oracle PL/SQL Cursor Attributes

Figure 1: Oracle PL/SQL Cursor Attributes

Cursor State and Status

Oracle provides attributes for Cursors in order to evaluate their state. These attributes can be used in many situations, for example, if you look at Line 12 of the code above you will see we are using the %NOTFOUND attirbute. This is telling our loop to exit when no more rows are found in the Cursor.

Below are all of the attributes provided by ORACLE for identifying Cursor state:

%ISOPEN

  • Returns TRUE if the cursor is open, FALSE if the cursor is closed.
  • Look at line 09  (below) for an example of how %ISOPEN can be used.

%FOUND

  • Returns INVALID_CURSOR if cursor is declared, but not open; or if cursor has been closed.
  • Returns NULL if cursor is open, but fetch has not been executed.
  • Returns TRUE if a successful fetch has been executed.
  • Returns FALSE if no row was returned.
  • Look at line 15 (below) for an example of how %FOUND can be used.

%NOTFOUND

  • Returns INVALID_CURSOR if cursor is declared, but not open; or if cursor has been closed.
  • Return NULL if cursor is open, but fetch has not been executed.
  • Returns FALSE if a successful fetch has been executed.
  • Returns TRUE if no row was returned.
  • This was covered in line 12 of the example above.

%ROWCOUNT

  • Returns INVALID_CURSOR if cursor is declared, but not open; or if cursor has been closed.
  • Returns the number of rows fetched.
  • The ROWCOUNT attribute doesn’t give the real row count until you have iterated through the entire cursor. In other words, you shouldn’t rely on this attribute to tell you how many rows are in a cursor after it is opened.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
DECLARE
CURSOR USERS_CR IS
SELECT userid,username, useraddress
FROM user
WHERE username like '%Mo%';
USERS_CR_row USERS_CR%ROWTYPE;
BEGIN
IF (NOT USERS_CR%ISOPEN) THEN
OPEN USERS_CR;
END IF;
FETCH USERS_CR INTO USERS_CR_row;
WHILE (USERS_CR_row%FOUND)LOOP
--Execute some logic here....
FETCH USERS_CR INTO USERS_CR_row;
END LOOP;
IF (USERS_CR%ISOPEN)THEN
CLOSE USERS_CR;
END IF;
END;

Additional Reading:
ORACLE PL/SQL Documentation

Spring 3.0 Hibernate Template Utility Project Part 1

The following topics will be discussed in this article:

  • Downloading & Installing SpringSource Tool Suite (STS) Version: 2.8.1.
  • Setting up a Spring Hibernate Template Utility project.
  • Executing the packaged Hibernate Test Case.
  • A detailed walk through of the Spring Hibernate Template Utility project components.

Downloading & Installing SpringSource Tool Suite (STS) Version: 2.8.1.
SpringSource Tool Suite (STS) can be downloaded for free from the Spring website: Spring STS Download
Note: For this example I am downloading springsource-tool-suite-2.8.1.RELEASE-e3.7.1-win32-installer.exe installed at C:\springsource using java version “1.6.0_16″. I followed the default installation options until complete.

Once the installation is complete launch STS. Close the welcome screen, your setup should look very similar to this:

Spring Source Tools Home Screen

Figure1: Spring Source Tools Home Screen

Setting up the Spring Hibernate Template Utility Project
Next lets create Spring Hibernate Template Utility Project. In STS execute the following steps:

  1. Click on File –> New –> Spring Template Project. Select Simple Spring Hibernate Utility Project and click Next. (I set my project name to ‘SpringHibernateTemplateProject’ and defined the top level package as ‘fm.devhub.myspringutility’).
  2. You will see an alert notifying you to download the necessary resources, click Yes. What this will do is pull down the necessary libraries for your project to build correctly.

Note: Ensure your proxy settings are configured if you use one. Otherwise the files will not be able to download. You can set them by going to the Window menu –> Preferences –> General –> Network Connections.

Figure 2: New Spring 3.0 Hibernate Utility Project

Figure 2: New Spring 3.0 Hibernate Utility Project

Executing the packaged Hibernate Test Case.
The goal of the Hibernate Utility Template is to provide all the libraries you need to get started with using Hibernate and a simple test case with a few beans configured, this will allow you expand/extend Hibernate functionality with ease. Upon execution of the provided test case (OrderPersistenceTests) we can verify that the project as provided is working correctly.

To run the provided test case right click on ‘OrderPersistenceTests’ in the fm.devhub.myspringutility package of the src/test/java directory. Click Run as –> JUnit Test. You should see as JUnit Panel in the left indicating that all of the configured test executed without any errors.

Figure 3: Spring3.0 Hibernate Utility Test Success

Figure 3: Spring3.0 Hibernate Utility Test Success

A detailed walk through of the Spring Hibernate Template Utility project components.
In this section we will step through most of the files & code included in the Hibernate Utility Project. Although this should be considered skimming the surface of both Spring and Hibernate, you should come away with a basic understanding of how both frameworks work together. Also, Part 2 of this tutorial will cover both frameworks more deeply as well as incorporating the use of a non-embedded database (MySQL for example).

HibernateConfiguration.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package fm.devhub.myspringutility;
import java.util.Properties;
import javax.sql.DataSource;
import org.hibernate.dialect.H2Dialect;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.orm.hibernate3.HibernateTransactionManager;
import org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean;
@Configuration
public class HibernateConfiguration {
@Value("#{dataSource}")
private DataSource dataSource;
@Bean
public AnnotationSessionFactoryBean sessionFactoryBean() {
Properties props = new Properties();
props.put("hibernate.dialect", H2Dialect.class.getName());
props.put("hibernate.format_sql", "true");
AnnotationSessionFactoryBean bean = new AnnotationSessionFactoryBean();
bean.setAnnotatedClasses(new Class[]{Item.class, Order.class});
bean.setHibernateProperties(props);
bean.setDataSource(this.dataSource);
bean.setSchemaUpdate(true);
return bean;
}
@Bean
public HibernateTransactionManager transactionManager() {
return new HibernateTransactionManager( sessionFactoryBean().getObject() );
}
}

Line 14:
The @Configuration annotation denotes that the Bean is a configuration bean. Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions and in this case its being used to configure the Hibernate Datasource. In order to understand how annotations and IOC works, i would recommend reading this article (its also referenced in the additional reading section below): Martin Fowler: IOC & Dependency Injection

Line 17:
@Bean plays the same role as the Bean XML tag in a spring configuration file. We are simply moving the configuration to Java instead of XML. Whether or not you would like to use annotations is completely up to you.

Line 21:
AnnotationSessionFactoryBean is a factory that produces SessionFactory automatically (Spring handles this internally). So in essence the inclusion of AnnotationSessionFactoryBean ensures that the bean will receive an immutable instance of a Hibernate Session on which Hibernate operations can be executed.

Line 35:
The HibernateTransactionManager creates a Transaction Manager to manage database transactions for our bean. In Part 2 of this tutorial we will delve further into transaction managers and transaction intercepters.

So in summary the class above using Java annotations have substituted the need for the a Spring bean configuration in XML.

Item.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package fm.devhub.myspringutility;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
/**
* An item in an order
*/
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Order order;
private String product;
private double price;
private int quantity;
/**
* @return the order
*/
public Order getOrder() {
return order;
}
/**
* @return the product
*/
public String getProduct() {
return product;
}
/**
* @param product
* the product to set
*/
public void setProduct(String product) {
this.product = product;
}
/**
* @return the price
*/
public double getPrice() {
return price;
}
/**
* @param price
* the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* @return the quantity
*/
public int getQuantity() {
return quantity;
}
/**
* @param quantity
* the quantity to set
*/
public void setQuantity(int quantity) {
this.quantity = quantity;
}
/**
* @return the id
*/
public Long getId() {
return id;
}
}

Line 13:
This is the Entity Bean declaration. The concept in Spring for an Entity bean mirrors the J2EE concept of an Entity bean in the sense that Entity beans represent a business object in a persistent storage mechanism. Basically an entity defines a table in a relational database where each instance of the bean would correlate to a row in that table.

Lines 16-17:
The Bean ID is equivalent to a unique ID associated with a row in a database table. There are five different strategies for generating Bean id’s and they are TABLE, SEQUENCE, IDENTITY, AUTO, and NONE. In this case we are using AUTO.

They are defined as follows:

AUTO Indicates that the persistence provider should pick an appropriate strategy for the particular database.
IDENTITY Indicates that the persistence provider must assign primary keys for the entity using database identity column.
SEQUENCE Indicates that the persistence provider must assign primary keys for the entity using database sequence column.
TABLE Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.

Line 20:
A @ManyToOne annotation is used to denote that the relationship is associated with the Order object, indicating that an Order can contain many Products.

Order.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package fm.devhub.myspringutility;
import java.util.Collection;
import java.util.LinkedHashSet;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
/**
* An order.
*/
@Entity
@Table(name="T_ORDER")
public class Order {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String customer;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="ORDER_ID")
private Collection items = new LinkedHashSet();
/**
* @return the customer
*/
public String getCustomer() {
return customer;
}
/**
* @param customer the customer to set
*/
public void setCustomer(String customer) {
this.customer = customer;
}
/**
* @return the items
*/
public Collection getItems() {
return items;
}
/**
* @param items the items to set
*/
public void setItems(Collection items) {
this.items = items;
}
/**
* @return the id
*/
public Long getId() {
return id;
}
}

Line 20:
This is the Entity Bean declaration. The concept in Spring for an Entity bean mirrors the J2EE concept of an Entity bean in the sense that Entity beans represents a business object in a persistent storage mechanism. Basically an entity defines a table in a relational database where each instance of the bean would correlate to a row in that table.

Line 21:
@Table(name=”T_ORDER”) creates a table called T_ORDER which is mirrored to our Order.java class.

Lines 24-25:
As mentioned above, the Bean ID is equivalent to a unique ID associated with a row in a database table. There are five different strategies for generating Bean id’s and they are TABLE, SEQUENCE, IDENTITY, AUTO, and NONE. In this case we are using AUTO.

They are defined as follows:

AUTO Indicates that the persistence provider should pick an appropriate strategy for the particular database.
IDENTITY Indicates that the persistence provider must assign primary keys for the entity using database identity column.
SEQUENCE Indicates that the persistence provider must assign primary keys for the entity using database sequence column.
TABLE Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness.

Lines 30-31:
The cascade option is used to cascade the required operations to the associated entity. If the cascade option is set to CascadeType.ALL then all the operations will be cascaded. For this example we have CascadeType.ALL on the ‘private Collection items = new LinkedHashSet();’ object in association with line 31 “@JoinColumn(name=”ORDER_ID”)”. In other words of an Item is created, then an Order will be created as well.

Specifically Cascade.ALL does the following:

CascadeType.PERSIST: cascades the persist (create) operation to associated entities persist() is called or if the entity is managed
CascadeType.MERGE: cascades the merge operation to associated entities if merge() is called or if the entity is managed
CascadeType.REMOVE: cascades the remove operation to associated entities if delete() is called
CascadeType.REFRESH: cascades the refresh operation to associated entities if refresh() is called
CascadeType.DETACH: cascades the detach operation to associated entities if detach() is called

CascadeType.ALL: all of the above

You can find more information on this in the Hibernate Reference Documentation: Hibernate Reference Documentation

OrderPersistenceTests.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package fm.devhub.myspringutility;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class OrderPersistenceTests {
@Autowired
private SessionFactory sessionFactory;
@Test
@Transactional
public void testSaveOrderWithItems() throws Exception {
Session session = sessionFactory.getCurrentSession();
Order order = new Order();
order.getItems().add(new Item());
session.save(order);
session.flush();
assertNotNull(order.getId());
}
@Test
@Transactional
public void testSaveAndGet() throws Exception {
Session session = sessionFactory.getCurrentSession();
Order order = new Order();
order.getItems().add(new Item());
session.save(order);
session.flush();
// Otherwise the query returns the existing order (and we didn't set the
// parent in the item)...
session.clear();
Order other = (Order) session.get(Order.class, order.getId());
assertEquals(1, other.getItems().size());
assertEquals(other, other.getItems().iterator().next().getOrder());
}
@Test
@Transactional
public void testSaveAndFind() throws Exception {
Session session = sessionFactory.getCurrentSession();
Order order = new Order();
Item item = new Item();
item.setProduct("foo");
order.getItems().add(item);
session.save(order);
session.flush();
// Otherwise the query returns the existing order (and we didn't set the
// parent in the item)...
session.clear();
Order other = (Order) session
.createQuery( "select o from Order o join o.items i where i.product=:product")
.setString("product", "foo").uniqueResult();
assertEquals(1, other.getItems().size());
assertEquals(other, other.getItems().iterator().next().getOrder());
}
}

Lines 15-16:
These annotations load the default configuration context for Spring as well as the ‘SpringJUnit4ClassRunner.class’ for JUnit testing. What SpringJUnit4ClassRunner.class does is provide JUnit4.4 functionality alongside with Spring TestContext framework functionality. If you notice on Line 23 for example, we are using the @Transactional annotation which is apart of the TestContext framework. I would recommend reading this article if you are not familiar with Springs TestContext: Spring 3.0.5 TestContext Framework

Lines 19-20:
@Autowired in this case is basically ensuring that the Hibernate SessionFactory is injected into the Bean prior to the execution of any other Bean configuration items. @AutoWired ensures that fields are injected right after construction of a bean, before any config methods are invoked. In this case we are applying to the SessionFactory which makes sense as none of the test cases can execute without a Hibernate Session available.

Lines 22-23:
@Test is a JUnit annotation made accessible to us by the SpringJunit4ClassRunner which was loaded on line 16. The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. @Transactional enables transactions for the particular method.

Lines 25-30:
Here we are executing a basic test case asserting that an Order ID is returned after an order has been added to the database.

Additional Reading:
Martin Fowler: IOC & Dependency Injection
Spring 3.0.5 TestContext Framework

Thanks for reading! If you find any technical inaccuracies or deficiencies in this article, please make devhub.fm aware by posting a comment. Any feedback is welcome & encouraged.

Spring 3.0 MVC Template Introduction

The following topics will be discussed in this article:

  • Downloading & Installing SpringSource Tool Suite (STS) Version: 2.8.1.
  • Setting up a Spring MVC Template project.
  • Deploying the packaged Hello World application.
  • A walk through of all Spring MVC Template components.

Downloading & Installing SpringSource Tool Suite (STS) Version: 2.8.1.
SpringSource Tool Suite (STS) can be downloaded for free from the Spring website: Spring STS Download
Note: For this example I am downloading springsource-tool-suite-2.8.1.RELEASE-e3.7.1-win32-installer.exe installed at C:\springsource using java version “1.6.0_16″. I followed the default installation options until complete.

Once the installation is complete launch STS. Close the welcome screen, your setup should look very similar to this:

Spring Source Tools Home Screen

Figure1: Spring Source Tools Home Screen

Setting up a Spring MVC Template project.
Next lets create an MVC Template project. In STS execute the following steps:

  1. Click on File –> New –> Spring Template Project. Select Spring MVC Project and click Next. (I set my project name to ‘SpringMVCTemplateProject’ and defined the top level package as ‘fm.devhub.myspringmvc’).
  2. You will see an alert notifying you to download the necessary resources, click Yes. What this will do is pull down the necessary libraries for your project to build correctly.

Note: Ensure your proxy settings are configured if you use one. Otherwise the files will not be able to download. You can set them by going to the Window menu –> Preferences –> General –> Network Connections.

STS New MVC Template Project

Figure 2: STS New MVC Template Project

Deploying the packaged Hello World application.
Next lets deploy and launch the application.

  1. Right click on the project and select Run As –> Run on Server.For this tutorial we are going to use VMware vFabric tc Server Developer Edition v2.6.
  2. Select the server and click Next. This screen indicates that the server is selected and that your project is configured to be deployed. Click Finish.
Run On Server

Figure 3: Run On Server

The first attempt at starting the server yielded the following error for me, if you didn’t receive any errors then skip over this section.

http://localhost:8080/SpringMVCTemplateProject/
HTTP Status 404 - 

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

type Status report

message 

description The requested resource () is not available.

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

VMware vFabric tc Runtime 2.6.1.RELEASE/7.0.20.B.RELEASE

WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/SpringMVCTemplateProject/] in DispatcherServlet with name appServlet'

In order to fix the error above right click on the webapp directory in your project explorer and select Build Path –> Click Use as Source Folder. What this does is add the webapp directory to the build path which ensures our webapp files are included in the deployment. I haven’t had much time to find out exactly why this problem is occurring out of the box, so if you know the root cause please leave a comment.

Once the error is resolved repeat the steps above to deploy the application (if you didn’t encounter any errors then ignore this step). You should see the following screen indicating that everything is working correctly:

Successful Spring MVC Template Deploy

Figure 4: Successful Spring MVC Template Deploy

Now that the application is up and running we can proceed to the next section in this tutorial.

A walk through of all Spring MVC Template components.
In this section we will dissect most of the files provided in the Spring MVC Template project.

STS Spring 3.0 MVC Template Project Explorer

Figure 5: STS Spring 3.0 MVC Template Project Explorer

In order to see the package explorer (as displayed in the picture above) in your STS IDE, you will need to switch the perspective from Java EE to Java.

HomeController.java

Java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package fm.devhub.myspringmvc;
+import java.text.DateFormat;
/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {
    private static final Logger logger =
LoggerFactory.getLogger(HomeController.class);
    /**
  * Simply selects the home view to
* render by returning its name.
  */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! the
client locale is "+ locale.toString());
        Date date = new Date();
        DateFormat dateFormat =
DateFormat.getDateTimeInstance(DateFormat.LONG,
DateFormat.LONG, locale);
        String formattedDate = dateFormat.format(date);
        model.addAttribute("serverTime", formattedDate );
        return "home";
    }
}

Line 8:
The @Controller annotation defines the class as a controller. Controller classes in Spring are used to prepare the model that will be mapped to a view for a particular resource.

In the code above we have a Model object (line 17) called model, and we are adding an attribute to it called ‘serverTime’ (line 25). What this does is pass the model to our ‘home’ View which is our home.jsp, and if you look at the home.jsp code, you will see that we are printing the Model attribute ‘serverTime’.

Line 16:
@RequestMapping is used to map an HTTP Request to a specific resource. In this case we are mapping http://localhost:8080/SpringMVCTemplateProject/ as denoted by value = “/” to our HomeController.java’s home method, which returns control to home.jsp.
Therefore any request comming in via localhost:8080/SpringMVCTemplateProject/ will get returned to home.jsp when line 27 is reached.

Method = RequestMethod.GET is specifying the HTTP method ‘GET’ vs. ‘POST’ for example.
Note: To understand HTTP Methods better you can read the post “HTTP Request/Response” under Web Development Basics.

Line 17:
public String home(Locale locale, Model model) is the a method that gets called when this request comes in:
@RequestMapping(value = “/”, method = RequestMethod.GET).

The name itself ‘home’ is an arbitrary name that the developers who created the Spring MVC Template chose to use. If you change the name to homepage for example, the application will still deploy correctly. Just as the method name ‘home’ is arbitrary, as are the arguments being passed “Locale local, Model model” we could choose to pass additional arguments if we wanted.

Keep in mind that the Spring MVC Template is setup to be as simple and re-usable/extendable as possible, hence the reason why its using only the most basic of Spring MVC’s features.

Line 25:
model.addAttribute(“serverTime”, formattedDate ) is basically setting an attribute that will be accessible to our home.jsp (our view). And that attribute is ‘serverTime’.
If you look at home.jsp line 12 you can clearly see the link:

The time on the server is ${serverTime}.

Line 27:
Return ‘home’ is basically returning to our home.jsp view. If you were to modify HomeController.java to return “home1″ for example, you will get an error saying that Spring cannot locate the resource. Likewise if you change the name of the file to home1.jsp without updating the HomeController, you will receive an error.

servlet-context.xml

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- DispatcherServlet Context: defines this servlet's
request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
    <!-- Handles HTTP GET requests for /resources/** by
efficiently serving up static resources in
the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
    <!-- Resolves views selected for rendering by @Controllers
to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean
class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="fm.devhub.myspringmvc" />
</beans:beans>

Lines 2-9:
Defining the namespace and schema for the Spring servlet context file version 3.0.

Line 10:

Line 13:
Enables Spring 3.0 MVC annotations, we have used a few of them already such as @Controller and @RequestMapping. Some others include @SessionAttributes, @ModelAttribute and @InitBinder.

Line 14-16
Here mvc:resources tag is being declared although it isn’t currently being used. What the resources tag does is declare a ‘resource’ directory where you would want your publicly accessible static files to reside. An example would be JavaScript files, images, etc. One of the benefits of utilizing this tag is that you can specify browser caching settings in order to reduce the number of requests for resources being sent to your server.

If you look at your Package Explorer, you can see Spring MVC Template has this folder created under webapp –> resources.

Line 19:
This is the declaration of a ViewResovler, this is basically the Spring component that defines the rules of how to match or resolve an HTTP Request with a View. so if we look at the code we can see that we are using the “InternthealResourceViewResolver”
we are defining the prefix as “/WEB-INF/views/”
and the suffix as ‘.jsp’
So, when HellowController.java returns ‘home’, the ViewResolver resolves the view by saying you want to look in “/WEB-INF/views/” and the file type is ‘.jsp’ so append ‘.jsp’ to the name. In term we end up with path and name of the file ‘View’ that we want which ends up being ‘/WEB-INF/views/home.jsp’

Keep in mind that there are many different types of View Resolvers, such as VelocityViewResolver, UrlBasedViewResolver, ResourceBundleViewResolvIemrplementation and XmlViewResolver. The MVC Spring 3.0 documentation defines InternthealResourceViewResolver as: A “Convenient subclass of UrlBasedViewResolver that supports InternalResourceView (in effect, Servlets and JSPs) and subclasses such as JstlView and TilesView. You can specify the view class for all views generated by this resolver by using setViewClass(..).”

Line 20:
The prefix is basically where to locate the files

Line 21:
The suffix is basically the file type, in our case a .jsp.

Line 23:
component-scan is basically enabling a feature in Spring 3.0 which will automatically search for annotated classes and register their BeanDefinitions to the Application Context. This is simply an alternative to explicitly declaring the configuration in the application context xml.

root-context.xml

XHTML
1
2
3
4
5
6
7
8
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!-- Root Context: defines shared resources visible to all other web components -->
        
</beans>

Lines 2-4:
Defines the namespace and schema for spring beans version 3.0.
Line 6:
Here we would have all shared resources for our application defined. For example a hibernate connection, or global bean values which need to be declared at startup.

home.jsp

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page session="false" %>
<html>
<head>
    <title>Home</title>
</head>
<body>
<h1>
    Hello world!
</h1>
<P> The time on the server is ${serverTime}. </P>
</body>
</html>

Line 1:
Is the declaration of our JSP taglibrary. Specifically JSTL1. I recommend reading more about JSTL if you have the time.

Lines 3-11:
The body of our home.jsp.

Line 12:
Here is where we use the Model attribute (serverTime) which was added in HomeController.java and display it to the user.

web.xml

XHTML
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Processes application requests -->
    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
        
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Lines 2-4:
Defines the namespace and schema for web-app version 2.5.

Lines 7-10:
Defines the ‘contextConfigLocation’. root-context.xml can be used to define Spring beans which are global to all Servlets. This could be useful for a database configuration for example.

Lines 13-15:
Adds a global context listener Spring. It basically can be viewed as initializing the Spring framework upon our web application start-up.

Lines 18-26:
A very important component of spring is the DispatcherServlet (look at Figure 6). The DispatcherServlet basically manages the request/response workflow between our controllers and views. It is a core component of how Spring MVC works.

Lines 28-31:
Adds our application servlet arbitrarily named appServlet.

Below is a visual representation of the application flow for our Spring 3.0 MVC Template hello world application.

Figure 6: Spring3.0 MVC Template Request/Response Flow

Figure 6: Spring3.0 MVC Template Request/Response Flow

Thanks for reading! If you find any technical inaccuracies or deficiencies in this article, please make devhub.fm aware by posting a comment. Any feedback is welcome & encouraged.

HTTP Request/Response Basics

The following introductory topics will be discussed in this article:

  • The life-cycle of an HTTP request & response.
  • Anatomy of an HTTP request & response.
  • HTTP Methods & best practices.
Figure 1: HTTP Request/Response

Figure 1: HTTP Request/Response

The life-cycle of an HTTP request commonly looks like this:

  1. A user visits the URL of a website.
  2. This creates a request which is routed to a web server via the internet (a network of DNS’s, routers and switches) over HTTP (Hypertext Transfer Protocol).
  3. The web server receives the HTTP request and responds to the user with the web page (or content) which was requested.

Every time you click on a link and visit a web page, behind the scenes you are making a request, and in turn receiving a response from a web server. Note that HTTP requests can be made via many channels, not just web browsers. For example, an HTTP request could be made using TELNET, or a client written in JAVA or C# etc.

To see an example of what an HTTP request and response looks like do the following:

  1. Go to the website http://web-sniffer.net/
  2. Type in www.google.com (or any website you wish) in the “HTTP(S)-URL:” input field. When you click on “Submit” you will see the HTTP request and response data for www.google.com.

The anatomy of an HTTP request:
As a web developer, an important area to understand is the method portion of an HTTP request. The method tells the web server what kind of request is being performed on a URI.
So if you type in the URL www.google.com/finance (for example). You are requesting the /finance URI. Within the /finance URI the HTTP request has to define an HTTP method.

The method portion of an HTTP request contains the following definition options:

       Method         = "OPTIONS"
                      | "GET"
                      | "HEAD"
                      | "POST"
                      | "PUT"
                      | "DELETE"
                      | "TRACE"
                      | "CONNECT"
                      | extension-method
       extension-method = token

OPTIONS
Options is useful for finding out which HTTP methods are accessible by a client. Depending on how the web server you are trying to connect to is configured, the administrator may only have the POST and GET HTTP methods accessible. While other HTTP methods such as DELETE, TRACE, etc are disabled.

GET
A GET request retrieves data from a web server by specifying parameters in the URL portion of the request. If you examine the example HTTP request below, we are asking for index.html, and passing the parameter report_id.

	GET /index.html?report_id=34543222 HTTP/1.1
	Host: www.awebsite.com
	User-Agent: Safari/4.0

Examples of when to use GET:

  1. You are accessing a URL purely for the sake of viewing data. You could think of it as using an SQL SELECT statement. You are asking for data from the web server without the intent of updating any data.
  2. You need a URL to be ‘bookmarkable’. Basically HTTP GET is considered to be repeatable, which allows requests to be retried safely and responses to be cached.
  3. You don’t mind the request being repeated. For example a user visiting the same URL more than once.

Examples of when not to use GET:

  1. You are passing sensitive data such as usernames, passwords, social security numbers, etc.
  2. You are sending large amounts of data. Although there isn’t a character limit defined in the HTTP specification for the length of a URL, IE 4 for example only supports a max URL length of ~2000 characters using a GET request.
  3. You need to update something on a server, for example submitting a form which will update a users address or shopping cart.

POST
A POST HTTP request utilizes a message body to send data to a web server. If you examine the example HTTP POST request below, you will see that we are passing a POST HTTP request with the message body of ‘userid=mo&password=mypassw’ to login.jsp (login.jsp would be an application that the web server forwards requests to).
Examples of when to use POST:

  1. You have a large amount of data to send to a web server (the size of data would exceed URL limits of the GET method).
  2. You are sending sensitive data such as uesrnames, passwords, social security numbers etc.
  3. You are altering the state of data in a web application. For example, a shopping cart keeping track of items which you are purchasing.

Examples of when not to use POST:

  1. The URL that you are passing has a requirement of being ‘bookmarkable’. If the state of the URL changes, then the user will not be able to retrieve, or view the data it it’s former state.
  2. Your request needs to be idempotent. Note that POST requests can be idempotent, however it’s better practice to use PUT (if this HTTP request method is supported by your web server and client)
	POST /login.jsp HTTP/1.1
	Host: www.awebsite.com
	User-Agent: Safari/4.0
	Content-Length: 27
	Content-Type: application/x-www-form-urlencoded

	userid=mo&password=mypassw

PUT
PUT similar to POST utilizes a message body to transfer data. However, there are some fundamental differences between the two. Firstly PUT is considered to be idempotent, secondly a PUT’s action is always defined for a specific URI, finally a PUT is for loading the data for that resource. In other words you should know the exact location of where the data you are sending will be retrieved later.
Example of when to use PUT:

  1. Put is idempotent, so basically if you need to accommodate for a scenario where a request is submitted multiple times but the result needs to be identical for each submission; use PUT. This could be useful for creating a new user for instance. If you send a PUT request to create a user Joe Smith multiple times, the last request should have the same results as if it were sent first.
  2. You have a specific URI which you are sending data to. For example:
POST URI:

http://hostname.com/users/new

PUT URI:

http://hostname.com/users/joesmith

Example of when not to use PUT:

  1. PUT should not be used for non idempotent requests (if the state of the resource is likely to change each time a request is sent).
  2. It’s good to keep in mind that in the case of html forms, most browsers do not support the PUT/DELETE methods. It is expected that POST/GET are used. Some Restful frameworks such as Ruby on Rails for example requires the use of PUT/DELETE, however these HTTP Methods are simply tunneled through the HTTP POST Method.
	PUT /somedatabase/some_doc_id HTTP/1.1
	Content-Length: 240
	Content-Type: application/json

	{
	  "Subject":"Resume",
	  "Author":"Mo",
	  "Body":"Find my resume attached"
	}

HEAD
The HTTP HEAD Method is used to retrieve information about a URL from a web server. So for example if you sent a HEAD request, you would receive a response from the web server containing the same information as you would with an HTTP POST excluding the body data. Here is an example:

	HEAD /de HTTP/1.1[CRLF]
	Host: www.google.com[CRLF]
	Connection: close[CRLF]
	User-Agent: Web-sniffer/1.0.37 [CRLF]
	Accept-Encoding: gzip[CRLF]
	Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]
	Cache-Control: no-cache[CRLF]
	Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF]
	Referer: http://web-sniffer.net/[CRLF]

DELETE
The HTTP DELETE method can be used to delete a resource from a server. Generally it is used in two scenarios. Fist scenario is if you are following RESTful standards in developing your web application. Secondly it can be used when DELETE is enabled on a web-server and you would like to follow the HTTP standard for deleting a resource. It’s important to note however that you can use HTTP POST to handle an HTTP DELETE action also, the decision is narrowed down to the options described above.

TRACE
If you attempt to execute an HTTP TRACE method on most web-servers you will likely see this message:

	Status: HTTP/1.1 501 Not Implemented

HTTP TRACE is used to eacho the contents of an HTTP Request back to the requester (which can be useful for debugging). This however may pose a security threat because malicious code can abuse HTTP TRACE functionality to gain access to information in HTTP headers such as cookies and authentication data, if an HTTP TRACE request is sent the original request data will be returned in addition to any user specific data. An example HTTP TRACE response can look like this:

	TRACE / HTTP/1.1
	Host: www.google.com

	HTTP/1.1 200 OK
	Server: Microsoft-IIS/5.0
	Date: Tue, 31 Oct 2012 03:01:44 GMT
	Connection: close
	Content-Type: message/http
	Content-Length: 39

	TRACE / HTTP/1.1
	Host: www.google.com

CONNECT
HTTP CONNECT can be used to establish a network connection to a web server over HTTP. It’s primarily used in cases where a secure/encrypted HTTP connection (tunnel) needs to be established between a client and a web server such as an SSL connection.
Simple HTTP tunnels are an unencrypted connection through an HTTP proxy to an arbitrary destination. The tunnel takes advantage of the HTTP CONNECT method normally used for HTTPS (secure web traffic) to connect to the destination server. A typical HTTPS connection through a proxy should look like:

	CONNECT remote-server:443 HTTP/1.0
	User-Agent: Mozilla/4.0 (compatible; MSIE 6.0;..
	Host: remote-server
	Content-Length: 0
	Proxy-Connection: Keep-Alive
	Pragma: no-cach

HTTP Response Codes
Whenever a request is made to an HTTP server, a response code is sent back to the client accompanying the requested data. It’s important to understand what these response codes are as they will be useful for managing errors in your web applications. A list of HTTP response codes and their meanings can be found here:
HTTP Response Codes

Additional Reading:
HTTP 1.1 specification
PUT vs. POST
HTTP TRACE Vulnerability Blog Post
Restful HTTP PUT/POST Article
HTTP CONNECT Security White Paper
TRA3EQ3SV99B

Thanks for reading! If you find any technical inaccuracies or deficiencies in this article, please make devhub.fm aware by posting a comment. Any feedback is welcome & encouraged.