Last Modified : 2010.02.23

¹«»óÅ ¼¼¼Ç ºó (Stateless Session Bean)

HelloWorld ¿¹Á¦ ½ÇÇà

¿öÅ©½ºÆäÀ̽º¸¦ C:/ejb ·Î ¼±ÅÃÇϰí ÀÌŬ¸³½º¸¦ ½ÇÇàÇÕ´Ï´Ù.
helloworld ¶ó´Â »õ·Î¿î ÇÁ·ÎÁ§Æ®¸¦ »ý¼ºÇÕ´Ï´Ù.
helloworld ÇÁ·ÎÁ§Æ®¿¡ weblogic.jar ¸¦ Add External JARs.. ¹öưÀ» ÀÌ¿ëÇÏ¿© Ãß°¡ÇÕ´Ï´Ù.

1. ¿ø°Ý ÀÎÅÍÆäÀ̽º ÀÛ¼º

HelloWorld.java


package kr.co.hanbitbook.ejb.examples;

import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface HelloWorld extends EJBObject {
	public String helloWorld() throws RemoteException;
}
2. Ȩ ÀÎÅÍÆäÀ̽º ÀÛ¼º

HelloWorldHome.java


package kr.co.hanbitbook.ejb.examples;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface HelloWorldHome extends EJBHome {
	public HelloWorld create() throws CreateException, RemoteException;
}
3. ºó Ŭ·¡½º ÀÛ¼º

HelloWorldBean.java

package kr.co.hanbitbook.ejb.examples;

import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

public class HelloWorldBean implements SessionBean {
	private SessionContext sc;
	
	public void setSessionContext(SessionContext sc) {
		this.sc = sc;
	}
	
	public String helloWorld() {
		return "Hello World";
	}
	
	public void ejbCreate() {}
	public void ejbRemove() {}
	public void ejbActivate() {}
	public void ejbPassivate() {}
}
4. ¹èÄ¡ µð½ºÅ©¸³ÅÍ ÀÛ¼º(À¥·ÎÁ÷8.1ÀÇ À¥·ÎÁ÷ ºô´õ ÀÌ¿ë)

À¥·ÎÁ÷ ºô´õ¸¦ ½ÇÇàÇÕ´Ï´Ù.
À¥·ÎÁ÷ ºô´õ ½ÇÇà
À¥·ÎÁ÷ ºô´õ Ãʱâ½ÇÇàÈ­¸é
File, Open ¸¦ Ŭ¸¯ÇÑ ÈÄ, ¾Æ·¡¿Í °°ÀÌ °æ·Î¸¦ ¼±ÅÃÇÕ´Ï´Ù.
À¥·ÎÁ÷ ºô´õ °æ·Î¼±ÅÃ
Open ¸¦ Ŭ¸¯ÇÏ¿© ¹èÄ¡ µð½ºÅ©¸³Å͸¦ »ý¼ºÇÒ °ÍÀÎÁö ¹¯´Â ´ëÈ­»óÀÚ¿¡¼­ ¿¹¸¦ Ŭ¸¯ÇÕ´Ï´Ù.
À¥·ÎÁ÷ ºô´õ ´ëÈ­»óÀÚ
À¥·ÎÁ÷ ºô´õ HelloWorldBean»ý¼º
À¥·ÎÁ÷ ºô´õ GeneralÅÇ
À¥·ÎÁ÷ ºô´õ classesÅÇ
À¥·ÎÁ÷ ºô´õ °á°ú¹°

5. EJB jar ÆÄÀÏ »ý¼º

À¥·ÎÁ÷ ºô´õ EJB jar ÆÄÀÏ»ý¼º
¶Ç´Â ¸í·ÉÇÁ·ÒÇÁÆ®¿¡¼­ C:\ejb\helloworld\bin ·Î À̵¿ ÈÄ ¾Æ·¡¿Í °°ÀÌ jar ¸¦ ÀÌ¿ëÇØ¼­ ¸¸µé ¼ö ÀÖ½À´Ï´Ù.
jar cvf helloworld.jar *.*

6. ¹èÄ¡

