Lesson 1.4 leads you through the creation of a web project
to test your application.
Before you begin, you must complete Lesson 1.3.
In this lesson you will
- Create a web project, EJBCounterWeb
- Create a web page, EJBCount.jsp
- Create a Servlet, EJBCount.java
- Run the Servlet to test your application
- Extract the EJBCounterDB
- Import the required database: Import resources. Save the file to your EJBCounterWeb project in
your workspace.
- Expand and double click EJBCounterDB.zip.
: Extract the database into your /derby/databases folder of your WebSphere® Application Server install folder:
: Extract the database into your /derby/databases folder of your WebSphere Application Server install folder.- Give your non-root user access to the databases directory. (The
easiest way is to give everyone access: chmod ugo+x databases.
- Give your non-root user write-access to the extracted database.
(For example, you can extract as the non-root user, provided you
have access to the databases directory).
Important: Depending on the
type of WebSphere Application
Server, the default location of your
/derby/databases may differ. For information about default installation directories,
see
Creating a WebSphere Application
Server.
- In the Java™ EE perspective, right-click your enterprise application project
and select to open the web project wizard.
- In the Web Project page, in the Project name field, type EJBCounterWeb.
- In the Project Templates field,
select Simple.
- In the Programming Model field,
select Java EE.
- On the deployment page, from the list of available configuration
options, click Deployment to open the Deployment
configuration page.
- In the Target runtime select WebSphere Application Developer v7 or v8
from the drop-down box.
- Clear Add support for WebSphere bindings and extensions.
- In the Web module version field, select 3.0
- In the EAR membership field, click Add project to an EAR..
- In the EAR project name field, ensure that EJBCounterWebEAR appears.
- Accept the other defaults and click Finish. If asked to Open associated perspective?, click No.
- Right click the EJBCounterWeb project,
and select .
- On the New Web page, in the File name field, type EJBCount.jsp.
- In the Source view of the Web page editor, replace all
the existing code with this code, and press CTRL+S to save:
<%@page session="false"%>
<HTML>
<HEAD>
<TITLE>IBM WebSphere EJB3 and JPA1 Counter Sample</TITLE>
<BODY bgcolor="cornsilk">
<H1>EJB 3.0 and JPA 1.0 Counter Sample</H1>
<P>
<B>
This application communicates with the WebSphere Application Server using http requests to increment a stateless EJB 3.0 counter bean which is using a JPA 1.0 entity (ie. keeps a persistent counter in a Derby database table).
</B>
<FORM METHOD=POST ACTION="counter">
<BR/>
<%
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires",0);
String msg = (String) request.getAttribute("msg");
if (msg == null) msg = "";
%>
<B>Click on the Increment button to increment the count</B>
<BR/><BR/>
<INPUT TYPE=SUBMIT VALUE="Increment">
</FORM>
<H3><%=msg%></H3>
</BODY>
</HTML>
- Right click the EJBCounterWeb project,
and select .
- On the New Servlet page, in the Java package field, type com.ibm.example.websphere.ejb3sample.counter.
- In the Class name field, type EJBCount, and Click Next:
- In the Name field, type EJB Count Servlet. In the URL mappings field, edit the existing mapping, highlight /EJB Count Servlet
and click Edit. Replace the pattern it with /counter, and click Finish:
- Right click the EJBCounterWeb project,
and select Properties.
- Select Deployment Assembly, select Manifest Entries, and click Add:
- Select EJBCounterSampleEE6.jar,
and click Finish, and then click OK
- Expand , and double click the EBJCount.java file. It opens in the Java editor.
- Replace the existing code with the following code, and
press CTRL+S to save:
package com.ibm.websphere.ejb3sample.counter;
// This program may be used, executed, copied, modified and distributed
// without royalty for the purpose of developing, using, marketing, or distributing.
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* This servlet demonstrates an EJB3 counter bean with JPA.
*/
public class EJBCount extends HttpServlet {
private static final long serialVersionUID = -5983708570653958619L;
// Use injection to get the ejb
@EJB private LocalCounter statelessCounter;
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String msg = null;
int ejbCount = 0;
try {
ejbCount = statelessCounter.getTheValue();
}
catch (RuntimeException e) {
msg = "Error - getTheValue() method on EJB failed!";
e.printStackTrace();
}
msg = "EJB Count value for Stateless Bean with JPA: " + ejbCount;
// Set attributes and dispatch the JSP.
req.setAttribute("msg", msg);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/EJBCount.jsp");
rd.forward(req, res);
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String msg = null;
int ejbCount = 0;
try {
ejbCount = statelessCounter.increment();
}
catch (RuntimeException e) {
msg = "Error - increment() method on EJB failed!";
e.printStackTrace();
}
msg = "EJB Count value for Stateless Bean with JPA: " + ejbCount;
// Set attibutes and dispatch the JSP.
req.setAttribute("msg", msg);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/EJBCount.jsp");
rd.forward(req, res);
}
}
- In the Enterprise Explorer view, expand the , and right-click the EJBCount.java file, and select
- The counter application opens in a web browser:
You have now completed the EJB 3.0 Counter tutorial.