Skip to main content

An Even Easier Way to Create a JSP

Posted by gsporar on October 7, 2005 at 2:58 PM PDT

Last week my colleague Brian Leonard wrote an excellent blog entry on how to quickly and easily build a JSP page that does a simple query of a database and then displays the output. He did a follow-up the next day that showed how to use XML for the data source instead of a database.

I decided to try building that same JSP page using a new feature that's in the 5.0 beta release of the NetBeans Integrated Development Environment (IDE). The new feature is a palette of controls that is available to help you build HTML and JSP pages faster. It looks like this:


To be honest, I was a little skeptical about this feature when I first heard about it. I like to keep my hands on the keyboard, and the new palette requires using a mouse. Turns out, though, it is worth taking your fingers off the home row in order to use it.

I had to type in Customer List as the top level header. And I also had to type in the <sql:setDataSource> tag (I'm not sure why that's not on the palette). Then it was time to start using the palette. I used the DB Query entry from the palette to generate the same SQL query that Brian had used:


Note, for reasons that are unclear, the dialog displayed by the palette did not allow me to specify the dataSource attribute. But it was easy enough to use code completion to type that in.

Then I added the table. This is where the payback started to really roll in. This is what I specified in the dialog that the palette presented to me when I dropped the table onto the JSP:


And the generated HTML was:


Nice. Naturally, I had to type in the details to match what Brian had, but the palette saved me some keystrokes.

Then all I needed was a forEach to pull the data from the select statement. There's a palette entry for that as well. I ran the application and got the same output that Brian did.

As I played around with the palette some more, I noticed an interesting entry: DB Report. It generates a query and a table. So I deleted the query and table I had just put in. After I filled in the values I needed in the DB Report dialog it looked like this:


The code it generated was:


Very nice. All I had to add was the dataSource attribute. When I ran the application I got:


That does not match exactly with what Brian had. There are two reasons: the DB Report dialog did not offer an option for a border attribute on the table. That's easy enough to fix using code completion. The other problem is the column names. Since I spelled them out, instead of using a * like Brian did, this is also easy to fix (and in fact, I could have done this earlier in the DB Report dialog when I filled in the query):


Then my application produced the same output as Brian's version. And I didn't have to type as much as he did. :-)