Tuesday May 19, 2009
TOTD #82: Getting Started with Servlet 3.0 and EJB 3.1 in Java EE 6 using NetBeans 6.7
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.



| public String
sayHello(String name) { return "Hello " + name; } |
| package server; import javax.ejb.Stateless; /** * @author arungupta */ @Stateless public class HelloEJB { public String sayHello(String name) { return "Hello " + name; } } |
| @WebServlet(urlPatterns="/hello") |
| extends HttpServlet |
| @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>"); } |
| package server; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * @author arungupta */ @WebServlet(urlPatterns="/hello") public class HelloServlet extends HttpServlet { @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>"); } } |


Posted by Arun Gupta in General | Comments[15]
|
|
|
|
|
Today's Page Hits: 2741
Total # blog entries: 1002