A session bean has limited value by itself. To be useful it needs to be used by a client such as a servlet or JSF page. In this recipe we will see how to use dependency injection to use a session bean in a servlet.
The essential steps to access a session EJB using dependency injection include:
We will be developing a HyperText Transfer Protocol (HTTP) based servlet named SalutationServlet
. This servlet will use the Salutation
EJB's methods and display their return value. Create a package in the WAR module called servlet
. Add the servlet to this package.
The servlet consists of a class declaration and three methods:
The servlet begins with the @WebServlet annotation then declares an instance of the Servlet EJB and uses it in the processRequest
method.
package servlet;
import javax.ejb.EJB;
import packt.Salutation;
@WebServlet(urlPatterns = {"/SalutationServlet"})
public class SalutationServlet extends HttpServlet {
@EJB
private Salutation salutation;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet SalutationServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>" + salutation.getFormalSalutation("Sherlock Holmes") + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.flush();
out.close();
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
Enter the URL as shown in the following screenshot into a browser. When executed, the salutation Dear Sherlock Holmes will be displayed in the browser.
To provide a reference to an EJB in a servlet we used the @EJB annotation to inject the bean. However, before we could use the annotation two import statements were required. The first one was for the annotation and the second one is for the Salutation
EJB.
import javax.ejb.EJB; import packt.Salutation;
The declaration of the servlet began with @WebServlet annotation. The @WebServlet is a class level annotation marking the class as an HTTP servlet. The urlPatterns parameter specifies the URL pattern which maps to this servlet. This pattern is important because this is how a user of the servlet is able to locate it on the server.
@WebServlet(urlPatterns = {"/SalutationServlet"}) public class SalutationServlet extends HttpServlet {
The salutation variable was declared as a field of the servlet. The @EJB annotation immediately preceded the variable declaration and effected dependency injection. This allows the EJB container to support the EJB.
@EJB private Salutation salutation;
HTTP Servlets typically respond to doGet
and doPost
HTTP commands. The doGet
and doPost
methods are invoked depending on whether a GET or POST HTTP command is issued. In this example, both of these methods called the processRequest
method using the common servlet logic in the processRequest
method.
The processRequest
method used standard servlet code to generate the HTML response sent back to the browser. Of particular interest to us was the use of the salutation object. The getFormalSalutation
method was invoked and its return value was sent forward to the browser.
... out.println("<h1>" + salutation.getFormalSalutation("Sherlock Holmes") + "</h1>"); ...
3.145.107.100