<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PlanetMysql.ru - информация о СУБД MySQL &#187; jpa2</title>
	<atom:link href="http://planetmysql.ru/category/jpa2/feed/" rel="self" type="application/rss+xml" />
	<link>http://planetmysql.ru</link>
	<description>Блог о самой популярной СУБД MySQL</description>
	<lastBuildDate>Fri, 10 Feb 2012 22:53:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>TOTD #95: EJB 3.1 + Java Server Faces 2.0 + JPA 2.0 web application &#8212; Getting Started with Java EE 6 using NetBeans 6.8 M1 &amp; GlassFish v3</title>
		<link>http://blogs.sun.com/arungupta/entry/totd_95_ejb_3_1?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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</link>
		<comments>http://blogs.sun.com/arungupta/entry/totd_95_ejb_3_1#comments</comments>
		<pubDate>Mon, 17 Aug 2009 10:00:00 +0000</pubDate>
		<dc:creator>Arun Gupta</dc:creator>
				<category><![CDATA[ejb]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[javaee6]]></category>
		<category><![CDATA[javaserverfaces]]></category>
		<category><![CDATA[jpa2]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[totd]]></category>
		<category><![CDATA[v3]]></category>

		<guid isPermaLink="false">http://blogs.sun.com/arungupta/entry/totd_95_ejb_3_1</guid>
		<description><![CDATA[
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 2 instead of
Servlet 3.0 for displaying the results. However we are still using a
POJO
for all the database interactions. This works fine if we are only
reading values from the database but that's not how a typical web
application behaves. The web application would typically perform all
CRUD operations. More typically they like to perform one or more CRUD
operations within the context of a transaction. And how do you do
transactions in the context of a web application ? Java EE 6 comes to
your rescue.

The EJB 3.1
specification (another new specification in Java EE 6) allow
POJO classes to be annotated with @EJB and bundled within
WEB-INF/classes of a WAR file. And so you get all transactional
capabilities in your web application very easily.

This&#160;Tip
Of The Day (TOTD) shows how
to enhance the application created in TOTD #94 and use EJB 3.1 instead
of the JSF managed bean
for
performing the business logic. There are two ways to achieve this
pattern as described below.

Lets call this TOTD #95.1

  The easiest way to back a JSF page with an EJB is to
convert the managed bean into an EJB by adding @javax.ejb.Stateless
annotation. So change the&#160; "StateList" class from TOTD #94 as
shown below:
    
    
      
        
          @javax.ejb.Stateless
@ManagedBean
public class StateList {
&#160;&#160;&#160; @PersistenceUnit
&#160;&#160;&#160; EntityManagerFactory emf;
          
&#160;&#160;&#160; public List&#60;States&#62;
getStates() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;
return
emf.createEntityManager().createNamedQuery("States.findAll").getResultList();
&#160;&#160;&#160; }
}
        
      
    
    
The change is highlighted in bold, and that's it!

Because of "Deploy-on-save" feature in NetBeans and GlassFish v3, the
application is autodeployed. Otherwise right-click on the project and
select Run (default shortcut "F6"). As earlier, the results can be seen
at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or
"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"
and looks like:



The big difference this time is that the business logic is executed by
an EJB in a fully transactional manner. Even though the logic in this
case is a single read-only operation to the database, but you get the
idea :)

Alternatively, you can use the delegate pattern in the managed bean as
described below. Lets call this #95.2.

  Right-click on the project, select "New", "Session Bean
..." and create a stateless session bean by selecting the options as
shown below:
    
    
    
This creates a stateless session with the name "StateBeanBean" (bug
#170392 for redundant "Bean" in the name).
  Simplify your managed bean by refactoring all the business
logic to the EJB as shown below:
    
    
      
        
          @Stateless
public class StateBeanBean {
&#160;&#160;&#160; @PersistenceUnit
&#160;&#160;&#160; EntityManagerFactory emf;
&#160;&#160; &#160;
&#160;&#160;&#160; public List&#60;States&#62;
getStates() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;
return
emf.createEntityManager().createNamedQuery("States.findAll").getResultList();
&#160;&#160;&#160; }
}
        
      
    
    
and 
    
    
      
        
          @ManagedBean
public class StateList {
&#160;&#160;&#160; @EJB StateBeanBean bean;
          
&#160;&#160;&#160; public List&#60;States&#62;
getStates() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;
return bean.getStates();
&#160;&#160;&#160; }
}
          
        
      
    
    
In fact the EJB code can be further simplified to:
    
    
      
        
          @Stateless
public class StateBeanBean {
&#160;&#160;&#160; @PersistenceContext
          &#160;&#160;&#160;
EntityManager em;
&#160;&#160;&#160; 
&#160;&#160;&#160; public List&#60;States&#62;
getStates() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;
return em.createNamedQuery("States.findAll").getResultList();
&#160;&#160;&#160; }
}
          
        
      
    
    
The changes are highlighted in bold.
  

If the application is already running then Deploy-on-Save
would have automatically deployed the entire application. Otherwise
right-click on the project and select Run (default shortcut "F6").
Again, the
results can be seen at
"http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or
"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"
and are displayed as shown in the screenshot above.

The updated directory structure looks like:



The important point to note is that our EJB is bundled in the WAR file
and no additional deployment descriptors were added or existing ones
modified to achieve that. Now, that's really clean :)

The next blog in this series will show how managed beans can be
replaced with WebBeans, err JCDI.

Also refer to other Java EE 6
blog entries. 

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 javaserverfaces
jpa2 ejb netbeans
]]></description>
			<content:encoded><![CDATA[<br>
<a href="http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with">TOTD
#93</a>
showed how to get started with <a href="http://jcp.org/en/jsr/detail?id=316">Java EE 6</a>
using <a href="http://www.netbeans.org/servlets/NewsItemView?newsItemID=1413">NetBeans
6.8 M1</a> and
<a href="http://glassfish.org">GlassFish v3</a> by
building a simple Servlet 3.0 + JPA 2.0 web
application. <a href="http://blogs.sun.com/arungupta/entry/totd_94_a_simple_java">TOTD
#94</a> built upon it by using Java Server Faces 2 instead of
Servlet 3.0 for displaying the results. However we are still using a
POJO
for all the database interactions. This works fine if we are only
reading values from the database but that's not how a typical web
application behaves. The web application would typically perform all
CRUD operations. More typically they like to perform one or more CRUD
operations within the context of a transaction. And how do you do
transactions in the context of a web application ? Java EE 6 comes to
your rescue.<br>
<br>
The <a href="http://jcp.org/en/jsr/detail?id=318">EJB 3.1
specification</a> (another new specification in Java EE 6) allow
POJO classes to be annotated with @EJB and bundled within
WEB-INF/classes of a WAR file. And so you get all transactional
capabilities in your web application very easily.<br>
<br>
This&nbsp;<span>T</span>ip
<span>O</span>f <span>T</span>he <span>D</span>ay (TOTD) shows how
to enhance the application created in TOTD #94 and use EJB 3.1 instead
of the JSF <span>managed bean</span>
for
performing the business logic. There are two ways to achieve this
pattern as described below.<br>
<br>
Lets call this TOTD #95.1<br>
<ol>
  <li>The easiest way to back a JSF page with an EJB is to
convert the managed bean into an EJB by adding @javax.ejb.Stateless
annotation. So change the&nbsp; "StateList" class from TOTD #94 as
shown below:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td><span>@javax.ejb.Stateless</span><br>
@ManagedBean<br>
public class StateList {<br>
&nbsp;&nbsp;&nbsp; @PersistenceUnit<br>
&nbsp;&nbsp;&nbsp; EntityManagerFactory emf;<br>
          <br>
&nbsp;&nbsp;&nbsp; public List&lt;States&gt;
getStates() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return
emf.createEntityManager().createNamedQuery("States.findAll").getResultList();<br>
&nbsp;&nbsp;&nbsp; }<br>
}</td>
        </tr>
      </tbody>
    </table>
    <br>
The change is highlighted in bold, and that's it!</li>
</ol>
Because of "Deploy-on-save" feature in NetBeans and GlassFish v3, the
application is autodeployed. Otherwise right-click on the project and
select Run (default shortcut "F6"). As earlier, the results can be seen
at "http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or
"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"
and looks like:<br>
<br>
<img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-ejb-1st-output.png" /><br>
<br>
The big difference this time is that the business logic is executed by
an EJB in a fully transactional manner. Even though the logic in this
case is a single read-only operation to the database, but you get the
idea :)<br>
<br>
Alternatively, you can use the delegate pattern in the <span>managed bean</span> as
described below. Lets call this #95.2.
<ol>
  <li>Right-click on the project, select "New", "Session Bean
