Alignment to the containers

The Servlet specification is one of the oldest in the Java EE platform. Over a period of time, it has been updated to allow Servlets to integrate with newer parts of the platform, such as CDI, EJB, JAX-RS, and WebSocket.

Aligning Servlets to the CDI container

The Java Servlet 3.1 specification mentions that the resources can be injected into a Servlet. If the web container provider has support for CDI, then the CDI container can inject the contextual objects into a web application using the @javax.inject.Inject annotation.

CDI is available on web containers and the application servers that implement the Java EE 7 Web Profile and Full Profile. The CDI components can just be injected into Servlet on those environments.

An example of the CDI services injected into Servlet is as follows:

@WebServlet(urlPatterns = {"/randomword"})
public class CDIServlet extends HttpServlet {
  @Inject private RandomWordService service;
  
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
    resp.setContentType("text/plain");
    PrintWriter pwriter = resp.getWriter();
    pwriter.printf("This is the class `%s'
" + "The date time is %s
" + "random word = %d
", getClass().getName(), new Date(), service.getNextWord());
    }
  }

Since Servlet 3.1 is part of Java EE 7, there is no need to bootstrap the CDI container as a separate service. The CDI container is available in conforming web containers as can be seen in the preceding example, CDIServlet, which injects a RandomWordService.Alignment Servlet to the EJB container.

References to stateful and stateless EJB can also be injected into Servlets and filters,if they meet either conformance with the Web Profile or Full Profile. The annotation @javax.ejb.EJB can be used in a Java Servlet component to reference an external EJB.

@WebServlet(urlPatterns = {"/cart/*"})
public class ShoppingCartServlet extends HttpServlet {
  @EJB private ShoppingCartServiceBean cartBean;
  
  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
    processRequest( req, res );
    }
  //
  }

In this example code for ShoppingCartServlet, the Servlet container on behalf of the EJB container injects an EJB reference into Servlet during the initialization phase. The internal logic of the application server processes the injection point with the correct representation, which is either a local or remote EJB proxy instance. The result is transparent to developer.

If the web application is running in the same JVM as the EJB implementation, then in order to use such EJB references in a Servlet, the web application must have references to the home and local interfaces in an accompanying JAR file, which is deployed in the WEB-INF/lib directory. These interfaces are usually Java interfaces, and not normally the implementations themselves.

If the web application uses an EJB that is remote, it does not run inside the same JVM, then a JAR file with the necessary home and remote interfaces must be prepared. This JAR file is also placed in the WEB-INF/lib directory of the web application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.136