3.1. Understand the purpose and role of JNDI in relation to EJB components
JNDI : Java naming directory
Binding - assigning a name to an EJB object which can be used later.
Lookup - looking up and getting an object of EJB.
3.2. Configure JNDI environment properties
http://docs.oracle.com/javaee/6/tutorial/doc/gipjf.html#girgn
global
java:global[/application name]/module name/enterprise bean name[/interface name]module
java:module/enterprise bean name/[interface name]app
java:app[/module name]/enterprise bean name[/interface name]
3.3. Use JNDI to look up a resource
Accessing an enterprise bean can be done by dependency injection or JNDI lookup.
Example using JNDI lookup:
ExampleRemote example = (ExmapleRemote) InitialContext.lookup("java:global/myApp/ExampleRemote");
3.4. Write code that receives a resource reference through injection
@EJB - used to inject other EJB reference (on fields or on methods).
@Resource - used to inject datasource or singleton services like sessionContext, timerService etc.
We will see a complete code example in the followings sections (3.5- 3.7) and step by step we will create a javaEE application. This simple application is compound by:
- Web client project : with a servlet and JSP
- EJB server project :with sessionEJB and its interface.
The purpose will be create an application that allows a user selecting one color from a dropdown list. When submitting the selection a servlet will be executed and will call an EJB bean method. The bean method will return the color that will be displayed in the screen.
3.5. Create a session bean client
We are going to define an stateless session bean with a method that will evaluate a number of options and it will return the hex code corresponding to one color:
If input is 1 -- > color blue
If input is 2 -- > color red
If input is 3 -- > color yellow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import javax.ejb.EJB; | |
import javax.ejb.Stateless; | |
@Stateless | |
@EJB(name="java:global/ExampleBeans/TestBean", beanInterface=TestBeanLocal.class) | |
public class TestBean implements TestBeanLocal { | |
/** | |
* Default constructor. | |
*/ | |
public TestBean() { | |
// TODO Auto-generated constructor stub | |
} | |
public String testColor(String color) { | |
String colorStyle = "#ffffff"; | |
switch (color) { | |
case "1": | |
colorStyle = "#0000ff"; | |
break; | |
case "2": | |
colorStyle = "#ff0000"; | |
break; | |
case "3": | |
colorStyle = "#ffff00"; | |
break; | |
} | |
return colorStyle; | |
} | |
} |
3.6. Create a session facade
http://www.oracle.com/technetwork/java/sessionfacade-141285.html
Session facade provides a level of abstraction between the client and the business object. It manages the interactions between client and business services.
For the session bean defined in the previous section we will define an interface, that will be used by the client to access to our bean.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package example; | |
import javax.ejb.Local; | |
import javax.ejb.Remote; | |
@Local | |
public interface TestBeanLocal { | |
public String testColor(String color); | |
} |
3.7. Use dependency injection to locate an EJB
Now, we will have to define the client that will call the bean (Servlet and JSP).
JSP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" | |
pageEncoding="ISO-8859-1"%> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> | |
<title>Insert title here</title> | |
<style> | |
p { | |
border: 1px solid grey; | |
padding: 10px; | |
margin: 30px; | |
} | |
</style> | |
</head> | |
<body> | |
<form action="myColor"> | |
<p> | |
SELECT ONE COLOR <select id="list" name="color"> | |
<option value="0" ${color == '0' ? 'selected' : ''}>SELECT | |
<option> | |
<option value="1" ${color == '1' ? 'selected' : ''}>BLUE</option> | |
<option value="2" ${color == '2' ? 'selected' : ''}>RED</option> | |
<option value="3" ${color == '3' ? 'selected' : ''}>YELLOW</option> | |
</select> <input type="submit" value="SUBMIT" /> <br /> | |
</p> | |
<p> | |
<input type="text" value="${colorStyle}" /> <br /> | |
</p> | |
<p> | |
<input type="color" value="${colorStyle}" /> | |
</p> | |
</form> | |
</body> | |
</html> |
Servlet class
The servlet class will have a method doGet, that will be called everytime the button submit is clicked.
The dependency injection to locate the bean is applied by annotation:
@EJB
TestBeanLocal colorBean;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package color; | |
import java.io.IOException; | |
import javax.ejb.EJB; | |
import javax.servlet.RequestDispatcher; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import example.TestBean; | |
import example.TestBeanLocal; | |
/** | |
* Servlet implementation class ColorSelectorServlet | |
*/ | |
@WebServlet("/myColor") | |
public class ColorSelectorServlet extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
@EJB | |
TestBeanLocal colorBean; | |
/** | |
* Default constructor. | |
*/ | |
public ColorSelectorServlet() { | |
// TODO Auto-generated constructor stub | |
} | |
/** | |
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse | |
* response) | |
*/ | |
protected void doGet(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
String color = request.getParameter("color"); | |
String colorStyle = "#ffffff"; | |
if (color != null) { | |
colorStyle = colorBean.testColor(color);//call the bean method | |
} | |
request.setAttribute("colorStyle", colorStyle); | |
request.setAttribute("color", color); | |
RequestDispatcher rd = request | |
.getRequestDispatcher("/colorSelector.jsp"); | |
rd.forward(request, response); | |
} | |
/** | |
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse | |
* response) | |
*/ | |
protected void doPost(HttpServletRequest request, | |
HttpServletResponse response) throws ServletException, IOException { | |
// TODO Auto-generated method stub | |
} | |
} |
After deploying our application (web and EJB) in the server. When entering this url in the browser: http://localhost:8080/ColorWeb/myColor, the page is open:
When a color is selected the corresponding hex code and color are shown in the screenNote: The details about environment configuration are described in this post : http://javawithbreakfast.blogspot.com/2015/11/configuring-ejb-environment.html
OCEJBCD (SCBCD) - 1Z0-895 - Enterprise JavaBeans Developer Certification
No comments:
Post a Comment