..." and create a stateless session bean by selecting the options as
shown below:<br>
    <br>
    <span><img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-ejb-create.png" /></span><br>
    <br>
This creates a stateless session with the name "StateBeanBean" (<a href="http://www.netbeans.org/issues/show_bug.cgi?id=170392">bug
#170392</a> for redundant "Bean" in the name).</li>
  <li>Simplify your managed bean by refactoring all the business
logic to the EJB as shown below:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>@Stateless<br>
public class StateBeanBean {<br>
&nbsp;&nbsp;&nbsp; @PersistenceUnit<br>
&nbsp;&nbsp;&nbsp; EntityManagerFactory emf;<br>
&nbsp;&nbsp; &nbsp;<br>
&nbsp;&nbsp;&nbsp; public List&lt;States&gt;
getStates() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return
emf.createEntityManager().createNamedQuery("States.findAll").getResultList();<br>
&nbsp;&nbsp;&nbsp; }<br>
}</td>
        </tr>
      </tbody>
    </table>
    <br>
and <br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>@ManagedBean<br>
public class StateList {<br>
&nbsp;&nbsp;&nbsp; @EJB StateBeanBean bean;<br>
          <br>
&nbsp;&nbsp;&nbsp; public List&lt;States&gt;
getStates() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return bean.getStates();<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
          <span></span></td>
        </tr>
      </tbody>
    </table>
    <br>
In fact the EJB code can be further simplified to:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>@Stateless<br>
public class StateBeanBean {<br>
&nbsp;&nbsp;&nbsp; <span>@PersistenceContext</span><br>
          <span>&nbsp;&nbsp;&nbsp;
EntityManager em;</span><br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; public List&lt;States&gt;
getStates() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return <span>em.</span>createNamedQuery("States.findAll").getResultList();<br>
&nbsp;&nbsp;&nbsp; }<br>
}<br>
          <span></span></td>
        </tr>
      </tbody>
    </table>
    <br>
