Download Tutorial for EJB Module (Session Bean) Creating the Java Class

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Tutorial for EJB Module (Session Bean)
Creating the Java Class Library
In this section you will create a Java Class Library project that will contain the remote interface
for the EJB. The remote interface behaves as an API for the EJB that is used by clients to
communicate with the EJB.
The library JAR is easy to distribute to any clients that may need to call the EJB. Clients that
want to access the EJB only need to add the library JAR to the project classpath. The EJB
implementation uses the same JAR to implement the interface.
Choose File > New Project and select Java Class Library in the Java category. Click Next.
Type ConverterRemoteInterface for the Project Name. Click Finish.
When you click Finish, the IDE creates a Java Class Library project. In the next section you will
create a Java EE enterprise application and an EJB module. You will then use a wizard to create a
session bean and the remote interface for your session bean in the Class Library project. The
application client will access the session bean via the interface in the class library.
Coding the EJB Module
Coding enterprise beans is easy. The IDE takes care of all the implementation details for you, so
you can concentrate on coding the business logic of your EJB module.
Creating the EJB Module Project
For this example, we will create an EJB Module project called EJBConverterV2.
Choose File > New Project (Ctrl-Shift-N) and select the EJB Module template from the Enterprise
category. Click Next.
1.
Name the EJB Module EJBConverterV2 and specify a location for the project and Select
the Glassfish application server and click Finish.
Coding the Session Bean
Now let's create a session bean that will implement the two following functions: dollarToYen and
yenToEuro.
1.
2.
In the Projects window, right-click the EJBConverter project node and choose New >
Session Bean. Name the session bean Converter, and place it in the ejb package. Set
the bean to be stateless and to have the remote interface and select the project
ConverterRemoteInterface. Then click Finish. The IDE creates the bean and opens
its bean class in the Source Editor.
Right-click anyway in editor and choose >"Insert code Methods > Add Business Method”
and specify the following information for the first function dollarToYen :
Element
Value
Method’s name dollarToYen
Return type
java.math.BigDecimal
Paramater
java.math.BigDecimal dollars
Exception
Use in Interface Select “remote”
3.
Right-click anyway in editor and choose >" Insert code > Add Business Method" and
specify the following information for the second function yenToEuro :
Element
Value
Method's name yenToEuro
Return type
java.math.BigDecimal
Paramater
java.math.BigDecimal yen
Exception
Use inInterface Select “remote”
4.
In the Source Editor, edit the dollarToYen business method and add the following code :
public BigDecimal dollarToYen(java.math.BigDecimal dollars) {
BigDecimal yenRate = new java.math.BigDecimal("121.6000");
BigDecimal result = dollars.multiply(yenRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
5.
In the Source Editor, edit the yenToEuro business method and add the following code :
public BigDecimal yenToEuro(BigDecimal yen) {
BigDecimal euroRate = new BigDecimal("0.0077");
BigDecimal result = yen.multiply(euroRate);
return result.setScale(2, BigDecimal.ROUND_UP);
}
6.
Expand the (source package) and the package ejb node. In this package, you have the
java file of the Session Bean and the remote interface is in the project
ConverterRemoteInterface .
7.
Expand the (Entreprise Beans) and the bean ConverterBean. In this description, you
have the remote methods which can be invoked.
Building and Deploying the EJB Module
The IDE has already configured the deployment descriptors for the session.
Deploy the EJB Module
The EJB Module should be deployed in the Application server.
1.
2.
In the Projects window, right-click the EJBConverter project and choose Build Project.
In the Projects window, right-click the EJBConverter project and choose Deploy Project.
The build Project action generates the jar file for the EJB Module with all descriptors. The
application server will be started and the archive file and the descriptor are deployed to the
server. (Select the services Window – Expand the application servers Node, GlassFish node
and EJBModules Node. Click. The EJBConverterV2 should appear from this node.
3.
4.
Right-click on the EJB Module Converter and select undeploy. The EJBConverterV2 will be
removed from the server.
Stop the application server and develop the web application client of the session bean
converter.
Coding the Web Application
Now we need to code a Web application that will provide the user interface for our converter
session bean. The Web application contains one servlet that call the EJB methods with input given
by a user.
Creating the Web Application Project
For this example, we will create a Web Application project
Choose File > New Project (Ctrl-Shift-N) and select the Web Application template from the Web
category. Click Next.
1.
Name the Web Application WebConverterV2 and specify a location for the project and
Select the application server and click Finish.
Coding the Servlet
For the final step, we will add a servlet to the Web application that lets you input for an amount
and (1) convert this amount of dollars in yen with the remote call of the method dollarsToYen
and (2) convert the same amount of yen in euros with the remote call of the method yenToEuro.
1.
2.
In the Projects window, right-click the WebConverterV2 node and choose New > Servlet.
Name the servlet ConverterServlet and put it in the web package.
In the Source Editor, right-click anywhere in the servlet class' body and choose
Enterprise Resources > Call Enterprise Bean. Select the Converter session bean.
The following line will be added in the Servlet.
@EJB
private ConverterRemote converterBean;
3.
Edit the processRequest method. Cut and paste the bold and italic code in the
processRequest method:
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ConverterServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ConverterServlet at " + request.getContextPath ()
+ "</h1>");
out.println("<h1><b><center>Converter</center></b></h1>");
out.println("<hr>");
out.println("<p>Enter an amount to convert:</p>");
out.println("<form method=\"get\">");
out.println("<input type=\"text\"name=\"amount\" size=\"25\">");
out.println("<br>");
out.println("<p>");
out.println("<input type=\"submit\" value=\"Submit\">");
out.println("<input type=\"reset\" value=\"Reset\">");
out.println("</form>");
String amount = request.getParameter("amount");
if ( amount != null && amount.length() > 0 ) {
try {
java.math.BigDecimal d = new java.math.BigDecimal(amount);
out.println("<p>");
out.println("<p>");
out.println(amount + " Dollars are " + converterBean.dollarToYen(d) + "
Yen.");
out.println("<p>");
out.println(amount + " Yen are " + converterBean.yenToEuro(d) + "
Euro.");
} catch (Exception e){
out.println("Cannot execute EJB!");
}
}
out.println("</body>");
out.println("</html>");
out.close();
}
Building and Deploying the Web Application
The IDE has already configured the web deployment descriptors.
Setting the Default Web Page for the Program
By default, a Web application displays its index.jsp page when it is run. Since our index.jsp
is blank, we want the WebConverterv2 project to display the ConverterServlet servlet
instead.
1.
2.
In the Projects window, right-click the ConverterAp project and choose Properties. Then
click Run in the left panel.
Type /ConverterServlet in the Relative URL field.
Deploy the Web Application
The Web Application should be deployed in the Application server.
1.
2.
In the Projects window, right-click the WebConverterv2 project and choose Build Project.
In the Projects window, right-click the WebConverterv2 project and choose Deploy
Project.
Running the Application
1.
Right-click the WebConverterv2 project and choose Run Project.
You should see the page with a form. When you type and amount and press Enter, the page
should display the conversion of this amount in yens and in euros.
Stopping the Application
1.
2.
In the Runtime Window, Right-click the Application server and select start/stop server.
Select the button stop server.
Exit the IDE.
Related documents