19 January 2010

JEE6 with Netbeans 6.8 and Glassfish 3

I've been using JEE6 with Netbeans and Glassfish for a while and decided I should update my tutorial Creating a JEE application in Netbeans 6.5 to JEE6.

I'm following a similar loose format and creating a skeleton of an application to show case the various tiers (ORM, business objects and web) and give you an overview of what is possible.

I also want to demonstrate how easy developing EJB applications has become since the spec came out. I wouldn't be developing with it if it wasn't.

The musical background for today comes from The Willing Mind as a shameless plug ;). Find our music here: The Willing Mind on last.fm, The Willing Mind on Facebook

OK, I've got Netbeans 6.8 loaded, this is what I am running on:

Product Version: NetBeans IDE 6.8 (Build 200912041610)
Java: 1.6.0_16; Java HotSpot(TM) 64-Bit Server VM 14.2-b01
System: Linux version 2.6.28-17-generic running on amd64; UTF-8; en_ZA (nb)

I'm in my testing project group and turning the music on (Charls Riff).

15:00 - Create new Project

File -> New Project -> Java EE -> Enterprise Application -> 'Next'

Project Name: BooksDemo

'Next' again and keep the defaults (Glassfish v3 Domain, Java EE 6 etc)


Three projects will be created for you, the EJB project, the Web Project and the EE project references to EJB and Web project. In my case they were called BooksDemo, BooksDemo-ejb and BooksDemo-war

15:04 - Create the Datastore

The Database

In your 'Services' tag (Ctrl+5) under 'Databases' select Java DB, right click and 'Create Database':

Database Name: booksdemo
User Name: booksdemo
Password: booksdemo

'OK' and the new Java DB database will be created and start the server if not started.

Now the Persistance Unit

Back to your 'Projects' window (Ctrl+1), select the EJB project (BooksDemo-ejb), right click -> New -> Other -> Persistence -> Persistence Unit -> 'Next'

For the Data Source, in the drop down select 'New Data Source..'

JNDI Name: jdbc/booksdemo
Database Connection: <select the database you created earlier>


Leave everything else as default and press 'Finish'.

The Entity class

In the projects window right click on the EJB project again and select 'New' -> 'Other' -> Persistence -> Entity Class -> 'Next'

Class Name: Book
Package: com.gamatam.tutorial.book.model


Now add a field to the Book class for the title:

Alt+Insert -> Add Property

Name: title


Thats our data layer finished! (listening to 'Lick' by 'The Willing Mind' is it wrong to love your own music so much?)

15:23 - Create the Session Bean

In the projects window, select the EJB project and right click again -> New -> Other -> Java EE -> Session Bean -> 'Next'

EJB Name: BookService
Package: com.gamatam.tutorial.book.service


In the BookService class, right click -> Persistence -> User Entity Manager, this will add an instance variable for the entity manager.

Now we need the business methods, really easy: Alt+Insert -> Add Business Method

Name: getAll
Return Type: List<Book>


Alt+Insert -> Add Business Method:

Name: add
(Parameters) 'Add'
Name: book
Type: Book


Now to get rid of the squigly lines, or to fix the imports: Ctrl+Shift+i

Next we need to flesh out the methods a bit.

