center of tech

EJB 3.1 (JSR
318) and Servlet 3.0 (JSR 315)
are the two new JSRs in Java EE 6 (JSR 316).
The EJB 3.1 specification provides multiple new features such as WAR
packaging, Optional
Local Business Interfaces, EJB.lite, Portable
Global JNDI Names, Singleton
Session Beans
(Container-managed and Bean-managed concurrency), Application
Initialization and Shutdown events, Timer Service enhancements,
Simple/Light-weight Asynchrony, and many other features defined in the specification.
The
Servlet 3.0 specification is an update to Servlet 2.5 and focuses on
ease-of-use. It also adds several new features such as “web.xml”
free
deployment (mostly), Dynamic Registration of
servlets/filters,
Pluggability
of frameworks using “web-fragment.xml”, Asynchronous API,
Security
enhancements (Constraints via annotations, programmatic
container authentication and logout), and several other miscellaneous
additions like default error page, file upload, etc.
GlassFish v3
provides the most complete implementation of EJB 3.1 and Servlet 3.0
along with other Java EE 6 specifications. This Tip Of The Day (TOTD) will show
how to create a simple EJB and invoke it from a Servlet, all in a
deployment-descriptor free way.

and click on “Next”.

Give the class name as “HelloEJB” and package as “server” as shown
below:

and click on “Finish”.
| public String sayHello(String name) { return “Hello ” + name; } |
to
the class. And can you believe it, that’s your complete EJB ready to be
deployed and that too in a WAR file - the beauty of Java EE 6. The
complete class looks like:
| package server;
import javax.ejb.Stateless; /** |
| @WebServlet(urlPatterns=”/hello”) |
| extends HttpServlet |
to the class.
| @EJB HelloEJB ejbClient; |
| @Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException { res.setContentType(”text/html”); res.getOutputStream().print(”<h1>Hosted at: ” + req.getContextPath() + “</h1>”); res.getOutputStream().print(”<h2>” + ejbClient.sayHello(”Duke”) + “</h2>”); } |
and again Shift+Command+I to fix the imports. The complete class looks
like:
| package server;
import java.io.IOException; /** @Override |
That
completes the project creation. Now lets make our application
deployment descriptor free by expanding “WEB-INF” directory and
deleting “sun-web.xml” and “web.xml”. Java EE 6 makes the deployment
descriptors optional by introducing equivalent annotations.
Lets run the project by right-click on the
project and select “Run”. The web application is deployed to GlassFish
v3 Preview 47b and “http://localhost:8080/WebApplication1″ shows the
default “index.jsp” created by the IDE.
Our servlet is accessible at
“http://localhost:8080/WebApplication1/hello” and shows the output as:

The directory of the generated WAR file looks like:

As evident “WEB-INF/classes” has only two POJO classes and yet this is
a Java EE 6 application.
So we created a trivial Java EE 6 application using Servlet 3 and EJB
3.1 APIs and deployed successfully on GlassFish v3 Preview 47b using
NetBeans 6.7.
Please leave suggestions on other TOTD (Tip Of The Day) that
you’d like to see.
A complete archive of all the tips is available here.
Technorati: totd
glassfish
v3 javaee6 servlet3
ejb3.1
netbeans
Source/Kaynak : http://blogs.sun.com/arungupta/entry/totd_81_getting_started_with