< 上一课 | 下一课 >

课程 1.4:创建 Web 项目以测试应用程序

课程 1.4 引导您创建 Web 项目以测试应用程序。

开始之前,必须完成课程 1.3。
在本课程中,您将完成下列任务:
  1. 解压缩 EJB3CounterDB.zip
    1. 下载所需数据库(提供 EJB 3.1 计数器项目持久性)。
    2. 下载 EJB3CounterDB 后,展开 EJB3CounterDB > EJB3CounterDB.zip,并双击 EJB3CounterDB.zip。将在外部窗口中打开文件解压缩实用程序。
      • Windows 图标:将数据库解压缩到 WebSphere® Application Server 安装文件夹的 /derby/databases 文件夹中:
      • Linux 图标:将数据库解压缩到 WebSphere Application Server 安装文件夹的 /derby/databases 文件夹中。
        • 授予对此数据库目录的非 root 用户访问权。(最容易的方法是为每个人授予访问权:chmod ugo+x databases。)
        • 授予非 root 用户对已解压数据库的写访问权。(例如,您可以非 root 用户身份进行解压缩,前提是您具有对 databases 目录的访问权)。
      要点: 根据 WebSphere Application Server 的类型, /derby/databases 的缺省位置可能不同。有关缺省安装目录的信息,请参阅创建 WebSphere Application Server
  2. 在 Java™ EE 透视图中,右键单击企业应用程序项目并选择新建 > Web 项目以打开“Web 项目”向导。
  3. 在“Web 项目”页面上,
    1. 项目名称字段中,输入 EJBCounterWebEE6
    2. 项目模板字段中,选择简单
    3. 编程模型字段中,选择 Java EE
    4. 在“部署”页面上,从可用的配置选项列表中,单击部署打开“部署配置”页面。
      • 目标运行时中从下拉框选择 WebSphere Application Developer V8。
      • 清除添加对 WebSphere 绑定和扩展的支持
      • Web 模块版本字段中,选择 3.0
      • EAR 成员资格字段中,单击将项目添加至 EAR
      • EAR 项目名称字段中,确保显示 EJBCounterSampleEE6EAR
    5. 接受其他缺省值并单击完成。如果系统询问您打开相关联的透视图吗?,请单击
  4. 右键单击 EJBCounterWebEE6 项目,并选择新建 > Web 页面
  5. 新建 Web 页面页面的文件名字段中,输入 EJBCount.jsp,确保所选模板为 JSP,然后单击完成
  6. 在 Web 页面编辑器的“源代码”视图中,用以下代码替换所有现有代码,然后按 CTRL+S 以进行保存:
    <%@page session="false"%>
    <HTML>
    <HEAD>
    <TITLE>IBM WebSphere EJB 3.1 and JPA 2.0 Counter Sample</TITLE>
    <BODY bgcolor="cornsilk">
    <H1>EJB 3.1 and JPA 2.0 Counter Sample</H1>
    <P>
    <B>
    This application communicates with the WebSphere Application Server using http requests to increment a singleton EJB 3.1 counter bean which is using a JPA 2.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>
  7. 右键单击 EJBCounterWebEE6 项目,并选择新建 > Servlet
  8. 新建 Servlet 页面的 Java 包字段中,输入 com.ibm.example.websphere.ejb3sample.counter
  9. 类名称字段中,输入 EJBCount,并单击下一步
    创建 Servlet 页面
  10. 名称字段中,输入 EJB Count Servlet。在 URL 映射字段中,编辑现有的映射、突出显示 /EJB Count Servlet 并单击编辑。将模式替换为 /counter,并单击完成
    输入特定于 Servlet 部署描述符的信息。
  11. 添加部署组合件条目:
    1. 右键单击 EJBCounterWebEE6 项目,并选择属性
    2. 选择部署组合件,选择清单条目,并单击添加
      添加部署组合件值
    3. 选择 EJBCounterSampleEE6.jar,并单击完成,然后单击确定
      添加模块依赖项
  12. 展开 EJBCounterWebEE6 > Java 资源 > src > com.ibm.example.websphere.ejb3sample.counter,并双击 EBJCount.java 文件。将在 Java 编辑器中打开此文件。
  13. 用以下代码替换现有代码,然后按 CTRL+S 以进行保存:
    package com.ibm.example.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.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.Servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /**
     * This servlet demonstrates an EJB3 counter bean with JPA.
     */
    
    @WebServlet(
    		description="This servlet demonstrates the various ways to increment EJB 3.1 counter beans.",
    		name="EJB Count Servlet",
    		displayName="EJB Count Servlet",
    		urlPatterns="/counter"
    		)
    public class EJBCount extends HttpServlet {
    
        private static final long serialVersionUID = -5983708570653958619L;
        
        // Use injection to get the ejb
        @EJB private LocalCounter singletonCounter;
        
        public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    		String msg = null;
    		int ejbCount = 0;
    		
    		try {
    			ejbCount = singletonCounter.getTheValue();
    		} 
    		catch (RuntimeException e) {
    			msg = "Error - getTheValue() method on EJB failed!";
            	e.printStackTrace();
    		}
    		msg = "EJB Count value for Singleton 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 = singletonCounter.increment();
    		} 
    		catch (RuntimeException e) {
    			msg = "Error - increment() method on EJB failed!";
            	e.printStackTrace();
    		}
    		msg = "EJB Count value for Singleton Bean with JPA: " + ejbCount;
    		
    		// Set attibutes and dispatch the JSP.
            req.setAttribute("msg", msg);
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/EJBCount.jsp");
            rd.forward(req, res);
    	}
        
    
    }
现在,您已准备好移至“课程 1.5:创建 Web 片段项目以测试应用程序”。
< 上一课 | 下一课 >
指示主题类型的图标 教程课程主题
信息中心的条款和条件 | 反馈

时间戳记图标 最近一次更新时间: 2014 年 4 月 17 日

文件名:exer14ejb31.html