public List<Book> getAll() {
return em.createQuery("select b from Book b").getResultList();

public void add(Book book) {

OK, that was the business tier, please note there are no longer any interfaces created for the SessionBean!

15:36 - The Web

hmm, thinks a bit as to how to go forward this time..

Lets run the application here and do a sanity check: right click on the BooksDemo main project (probably highlighted) and select 'Run'.

32 seconds to build, browser coming up and we have 'Hello World!' staring at us, yay. We are looking at the default index.jsp page that was created by Netbeans when we created the project, there is no connection to the datastore or enterprise bean at this stage, but the database should have bean created.

Go to the Services window (Ctrl+5), under Databases select the database that we created at the begining of the tutorial, right click and 'Connect', then click to explore the node and select 'BOOKSDEMO', open it and open the 'Table' node.

You will see that the 'BOOK' table has been created for you and is made up of an 'ID' column and a 'TITLE' column to mach the properties in the Book entity class.

15:46 - Add Book Servlet

Back to the project window (Ctrl+1), select the war project (BooksDemo-war), right click -> New -> Other -> Web -> Servlet -> Next

Class Name: BookAdd
Package: com.gamatam.tutorial.book.web.action
'Next' and 'Finish'

Note the new 'Webservlet' attribute on the class, this means no more editing of the web.xml for serlvets!

Alt+Insert -> Call Enterprise Bean -> select the EJB project (BooksDemo-ejb) and select the BookService -> 'OK'

Remove everything from the 'processRequest' method and we will get the book name as a paramter, create a Book object and persist it using the SessionBean:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String title = request.getParameter("title");
Book book = new Book();

Once you save the servlet, Netbeans should deploy it, so you can test it by trying the following URL:


You will not see much as the servlet redirects back to the index page, but lets look in the database:

In the Services window (Ctrl+5), right click on the BOOK table and select 'View Data' - you will see a new row inserted with the title 'test'.

16:00 - Custom Tag

Now we will create a custom tag to view all the books:

Back to the project window (Ctrl+1) and right click on the war project -> New- > Other -> Web -> Tag Library Descriptor -> 'Next'

TLD Name: books


right click on the war project -> New- > Other -> Web -> Tag Handler -> 'Next'

Class Name: BookTag
Package: com.gamatam.tutorial.book.web.tag
TLD File: WEB-INF/tlds/books.tld
Tag Name: book


Next we add a reference to the BookService EJB and modify the doTag method so that loops through all the books:

public class BookTag extends SimpleTagSupport {
Bookservice bookservice;

public void doTag() throws JspException, IOException {
List<Book> books = bookservice.getAll();
for(Book book: books){
getJspContext().setAttribute("book", book);

Next, in the index.jsp page we need to first declare the tag library and then use it:

Open the index.jsp page (war project, Web Pages, index.jsp) and add the following:

<%@taglib uri="/WEB-INF/tlds/books" prefix="b"%>

And in the page body:


If you reload the page in your browser, you will see a list of books added to the datastore.

And lastly, I will add a simple form for adding a book:

<form action="BookAdd"><input name="title"><input type="submit"></form>

And at 16:17, thats a wrap!

I'll leave it as an exercise for you to 'dolly' it up.


Lincoln Baxter said...

I'm wondering why you chose to demonstrate JSP here. JSF2.0 is the standard web-framework in JEE6, officially deprecating JSP.

You would need much less work in the UI display layer if using JSF.

Learn more about JSF2 at www.javaserverfaces.org

Brian Silberbauer said...


I prefer JSP (without scripting) as the most agile and expendable way of developing a web front end.

I've alway liked the idea of JSF and have used it in projects, but haven't found it to be that useful in practise.

Could you add your source for the comment that JSP has officially been deprecated? (just out of interest).

Rick said...

Thanks for the post. I've recently started using Netbeans and GlassFish-- I think it's a landmark in JEE / IDE development. (Not as easy as VisualStudio, but getting close.)

Anyway, I thought your use of timehacks and 'breadcrumbs' to show menu choices were good. I may have to adapt those in my blog once in a while!

Keep up the good blogging!


Anonymous said...

Thank you for your tutorial.

I am getting some error messages:

Copying 1 file to D:\Aprendo\Java\BooksDemo\build
Compiling 1 source file to D:\Aprendo\Java\BooksDemo\BooksDemo-war\build\web\WEB-INF\classes
D:\Aprendo\Java\BooksDemo\BooksDemo-war\src\java\com\gamatam\tutorial\book\web\action\BookAdd.java:36: cannot find symbol
symbol : class Book
location: class com.gamatam.tutorial.book.web.action.BookAdd
Book book = new Book();

D:\Aprendo\Java\BooksDemo\BooksDemo-war\src\java\com\gamatam\tutorial\book\web\action\BookAdd.java:36: cannot find symbol
symbol : class Book
location: class com.gamatam.tutorial.book.web.action.BookAdd
Book book = new Book();

D:\Aprendo\Java\BooksDemo\BooksDemo-war\src\java\com\gamatam\tutorial\book\web\action\BookAdd.java:38: non-static method add(com.gamatam.tutorial.book.model.Book) cannot be referenced from a static context
3 errors

The first two error messages were fixed by importing Book class like this:

import com.gamatam.tutorial.book.model.Book;

but the last one I don't know how to fix it. I am new to java programming.

Do you have the source code for this project somewhere so I can download it.



Anonymous said...


The image for the Book Servlet is incorrect. You included the Session Bean's one.



Anonymous said...


You won't believe me but once I posted my comments I found the error.

Could you please ignore my two previous posts.

Thank you again for your tutorial. It has been very illustrative to me.


Anonymous said...

Kudos for creating a simple, and jargon-free tutorial on a topic that can be intimidating and confusing for newbies like me...

I was looking for a tutorial that would let me work on EJBs with the default database and glassfish app server in Netbeans, and this tutorial just hit the spot right on.

Highly reccommended for EJB newbies - keep up the good work!