第一個 Session Bean(Servlet 客戶端)


當Servlet與EJB不為同一個應用程式時(不為同一個JVM),其實與 第一個 Session Bean(單機客戶端) 類似的,要使用JNDI查找以取得Bean,重點在於何時取得Bean,若為Stateless Session Bean,由於其沒有狀態,每個客戶端都可以使用同一個Stateless Session Bean,因此Servlet初始化時可以是取得Bean的時機,也就是定義在init()方法之中,例如:
    ....
    private HelloBean hello;
   
    public void init() throws ServletException {
        try {
            super.init();
            InitialContext context = new InitialContext();
            Object obj = context.lookup("onlyfun.caterpillar.HelloBean");
            hello = (HelloBean) PortableRemoteObject.narrow(obj, HelloBean.class);
        } catch (NamingException ex) {
           Logger.getLogger(HelloServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    ....

為了示範完整的範例,在這邊再次列出Session Bean的程式碼,首先是介面定義:
  • HelloBean.java
package onlyfun.caterpillar;

import javax.ejb.Remote;

@Remote
public interface HelloBean {
public String doHello(String message);
}

再來是實作,在這邊不使用mappedName屬性,因此稍後可以透過查找介面名稱來取得Bean:
  • HelloBeanImpl.java
package onlyfun.caterpillar;

import javax.ejb.Stateless;

@Stateless(name="ejb/HelloFacade")
public class HelloBeanImpl implements HelloBean {
public String doHello(String message) {
return message + "processed....";
}
}

而在Servlet客戶端,重新定義其init()方法以取得Stateless Session Bean:
  • HelloServlet.java
package onlyfun.caterpillar;

import java.io.*;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.logging.*;
import javax.rmi.PortableRemoteObject;

public class HelloServlet extends HttpServlet {
private HelloBean hello;

public void init() throws ServletException {
try {
super.init();
InitialContext context = new InitialContext();
Object obj = context.lookup("onlyfun.caterpillar.HelloBean");
hello = (HelloBean)
PortableRemoteObject.narrow(obj, HelloBean.class);
} catch (NamingException ex) {
Logger.getLogger(HelloServlet.class.getName())
.log(Level.SEVERE, null, ex);
}
}

protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet HelloServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>" + hello.doHello("Orz...") + "</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}

protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

public String getServletInfo() {
return "Short description";
}
}

您需要準備jndi.properties,放在WEB-INF/classes中:
  • jndi.properties
java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory
java.naming.factory.url.pkgs=com.sun.enterprise.naming
java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl
# 選用,如果伺服端不在本機的話
org.omg.CORBA.ORBInitialHost=localhost
# 選用,如果伺服端不是使用3700埠的話
org.omg.CORBA.ORBInitialPort=3700

若您請求該Servlet,則將得到以下的結果:
<html>
<head>
<title>Servlet HelloServlet</title>
</head>
<body>
<h1>Orz...processed....</h1>
</body>
</html>