< 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. Unzip the EJBCounterDB
    1. After you have imported the EJBCounterDB, expand EJBCounterDB > EJBCounterDB.zip and double click on 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 unzipped database. (The easiest way is to unzip as the non-root user, which will work provided the user has access to the databases directory).
  2. In the Java™ EE perspective, select File > New > Other > Dynamic Web Project.
  3. In the Dynamic Web Project page, in the Project name field, type EJBCounterWeb.
  4. Accept the other defaults and click Finish. If asked to Open associated perspective?, click No.
  5. Right click the EJBCounterWeb project, and select New > Web page.
  6. On the New Web page, in the File name field, type EJBCount.jsp.
  7. 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>
  8. Right click the EJBCounterWeb project, and select New > servlet.
  9. On the New Servlet page, in the Java package field, type com.ibm.websphere.ejb3sample.counter.
  10. In the Class name field, type EJBCount, and Click Next:
    Create Servlet page
  11. On the Enter Servlet deployment descriptor specific information page, in the Name field, type EJB Count Servlet. In the URL mappings field, edit the existing mapping, and replace it with /counter, and click Finish:
    Enter Servlet deployment descriptor specific information.
  12. Right click the EJBCounterWeb project, and select Properties.
  13. Select J2EE Module Dependencies, and click the EJBCounterSample.jar file, and click Okay:
    J2EE Module dependencies
  14. Expand EJBCounterWeb > Java Resources > src > com.ibm.websphere.ejb3sample.counter, and double click the EBJCount.java file. It opens in the Java Editor.
  15. 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);
    	}
        
    
    }
  16. In the Enterprise explorer view, expand the EJBCounterWeb > Deployment Descriptor > Java Resources/src > com.ibm.websphere.ejb3sample.counter, and right-click the EJBCount.java file, and select Run > Run on Server
  17. The counter application opens in a Web browser:
    EBJ 3.0 web page
Congratulations! You have completed the EJB 3.0 Counter application.
< Previous | Next >

Feedback