NetBeans 6.8 M1 introduces support for creating Java EE 6 applications … cool!
This Tip Of The Day (TOTD) shows how to create a simple web application using JPA 2.0 and Servlet 3.0 and deploy on GlassFish v3 latest promoted build (58 as of this writing). If you can work with the one week older build then NetBeans 6.8 M1 comes pre-bundled with 57. The example below should work fine on that as well.
- Create the database, table, and populate some data into it as shown below:
~/tools/glassfish/v3/58/glassfishv3/bin >sudo mysql –user root
Password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1592
Server version: 5.1.30 MySQL Community Server (GPL)Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.
mysql> create database states;
Query OK, 1 row affected (0.02 sec)mysql> CREATE USER duke IDENTIFIED by ‘glassfish’;
Query OK, 0 rows affected (0.00 sec)mysql> GRANT ALL on states.* TO duke;
Query OK, 0 rows affected (0.24 sec)mysql> use states;
Database changedmysql> CREATE TABLE STATES (
-> id INT,
-> abbrev VARCHAR(2),
-> name VARCHAR(50),
-> PRIMARY KEY (id)
-> );
Query OK, 0 rows affected (0.16 sec)mysql> INSERT INTO STATES VALUES (1, “AL”, “Alabama”);
INSERT INTO STATES VALUES (2, “AK”, “Alaska”);. . .
mysql> INSERT INTO STATES VALUES (49, “WI”, “Wisconsin”);
Query OK, 1 row affected (0.00 sec)mysql> INSERT INTO STATES VALUES (50, “WY”, “Wyoming”);
Query OK, 1 row affected (0.00 sec)The complete INSERT statement is available in TOTD #38. Most of this step can be executed from within the IDE as well as explained in TOTD #38.
- Download and unzip GlassFish v3 build 58. Copy the latest MySQL Connector/J jar in “domains/domain1/lib” directory of GlassFish and start the application server as:
~/tools/glassfish/v3/58/glassfishv3/bin >asadmin start-domain - Create JDBC connection pool and JNDI resource as shown below:
~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin create-jdbc-connection-pool –datasourceclassname com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource –restype javax.sql.DataSource –property “User=duke:Password=glassfish:URL=jdbc\:mysql\://localhost/states” jdbc/states Command create-jdbc-connection-pool executed successfully.
~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin ping-connection-pool jdbc/statesCommand ping-connection-pool executed successfully.
~/tools/glassfish/v3/58/glassfishv3/bin >./asadmin create-jdbc-resource –connectionpoolid jdbc/states jdbc/jndi_statesCommand create-jdbc-resource executed successfully.
- Download NetBeans 6.8 M1 and install “All” version. Expand “Servers” node and add the recently installed GlassFish server.
- Create a new Web project and name it “HelloEclipseLink”. Make sure to choose “GlassFish v3″ as the server and “Java EE 6 Web” as the Java EE version as shown below:
Take defaults elsewhere.
- Create the Persistence Unit
- Right-click on the newly created project and select “New”, “Entity Classes from Database …”. Choose the earlier created data source “jdbc/jndi_states” as shown below:
- Select “STATES” table in “Available Tables:” and click on “Add >” and then “Next >”.
- Click on “Create Persistence Unit …”, take all the defaults and click on “Create”. “EclipseLink” is the Reference Implementation for JPA 2.0 is the default choosen Persistence Provider as shown below:
- Enter the package name as “server” and click on “Finish”.
- Create a Servlet to retrieve and display all the information from the database
- Right click on the project, “New”, “Servlet …”.
- Give the Servlet name “ShowStates” and package “server”.
- Even though you can take all the defaults and click on “Finish” but instead click on “Next >” and the following screen is shown:
Notice “Add information to deployment descriptor (web.xml)” checkbox. Servlet 3.0 makes “web.xml” optional in most of the common cases by providing corresponding annotations and NetBeans 6.8 leverages that functionality. As a result, no “web.xml” will be bundled in our WAR file. Click on “Finish” now.
The generated servlet code looks like:
Notice @WebServlet annotation, this makes “web.xml” optional. TOTD #82 provide another example on how to use Servlet 3.0 with EJB 3.1.
- Inject the Persistence Unit as:
@PersistenceUnit
EntityManagerFactory emf;right above “processRequest” method.
- Change the “try” block of “processRequest” method to:
List<States> list = emf.createEntityManager().createNamedQuery(“States.findAll”).getResultList();
out.println(“<table border=\”1\”>”);
for (States state : list) {
out.println(“<tr><td>” + state.getAbbrev() +
“</td><td>” + state.getName() +
“</td></tr>”);
}
out.println(“</table>”);This uses a predefined query to retrieve all rows from the table and then display them in a simple formatted HTML table.
- Run the project
- Right click on the project, select “Properties” and change the “Relative URL” to “/ShowStates”. This is the exact URL that you specified earlier.
- Right-click on the project and select “Run” to see the following output:
So we created a simple web application that uses Servlet 3.0, JPA 2.0, EclipseLink and deployed on GlassFish v3 using NetBeans 6.8 M1. NetBeans provides reasonable defaults making you a lazy programmer. Believe this is more evident when you start playing with Java EE support in other IDEs
Finally, lets look at the structure of the generated WAR file:
It’s very clean – no “web.xml”, only the relevant classes and “persistence.xml”.
Also refer to other Java EE 6 blog entries. A future blog entry will show how to use JSF 2.0 instead of Servlet for displaying the results.
Please leave suggestions on other TOTD that you’d like to see. A complete archive of all the tips is available here.
Technorati: totd glassfish v3 mysql javaee6 servlet3 jpa2 netbeans
Related posts:- TOTD #94: A simple Java Server Faces 2.0 + JPA 2.0 application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
- TOTD #95: EJB 3.1 + Java Server Faces 2.0 + JPA 2.0 web application – Getting Started with Java EE 6 using NetBeans 6.8 M1 & GlassFish v3
- TOTD #99: Creating a Java EE 6 application using MySQL, JPA 2.0 and Servlet 3.0 with GlassFish Tools Bundle for Eclipse
- TOTD #108: Java EE 6 web application (JSF 2.0 + JPA 2.0 + EJB 3.1) using Oracle, NetBeans, and GlassFish
- TOTD #82: Getting Started with Servlet 3.0 and EJB 3.1 in Java EE 6 using NetBeans 6.7
Thanks Arun, this is extremely useful. I was a bit stuck with the connection between servlet and JPA since I used "Persistence -> Use EntityManager" from NB.
Your code (using EntityManagerFactory.createEntityManager()) did the job for me.
Thanks a lot!
- Johan
Comment by Johan Vos — August 13, 2009 @ 5:32 am
[Trackback] TOTD #93 showed how to get started with Java EE 6 using NetBeans 6.8 M1 and GlassFish v3 by building a simple Servlet 3.0 + JPA 2.0 web application. JPA 2.0 + Eclipselink was used for the database connectivity…
Comment by Arun Gupta's Blog — August 14, 2009 @ 8:41 am
[Trackback] TOTD #93 showed how to get started with Java EE 6 using NetBeans 6.8 M1 and GlassFish v3 by building a simple Servlet 3.0 + JPA 2.0 web application. TOTD #94 built upon it by using Java Server Faces…
Comment by Arun Gupta's Blog — August 17, 2009 @ 8:36 am
[...] Preserving session state across deployments, Java EE 6 wizards in NetBeans (1, 2, 3, 4, 5) and Eclipse (1, 2, 3), Metro JAX-WS Web services development in Eclipse, GlassFish v3 [...]
Pingback by Silicon Valley Code Code Camp 2009 Trip Report « Miles to go … — October 3, 2009 @ 11:34 pm