< Previous | Next >

Lesson 1.4: Create a web project to test your application

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
  1. Extract the EJBCounterDB
    1. Import the required database: Import resources. Save the file to your EJBCounterWeb project in your workspace.
    2. Expand EJBCounterDB > EJBCounterDB.zip and double click EJBCounterDB.zip.
      • Windows icon: Extract the database into your /derby/databases folder of your WebSphere® Application Server install folder:
      • Linux icon: 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.
  2. In the Java™ EE perspective, right-click your enterprise application project and select New > Web Project to open the web project wizard.
  3. In the Web Project page, in the Project name field, type EJBCounterWeb.
  4. In the Project Templates field, select Simple.
  5. In the Programming Model field, select Java EE.
  6. 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.
  7. Accept the other defaults and click Finish. If asked to Open associated perspective?, click No.
  8. Right click the EJBCounterWeb project, and select New > Web page.
  9. On the New Web page, in the File name field, type EJBCount.jsp.
  10. 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>
  11. Right click the EJBCounterWeb project, and select New > servlet.
  12. On the New Servlet page, in the Java package field, type com.ibm.example.websphere.ejb3sample.counter.
  13. In the Class name field, type EJBCount, and Click Next:
    Create Servlet page
  14. 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:
    Enter Servlet deployment descriptor specific information.
  15. Right click the EJBCounterWeb project, and select Properties.
  16. Select Deployment Assembly, select Manifest Entries, and click Add:
    J2EE Module dependencies
  17. Select EJBCounterSampleEE6.jar, and click Finish, and then click OK
    Add module dependency
  18. Expand EJBCounterWeb > Java Resources > src > com.ibm.example.websphere.ejb3sample.counter, and double click the EBJCount.java file. It opens in the Java editor.
  19. 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);
    	}
        
    
    }
  20. In the Enterprise Explorer view, expand the EJBCounterWeb > Deployment Descriptor > Java Resources/src > com.ibm.example.websphere.ejb3sample.counter, and right-click the EJBCount.java file, and select Run > Run on Server
  21. The counter application opens in a web browser:
    EBJ 3.0 web page
You have now completed the EJB 3.0 Counter tutorial.
< Previous | Next >

Feedback