The changes are highlighted in bold.<br>
  </li>
</ol>
If the application is already running then <span>Deploy-on-Save</span>
would have automatically deployed the entire application. Otherwise
right-click on the project and select Run (default shortcut "F6").
Again, the
results can be seen at
"http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or
"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"
and are displayed as shown in the screenshot above.<br>
<br>
The updated directory structure looks like:<br>
<br>
<img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-ejb-dir-structure.png" /><br>
<br>
The important point to note is that our EJB is bundled in the WAR file
and no additional deployment descriptors were added or existing ones
modified to achieve that. Now, that's really clean :)<br>
<br>
The next blog in this series will show how managed beans can be
replaced with WebBeans, err JCDI.<br>
<br>
Also refer to other <a href="http://blogs.sun.com/arungupta/tags/javaee6">Java EE 6
blog entries</a>. <br>
<br>
Please leave suggestions on other TOTD that
you'd like to see.
A complete archive of all the tips is available <a href="http://blogs.sun.com/arungupta/tags/totd">here</a>.
<br>
<br>
<small>Technorati: <a href="http://technorati.com/tags/totd">totd</a>
<a href="http://technorati.com/tags/glassfish">glassfish</a>
<a href="http://technorati.com/tags/v3">v3</a> <a href="http://technorati.com/tags/mysql">mysql</a> <a href="http://technorati.com/tags/javaee6">javaee6</a> <a href="http://technorati.com/tags/javaserverfaces">javaserverfaces</a>
<a href="http://technorati.com/tags/jpa2">jpa2</a> <a href="http://technorati.com/tags/ejb">ejb</a> <a href="http://technorati.com/tags/netbeans">netbeans</a></small>
<br><br/>PlanetMySQL Voting:
	 <a href="http://planet.mysql.com/entry/vote/?entry_id=20667&vote=1&apivote=1">Vote UP</a> /
	 <a href="http://planet.mysql.com/entry/vote/?entry_id=20667&vote=-1&apivote=1">Vote DOWN</a>]]></content:encoded>
			<wfw:commentRss>http://planetmysql.ru/2009/08/17/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/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TOTD #94: A simple Java Server Faces 2.0 + JPA 2.0 application &#8212; Getting Started with Java EE 6 using NetBeans 6.8 M1 &amp; GlassFish v3</title>
		<link>http://blogs.sun.com/arungupta/entry/totd_94_a_simple_java?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=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</link>
		<comments>http://blogs.sun.com/arungupta/entry/totd_94_a_simple_java#comments</comments>
		<pubDate>Fri, 14 Aug 2009 10:00:00 +0000</pubDate>
		<dc:creator>Arun Gupta</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[javaee6]]></category>
		<category><![CDATA[javaserverfaces]]></category>
		<category><![CDATA[jpa2]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[totd]]></category>
		<category><![CDATA[v3]]></category>

		<guid isPermaLink="false">http://blogs.sun.com/arungupta/entry/totd_94_a_simple_java</guid>
		<description><![CDATA[
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
and Servlet 3.0 was used for displaying the results to the user. The
sample demonstrated how the two technologies can be mixed to create a
simple web application. But Servlets are meant for server-side
processing rather than displaying the results to end user. JavaServer
Faces 2 (another new specification in Java EE 6) is designed
to fulfill
that purpose.

This&#160;Tip
Of The Day (TOTD) shows how
to enhance the application created in TOTD #93 and use JSF 2 for
displaying the results.

  Right-click on the project, select "Properties", select
"Frameworks", click on "Add ..." as shown below:
    
    
    
Select "JavaServer Faces" and click on "OK". The following
configuration screen is shown:
    
    
    
Click on "OK" to complete the dialog. This generates a whole bunch of
files (7 to be accurate) in your project. Most of these files are
leftover from previous version of NetBeans and will be cleaned up. For
example, "faces-config.xml" is now optional and "forwardToJSF.jsp" is
redundant.
  Anyway, lets add a POJO class that will be our managed bean.
Right-click on "server" package and select "New", "Java Class ...",
give the name as "StateList". Change the class such that it looks like:
    
    
      
        
          package server;
          
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import states.States;
          
/**
&#160;* @author arungupta
&#160;*/
@ManagedBean
public class StateList {
&#160;&#160;&#160; @PersistenceUnit
&#160;&#160;&#160; EntityManagerFactory emf;
          
&#160;&#160;&#160; public List&#60;States&#62;
getStates() {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;
return
emf.createEntityManager().createNamedQuery("States.findAll").getResultList();
&#160;&#160;&#160; }
}
        
      
    
    
Here are the main characterisitcs of this class:
  
  
    This is a POJO class with @ManagedBean annotation. This
annotation makes this class a managed
bean that can be used in the JSF pages. As no other
annotations or parameters are specified, this is a request-scoped
managed bean with the name "stateList" and lazily initialized. More
details about this annotation are available in the javadocs.
    The persistence unit created in TOTD
#93 is injected using @PersistenceUnit annotation.
    The POJO has one getter method that queries the database
and return the list of all the states.
    
  
  In the generated file "template-client.xhtml", change the
"head" template to:
    
    
      
        
          Show States
          
        
      
    
    
and "body" template to:
    
    
      
        
          &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
&#60;h:dataTable var="state" value="#{stateList.states}"
border="1"&#62;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
&#60;h:column&#62;&#60;h:outputText
value="#{state.abbrev}"/&#62;&#60;/h:column&#62;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
&#60;h:column&#62;&#60;h:outputText
value="#{state.name}"/&#62;&#60;/h:column&#62;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
&#60;/h:dataTable&#62;
          
        
      
    
  
  
This uses the standard JSF "dataTable", "column", and "outputText" tags
and uses the value
expression to fetch the values from the managed bean.
  

If the application is already running from&#160;TOTD
#93, then Deploy-on-Save
would have automatically deployed the entire application. Otherwise
right-click on the project and select Run (default shortcut "F6"). The
results can be seen at
"http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or
"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"
and looks like:



The updated directory structure looks like:



There were multiple files added by the JSF framework support in
NetBeans. But as I said earlier, they will be cleaned up before the
final release.

Also refer to other Java EE 6
blog entries. 

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 javaserverfaces
jpa2 netbeans

]]></description>
			<content:encoded><![CDATA[<br>
<a href="http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with">TOTD
#93</a>
showed how to get started with <a href="http://jcp.org/en/jsr/detail?id=316">Java EE 6</a>
using <a href="http://www.netbeans.org/servlets/NewsItemView?newsItemID=1413">NetBeans
6.8 M1</a> and
<a href="http://glassfish.org">GlassFish v3</a> by
building a simple Servlet 3.0 + JPA 2.0 web
application. JPA 2.0 + Eclipselink was used for the database
connectivity
and Servlet 3.0 was used for displaying the results to the user. The
sample demonstrated how the two technologies can be mixed to create a
simple web application. But Servlets are meant for server-side
processing rather than displaying the results to end user. <a href="http://jcp.org/en/jsr/detail?id=314">JavaServer
Faces 2</a> (another new specification in Java EE 6) is designed
to fulfill
that purpose.<br>
<br>
This&nbsp;<span>T</span>ip
<span>O</span>f <span>T</span>he <span>D</span>ay (TOTD) shows how
to enhance the application created in TOTD #93 and use JSF 2 for
displaying the results.<br>
<ol>
  <li>Right-click on the project, select "Properties", select
"Frameworks", click on "Add ..." as shown below:<br>
    <br>
    <a href="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-add-framework.png"><img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-add-framework.png" /></a><br>
    <br>
Select "JavaServer Faces" and click on "OK". The following
configuration screen is shown:<br>
    <br>
    <a href="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-framework-added.png"><img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-framework-added.png" /></a><br>
    <br>
Click on "OK" to complete the dialog. This generates a whole bunch of
files (7 to be accurate) in your project. Most of these files are
leftover from previous version of NetBeans and will be cleaned up. For
example, "faces-config.xml" is now optional and "forwardToJSF.jsp" is
redundant.</li>
  <li>Anyway, lets add a POJO class that will be our <span>managed bean</span>.
Right-click on "server" package and select "New", "Java Class ...",
give the name as "StateList". Change the class such that it looks like:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>package server;<br>
          <br>
import java.util.List;<br>
import javax.faces.bean.ManagedBean;<br>
import javax.persistence.EntityManagerFactory;<br>
import javax.persistence.PersistenceUnit;<br>
import states.States;<br>
          <br>
/**<br>
&nbsp;* @author arungupta<br>
&nbsp;*/<br>
@ManagedBean<br>
public class StateList {<br>
&nbsp;&nbsp;&nbsp; @PersistenceUnit<br>
&nbsp;&nbsp;&nbsp; EntityManagerFactory emf;<br>
          <br>
&nbsp;&nbsp;&nbsp; public List&lt;States&gt;
getStates() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
return
emf.createEntityManager().createNamedQuery("States.findAll").getResultList();<br>
&nbsp;&nbsp;&nbsp; }<br>
}</td>
        </tr>
      </tbody>
    </table>
    <br>
Here are the main characterisitcs of this class:<br>
  </li>
  <ol>
    <li>This is a POJO class with @ManagedBean annotation. This
annotation makes this class a <span>managed
bean</span> that can be used in the JSF pages. As no other
annotations or parameters are specified, this is a request-scoped
managed bean with the name "stateList" and lazily initialized. More
details about this annotation are available in the <a href="https://javaserverfaces.dev.java.net/nonav/docs/2.0/managed-bean-javadocs/index.html">javadocs</a>.</li>
    <li>The persistence unit created in <a href="http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with">TOTD
#93</a> is injected using @PersistenceUnit annotation.</li>
    <li>The POJO has one getter method that queries the database
and return the list of all the states.<br>
    </li>
  </ol>
  <li>In the generated file "template-client.xhtml", change the
"head" template to:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>Show States<br>
          <span></span></td>
        </tr>
      </tbody>
    </table>
    <br>
and "body" template to:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;h:dataTable var="state" value="#{stateList.states}"
border="1"&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;h:column&gt;&lt;h:outputText
value="#{state.abbrev}"/&gt;&lt;/h:column&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;h:column&gt;&lt;h:outputText
value="#{state.name}"/&gt;&lt;/h:column&gt;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;/h:dataTable&gt;<br>
          <span></span></td>
        </tr>
      </tbody>
    </table>
  </li>
  <br>
This uses the standard JSF "dataTable", "column", and "outputText" tags
and uses the <span>value
expression</span> to fetch the values from the <span>managed bean</span>.<br>
  <br>
</ol>
If the application is already running from&nbsp;<a href="http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with">TOTD
#93</a>, then <span>Deploy-on-Save</span>
would have automatically deployed the entire application. Otherwise
right-click on the project and select Run (default shortcut "F6"). The
results can be seen at
"http://localhost:8080/HelloEclipseLink/forwardToJSF.jsp" or
"http://localhost:8080/HelloEclipseLink/faces/template-client.xhtml"
and looks like:<br>
<br>
<img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-output.png" /><br>
<br>
The updated directory structure looks like:<br>
<br>
<img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jee6-jsf2-dir-structure.png" /><br>
<br>
There were multiple files added by the JSF framework support in
NetBeans. But as I said earlier, they will be cleaned up before the
final release.<br>
<br>
Also refer to other <a href="http://blogs.sun.com/arungupta/tags/javaee6">Java EE 6
blog entries</a>. <br>
<br>
Please leave suggestions on other TOTD that
you'd like to see.
A complete archive of all the tips is available <a href="http://blogs.sun.com/arungupta/tags/totd">here</a>.
<br>
<br>
<small>Technorati: <a href="http://technorati.com/tags/totd">totd</a>
<a href="http://technorati.com/tags/glassfish">glassfish</a>
<a href="http://technorati.com/tags/v3">v3</a> <a href="http://technorati.com/tags/mysql">mysql</a> <a href="http://technorati.com/tags/javaee6">javaee6</a> <a href="http://technorati.com/tags/javaserverfaces">javaserverfaces</a>
<a href="http://technorati.com/tags/jpa2">jpa2</a> <a href="http://technorati.com/tags/netbeans">netbeans</a></small>
<br>
<br><br/>PlanetMySQL Voting:
	 <a href="http://planet.mysql.com/entry/vote/?entry_id=20638&vote=1&apivote=1">Vote UP</a> /
	 <a href="http://planet.mysql.com/entry/vote/?entry_id=20638&vote=-1&apivote=1">Vote DOWN</a>]]></content:encoded>
			<wfw:commentRss>http://planetmysql.ru/2009/08/14/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/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TOTD #93: Getting Started with Java EE 6 using NetBeans 6.8 M1 &amp; GlassFish v3 &#8212; A simple Servlet 3.0 + JPA 2.0 app</title>
		<link>http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=totd-93-getting-started-with-java-ee-6-using-netbeans-6-8-m1-glassfish-v3-a-simple-servlet-3-0-jpa-2-0-app</link>
		<comments>http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with#comments</comments>
		<pubDate>Thu, 13 Aug 2009 10:00:00 +0000</pubDate>
		<dc:creator>Arun Gupta</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[glassfish]]></category>
		<category><![CDATA[javaee6]]></category>
		<category><![CDATA[jpa2]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[netbeans]]></category>
		<category><![CDATA[servlet3]]></category>
		<category><![CDATA[totd]]></category>
		<category><![CDATA[v3]]></category>

		<guid isPermaLink="false">http://blogs.sun.com/arungupta/entry/totd_93_getting_started_with</guid>
		<description><![CDATA[
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&#160;&#62;sudo mysql --user root
Password:
Welcome to the MySQL monitor.&#160; 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&#62; create
database states;
Query OK, 1 row affected (0.02 sec)
          
mysql&#62; CREATE
USER duke IDENTIFIED by 'glassfish';
Query OK, 0 rows affected (0.00 sec)
          
mysql&#62; GRANT ALL
on states.* TO duke;
Query OK, 0 rows affected (0.24 sec)
          
mysql&#62; use states;
          Database changed
          
mysql&#62; CREATE
TABLE STATES (
          &#160;&#160;&#160;
-&#62;&#160;&#160;&#160;&#160;&#160;&#160;
id INT,
          &#160;&#160;&#160;
-&#62;&#160;&#160;&#160;&#160;&#160;&#160;
abbrev VARCHAR(2),
          &#160;&#160;&#160;
-&#62;&#160;&#160;&#160;&#160;&#160;&#160;
name VARCHAR(50),
          &#160;&#160;&#160;
-&#62;&#160;&#160;&#160;&#160;&#160;&#160;
PRIMARY KEY (id)
          &#160;&#160;&#160;
-&#62; );
Query OK, 0 rows affected (0.16 sec)
          
mysql&#62; INSERT
INTO STATES VALUES (1, "AL", "Alabama");
          INSERT INTO
STATES VALUES (2, "AK", "Alaska");
          
. . .
          
mysql&#62; INSERT
INTO STATES VALUES (49, "WI", "Wisconsin");
Query OK, 1 row affected (0.00 sec)
          
mysql&#62; 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 &#62;asadmin start-domain
        
      
    
  
  Create JDBC connection pool and JNDI resource as shown
below:
    
    
      
        
          ~/tools/glassfish/v3/58/glassfishv3/bin &#62;./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 &#62;./asadmin ping-connection-pool
jdbc/states
          
Command ping-connection-pool executed successfully.
~/tools/glassfish/v3/58/glassfishv3/bin &#62;./asadmin create-jdbc-resource
--connectionpoolid jdbc/states jdbc/jndi_states
          
Command 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 &#62;" and then "Next &#62;".
    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 &#62;" 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:
      
      
        
          
            &#160;&#160;&#160; @PersistenceUnit
&#160;&#160;&#160; EntityManagerFactory emf;
          
        
      
      
right above "processRequest" method.
    Change the "try" block of "processRequest" method to:
      
      
        
          
            &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
List&#60;States&#62; list =
emf.createEntityManager().createNamedQuery("States.findAll").getResultList();
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
out.println("&#60;table border=\"1\"&#62;");
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
for (States state : list) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
out.println("&#60;tr&#62;&#60;td&#62;" + state.getAbbrev() +
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
"&#60;/td&#62;&#60;td&#62;" + state.getName() +
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
"&#60;/td&#62;&#60;/tr&#62;");
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
}
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;
out.println("&#60;/table&#62;");
          
        
      
      
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]]></description>
			<content:encoded><![CDATA[<br>
<a href="http://www.netbeans.org/servlets/NewsItemView?newsItemID=1413">NetBeans
6.8 M1</a> introduces support for creating Java EE 6 applications
... cool!<br>
<br>
This <span>T</span>ip <span>O</span>f <span>T</span>he <span>D</span>ay (TOTD) shows how
to create a simple web application using JPA 2.0 and Servlet 3.0 and
deploy on GlassFish v3 <a href="http://download.java.net/glassfish/v3/promoted/">latest
promoted build</a> (<a href="http://download.java.net/glassfish/v3/promoted/glassfish-v3-preview-b58.zip">58</a>
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.<br>
<ol>
  <li>Create the database, table, and populate some data into it
as shown below:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>~/tools/glassfish/v3/58/glassfishv3/bin&nbsp;&gt;<span>sudo mysql --user root</span><br>
Password:<br>
Welcome to the MySQL monitor.&nbsp; Commands end with ; or \g.<br>
Your MySQL connection id is 1592<br>
Server version: 5.1.30 MySQL Community Server (GPL)<br>
          <br>
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.<br>
          <br>
mysql&gt; <span>create
database states;</span><br>
Query OK, 1 row affected (0.02 sec)<br>
          <br>
mysql&gt; <span>CREATE
USER duke IDENTIFIED by 'glassfish';</span><br>
Query OK, 0 rows affected (0.00 sec)<br>
          <br>
mysql&gt; <span>GRANT ALL
on states.* TO duke;</span><br>
Query OK, 0 rows affected (0.24 sec)<br>
          <br>
mysql&gt; <span>use states;</span><br>
          <span>Database changed</span><br>
          <br>
mysql&gt; <span>CREATE
TABLE STATES (</span><br>
          <span>&nbsp;&nbsp;&nbsp;
-&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
id INT,</span><br>
          <span>&nbsp;&nbsp;&nbsp;
-&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
abbrev VARCHAR(2),</span><br>
          <span>&nbsp;&nbsp;&nbsp;
-&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
name VARCHAR(50),</span><br>
          <span>&nbsp;&nbsp;&nbsp;
-&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
PRIMARY KEY (id)</span><br>
          <span>&nbsp;&nbsp;&nbsp;
-&gt; );</span><br>
Query OK, 0 rows affected (0.16 sec)<br>
          <br>
mysql&gt; <span>INSERT
INTO STATES VALUES (1, "AL", "Alabama");</span><br>
          <span>INSERT INTO
STATES VALUES (2, "AK", "Alaska");</span><br>
          <br>
. . .<br>
          <br>
mysql&gt; <span>INSERT
INTO STATES VALUES (49, "WI", "Wisconsin");</span><br>
Query OK, 1 row affected (0.00 sec)<br>
          <br>
mysql&gt; <span>INSERT
INTO STATES VALUES (50, "WY", "Wyoming");</span><br>
Query OK, 1 row affected (0.00 sec)</td>
        </tr>
      </tbody>
    </table>
    <br>
The complete INSERT statement is available in <a href="http://blogs.sun.com/arungupta/entry/totd_38_creating_a_mysql">TOTD
#38</a>. Most of this step can be executed from within the IDE as
well as explained in <a href="http://blogs.sun.com/arungupta/entry/totd_38_creating_a_mysql">TOTD
#38</a>. </li>
  <li>Download and unzip GlassFish v3 <a href="http://download.java.net/glassfish/v3/promoted/glassfish-v3-preview-b58.zip">build
58</a>. Copy the latest <a href="http://dev.mysql.com/downloads/connector/j/5.1.html">MySQL
Connector/J</a>
jar in "domains/domain1/lib" directory of GlassFish and start the
application server
as:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>~/tools/glassfish/v3/58/glassfishv3/bin &gt;<span>asadmin start-domain</span></td>
        </tr>
      </tbody>
    </table>
  </li>
  <li>Create JDBC connection pool and JNDI resource as shown
below:<br>
    <br>
    <table
 cellpadding="2" cellspacing="2">
      <tbody>
        <tr>
          <td>~/tools/glassfish/v3/58/glassfishv3/bin &gt;<span>./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</span><br>
          <br>
Command create-jdbc-connection-pool executed successfully.<br>
~/tools/glassfish/v3/58/glassfishv3/bin &gt;<span>./asadmin ping-connection-pool
jdbc/states</span><br>
          <br>
Command ping-connection-pool executed successfully.<br>
~/tools/glassfish/v3/58/glassfishv3/bin &gt;<span>./asadmin create-jdbc-resource
--connectionpoolid jdbc/states jdbc/jndi_states</span><br>
          <br>
Command create-jdbc-resource executed successfully.</td>
        </tr>
      </tbody>
    </table>
    <br>
  </li>
  <li>Download <a href="http://bits.netbeans.org/netbeans/6.8/m1/">NetBeans
6.8 M1</a> and install "All" version. Expand "Servers" node and
add the recently installed GlassFish server.</li>
  <li>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:<br>
    <br>
    <img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jpa-javaee6web.png" /><br>
    <br>
Take defaults elsewhere.</li>
  <li>Create the Persistence Unit</li>
  <ol>
    <li>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:<br>
      <br>
      <img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jpa-datasource.png" /></li>
    <li>Select "STATES" table in "Available Tables:" and click on
"Add &gt;" and then "Next &gt;".</li>
    <li>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:<br>
      <br>
      <img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jpa-pu.png" /></li>
    <li>Enter the package name as "server" and click on "Finish".</li>
  </ol>
  <li>Create a Servlet to retrieve and display all the
information from the database</li>
  <ol>
    <li>Right click on the project, "New", "Servlet ...".</li>
    <li>Give the Servlet name "ShowStates" and package "server".</li>
    <li>Even
though you can take all the defaults and click on "Finish" but instead
click on "Next &gt;" and the following screen is shown:<br>
      <br>
      <img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jpa-servlet-deployment.png" /><br>
      <br>
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.<br>
      <br>
The generated servlet code looks like:<br>
      <br>
      <img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jpa-servlet-annotations.png" /><br>
      <br>
Notice @WebServlet annotation, this makes "web.xml" optional. <a href="http://blogs.sun.com/arungupta/entry/totd_81_getting_started_with">TOTD
#82</a> provide another example on how to use Servlet 3.0 with
EJB 3.1.</li>
    <li>Inject the Persistence Unit as:<br>
      <br>
      <table
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td>&nbsp;&nbsp;&nbsp; @PersistenceUnit<br>
&nbsp;&nbsp;&nbsp; EntityManagerFactory emf;</td>
          </tr>
        </tbody>
      </table>
      <br>
right above "processRequest" method.</li>
    <li>Change the "try" block of "processRequest" method to:<br>
      <br>
      <table
 cellpadding="2" cellspacing="2">
        <tbody>
          <tr>
            <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
List&lt;States&gt; list =
emf.createEntityManager().createNamedQuery("States.findAll").getResultList();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
out.println("&lt;table border=\"1\"&gt;");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
for (States state : list) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
out.println("&lt;tr&gt;&lt;td&gt;" + state.getAbbrev() +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
"&lt;/td&gt;&lt;td&gt;" + state.getName() +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
"&lt;/td&gt;&lt;/tr&gt;");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
}<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
out.println("&lt;/table&gt;");</td>
          </tr>
        </tbody>
      </table>
      <br>
This uses a predefined query to retrieve all rows from the table and
then display them in a simple formatted HTML table.</li>
  </ol>
  <li>Run the project</li>
  <ol>
  </ol>
  <ol>
    <li>Right click on the project, select "Properties" and
change the "Relative URL" to "/ShowStates". This is the exact URL that
you specified earlier.<br>
      <br>
      <img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jpa-run.png" /></li>
    <li>Right-click on the project and select "Run" to see the
following output:<br>
      <br>
      <img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jpa-output.png" /><br>
      <br>
    </li>
  </ol>
</ol>
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 ;-)<br>
<br>
Finally, lets look at the structure of the generated WAR file:<br>
<br>
<img alt="" src="http://blogs.sun.com/arungupta/resource/images/nb68m1-jpa-dir-structure.png" /><br>
<br>
It's very clean - no "web.xml", only the relevant classes and
"persistence.xml".<br>
<br>
Also refer to other <a href="http://blogs.sun.com/arungupta/tags/javaee6">Java EE 6
blog entries</a>. A future blog entry will show how to use JSF
2.0 instead of Servlet for displaying the results.<br>
<br>
Please leave suggestions on other TOTD that
you'd like to see.
A complete archive of all the tips is available <a href="http://blogs.sun.com/arungupta/tags/totd">here</a>.
<br>
<br>
<small>Technorati: <a href="http://technorati.com/tags/totd">totd</a>
<a href="http://technorati.com/tags/glassfish">glassfish</a>
<a href="http://technorati.com/tags/v3">v3</a> <a href="http://technorati.com/tags/mysql">mysql</a> <a href="http://technorati.com/tags/javaee6">javaee6</a> <a href="http://technorati.com/tags/servlet3">servlet3</a>
<a href="http://technorati.com/tags/jpa2">jpa2</a> <a href="http://technorati.com/tags/netbeans">netbeans</a></small><br/>PlanetMySQL Voting:
	 <a href="http://planet.mysql.com/entry/vote/?entry_id=20629&vote=1&apivote=1">Vote UP</a> /
	 <a href="http://planet.mysql.com/entry/vote/?entry_id=20629&vote=-1&apivote=1">Vote DOWN</a>]]></content:encoded>
			<wfw:commentRss>http://planetmysql.ru/2009/08/13/totd-93-getting-started-with-java-ee-6-using-netbeans-6-8-m1-glassfish-v3-a-simple-servlet-3-0-jpa-2-0-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