À¥·ÎÁ÷ ¼­¹ö¸¦ ½ÃÀÛÇÑ ÈÄ mydomain - Deployments - EJB Deployments ¸Þ´º¸¦ ¼±ÅÃÇÕ´Ï´Ù.
Deploy a new EJB Module... ¸¦ Ŭ¸¯ÇÕ´Ï´Ù.
À̾îÁö´Â È­¸é¿¡¼­ ¾Æ·¡¿Í °°ÀÌ ¼³Á¤ÇÑ ÈÄ, Target Module ¹öưÀ» Ŭ¸¯ÇÕ´Ï´Ù.
Deploy an EJB Module
À̾îÁö´Â È­¸é¿¡¼­ Deploy ¹öưÀ» Ŭ¸¯ÇÕ´Ï´Ù.
helloworld deployment confirm

Å×½ºÆ®

1. À¥ ¾ÖÇø®ÄÉÀÌ¼Ç Å×½ºÆ®

¼¼¼Ç ºóÀ» ÀÌ¿ëÇϴ Ŭ¶óÀÌ¾ðÆ® helloworld.jsp ÀÛ¼º

helloworld.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@ page import="javax.rmi.*"%> 
<%@ page import="javax.naming.*"%> 
<%@ page import="javax.transaction.*"%> 
<%@ page import="kr.co.hanbitbook.ejb.examples.*"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>HelloWorldBean ÀÌ¿ë</title> 
</head> 
<body> 
   <h3> 
<% 
	Context ctx = null;
	
	try {
		ctx = new InitialContext();
		Object h = ctx.lookup("HelloWorldBean");
		HelloWorldHome home = (HelloWorldHome)PortableRemoteObject.narrow(h, HelloWorldHome.class);
		HelloWorld helloworld = home.create();
		out.println(helloworld.helloWorld());
	} catch (Exception e) {
		out.println(e.toString());
	} 
%> 
   </h3> 
</body> 
</html> 

½ÇÇàÇÏ·Á¸é
wartest/WEB-INF/lib µð·ºÅ丮¿¡ helloworld.jar ÆÄÀÏÀ» º¹»çÇÕ´Ï´Ù.
´ÙÀ½ À¥·ÎÁ÷ ¼­¹ö¸¦ Àç½ÃÀÛÇÕ´Ï´Ù.
http://localhost:7001/helloworld.jsp À» ¹æ¹®ÇÏ¿© Hello World °¡ Ãâ·ÂµÇ´ÂÁö È®ÀÎÇÕ´Ï´Ù.

2. ÀÚ¹Ù ¾ÖÇø®ÄÉÀÌ¼Ç Å×½ºÆ®

helloworld_client ¶õ »õ·Î¿î ÇÁ·ÎÁ§Æ®¸¦ »ý¼ºÇÕ´Ï´Ù.
ȯ°æÀ» ¾Æ·¡¿Í °°ÀÌ ¼³Á¤ÇÕ´Ï´Ù.
¼¼¼Ç ºóÀ» ÀÌ¿ëÇÏ´Â ¼ø¼ö Àڹ٠Ŭ¶óÀÌ¾ðÆ®¸¦ À§ÇÑ ÇÁ·ÎÁ§Æ® ȯ°æ ¼³Á¤

HelloWorldClient.java

import javax.rmi.*;
import javax.naming.*;
import kr.co.hanbitbook.ejb.examples.*;

public class HelloWorldClient{
   public static void main(String args[]) {
      Context ctx = null;
      
      try {
         java.util.Properties p = new java.util.Properties();
         p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
         p.put(Context.PROVIDER_URL, "t3://localhost:7001");
         ctx = new InitialContext(p);
         Object h = ctx.lookup("HelloWorldBean");
         HelloWorldHome home = (HelloWorldHome)PortableRemoteObject.narrow(h, HelloWorldHome.class);
         HelloWorld helloWorld = home.create();
         System.out.println(helloWorld.helloWorld());
      } catch(Exception e) {
         System.out.println(e.toString());
      }
   }
}

ÀÌŬ¸³½º¿¡¼­ ½ÇÇàÇÕ´Ï´Ù.

Âü°í ¹®¼­

¾Ë±â½±°Ô Ç®¾î ¾´ À¥·ÎÁ÷°ú EJB 7Àå -ÇѺû¹Ìµð¾î,±è¼º¹Ú Àú-
¿¹Á¦¿¡ ¾²¿´´ø jar,war,ear ÆÄÀÏ (¹èÄ¡½Ã ÀÍ¼Ê¼Ç ¹ß»ýÇÒ ¶§ Âü°í¿ë)