< 이전 | 다음 >

학습 1.4: 애플리케이션을 테스트할 웹 프로젝트 작성

학습 1.4에서는 애플리케이션 테스트를 위해 웹 프로젝트를 작성하는 단계를 설명합니다.

시작하기 전에 학습 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 폴더에 데이터베이스의 압축을 푸십시오.
        • 데이터베이스 디렉토리에 대한 비루트 사용자 액세스를 제공하십시오. 가장 쉬운 방법은 모든 사용자에게 액세스 권한 chmod ugo+x databases를 제공하는 것입니다.
        • 압축을 푼 데이터베이스에 대한 비루트 사용자 쓰기 액세스를 제공하십시오. (루트가 아닌 사용자로 압축을 풀 수 있으며, 이는 databases 디렉토리에 액세스 권한이 있는 경우에 수행됩니다.)
      중요사항: WebSphere Application Server의 유형에 따라 /derby/databases의 기본 위치가 다를 수 있습니다. 기본 설치 디렉토리에 대한 정보는 WebSphere Application Server 작성을 참조하십시오.
  2. Java™ EE 퍼스펙티브에서 엔터프라이즈 애플리케이션 프로젝트를 마우스 오른쪽 단추로 클릭하고 새로 작성 > 웹 프로젝트를 선택하여 웹 프로젝트 마법사를 여십시오.
  3. 웹 프로젝트 페이지에서 다음을 수행하십시오.
    1. 프로젝트 이름 필드에 EJBCounterWebEE6를 입력하십시오.
    2. 프로젝트 템플리트 필드에서 단순을 선택하십시오.
    3. 프로그래밍 모델 필드에서 Java EE를 선택하십시오.
    4. 배치 페이지의 사용 가능한 구성 옵션 목록에서 배치를 클릭하여 배치 구성 페이지를 여십시오.
      • 대상 런타임의 드롭 다운 상자에서 WebSphere Application Developer v8을 선택하십시오.
      • WebSphere 바인딩 및 확장기능용 지원 추가를 선택 취소하십시오.
      • Web 모듈 버전 필드에서 3.0을 선택하십시오.
      • EAR 멤버십 필드에서 EAR에 프로젝트 추가를 클릭하십시오.
      • EAR 프로젝트 이름 필드에서 EJBCounterSampleEE6EAR이 표시되는지 확인하십시오.
    5. 다른 기본값을 승인하고 완료를 클릭하십시오. 연관된 퍼스펙티브를 여시겠습니까?가 표시되면 아니오를 클릭하십시오.
  4. EJBCounterWebEE6 프로젝트를 마우스 오른쪽 단추로 클릭한 후, 새로 작성 > 웹 페이지를 선택하십시오.
  5. 새 웹 페이지 페이지의 파일 이름 필드에 EJBCount.jsp를 입력하고 JSP가 선택된 템플리트인지 확인하고 완료를 클릭하십시오.
  6. 웹 페이지 편집기의 소스 보기에서 모든 기존 코드를 이 코드로 바꾸고 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 프로젝트를 마우스 오른쪽 단추로 클릭하고 새로 작성 > 서블릿을 선택하십시오.
  8. 새 서블릿 페이지의 Java 패키지 필드에 com.ibm.example.websphere.ejb3sample.counter를 입력하십시오.
  9. 클래스 이름 필드에 EJBCount를 입력하고 다음을 클릭하십시오.
    서블릿 작성 페이지
  10. 이름 필드에 EJB Count Servlet을 입력하십시오. URL 맵핑 필드에서 기존 맵핑을 편집하고 /EJB Count Servlet을 강조표시하고 편집을 클릭하십시오. /counter로 변경한 후, 완료를 클릭하십시오.
    서블릿 배치 디스크립터 특정 정보 입력
  11. 다음을 수행하여 배치 어셈블리 항목을 추가하십시오.
    1. EJBCounterWebEE6 프로젝트를 마우스 오른쪽 단추로 클릭한 후, 특성을 선택하십시오.
    2. 배치 어셈플리를 선택하고 Manifest 항목을 선택한 다음, 추가를 클릭하십시오.
      배치 어셈블리 값 추가
    3. EJBCounterSampleEE6.jar을 선택하고 완료를 클릭한 다음, 확인을 클릭하십시오.
      모듈 종속성 추가
  12. EJBCounterWebEE6 > Java Resources > 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 애플리케이션을 테스트하는 웹 단편 프로젝트 작성으로 이동할 수 있습니다.
< 이전 | 다음 >
주제 유형을 표시하는 아이콘 학습서 학습 주제
Information Center의 이용 약관 | 피드백

시간소인 아이콘 마지막 업데이트 날짜: 2014-05-22

파일 이름: exer14ejb31.html