EJB客戶端程序
完成了EJB后我們需要再來完成使用EJB的客戶端程序,我們寫一個Servlet程序來完成這個工作,代碼如下:
package net.chinacode.web;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.rmi.PortableRemoteObject;
import net.chinacode.hello.Hello;
import net.chinacode.hello.HelloHome;
public class HelloServlet extends HttpServlet {
// constructor
public HelloServlet() {
super();
trace("");
}
// A reference to the remote `Hello' object
protected Hello _hello;
// Initializes this servlet
public void init(ServletConfig config) throws ServletException {
super.init(config);
trace("init");
// Get the initial JNDI context using our settings
Context context;
try {
context = new InitialContext();
}
catch (Throwable exception) {
throw new ServletException(
"Unable to get initial JNDI context: " + exception.toString());
}
// Get a reference to the Hello home interface
HelloHome helloHome;
try {
Object boundObject = context.lookup("java:comp/env/ejb/HelloHome");
helloHome = (HelloHome) PortableRemoteObject.narrow(boundObject,HelloHome.class);
}
catch (Throwable exception) {
throw new ServletException(
"Unable to get home interface: " + exception.toString());
}
// Get a reference to a Hello instance
try {
_hello = helloHome.create();
}
catch (Throwable exception) {
throw new ServletException(
"Unable to create Hello instance: " + exception.toString());
}
// Insanity check: Make sure we have a valid reference
if (_hello == null) {
throw new ServletException(
"Unable to create Hello instance, create() returned null");
}
}
// Handles the HTTP GET request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
trace("doGet");
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/html");
// Get the answer from the bean
String answer;
try {
answer = _hello.sayHello("HD");
}
catch (Throwable exception) {
out.println("");
out.println("Time stamp: " + new Date().toString());
out.println("
Hello type: " + _hello.getClass().getName());
out.println("Error calling the Hello bean");
out.println(exception.toString());
out.println("");
out.println("");
return;
}
out.println("");
out.println("Time stamp: " + new Date().toString());
out.println("
Hello type: " + _hello.getClass().getName());
out.println("
Answer: " + answer);
out.println("");
out.println("");
}
// Displays a trace message to System.out
private void trace(String methodName) {
System.out.print(methodName);
System.out.println("() called");
}
}
這我們在servlet中先使用context = new InitialContext();來得到環境上下文,通過這句話我們就可以來向JNDI數據樹中進行查詢了,而又使用Object boundObject = context.lookup("java:comp/env/ejb/HelloHome");來得到了我們在系統的web.xml中設置的參數,這個參數是在JNDI目錄數中的節點名,再通過一個對以下操作來得到EJB的Home接口:helloHome = (HelloHome) PortableRemoteObject.narrow(boundObject,HelloHome.class);而Home接口的create方法會返回Remote接口,這個Remote接口可以來直接調用Server上的實際方法。
package net.chinacode.web;
import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.naming.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.rmi.PortableRemoteObject;
import net.chinacode.hello.Hello;
import net.chinacode.hello.HelloHome;
public class HelloServlet extends HttpServlet {
// constructor
public HelloServlet() {
super();
trace("
}
// A reference to the remote `Hello' object
protected Hello _hello;
// Initializes this servlet
public void init(ServletConfig config) throws ServletException {
super.init(config);
trace("init");
// Get the initial JNDI context using our settings
Context context;
try {
context = new InitialContext();
}
catch (Throwable exception) {
throw new ServletException(
"Unable to get initial JNDI context: " + exception.toString());
}
// Get a reference to the Hello home interface
HelloHome helloHome;
try {
Object boundObject = context.lookup("java:comp/env/ejb/HelloHome");
helloHome = (HelloHome) PortableRemoteObject.narrow(boundObject,HelloHome.class);
}
catch (Throwable exception) {
throw new ServletException(
"Unable to get home interface: " + exception.toString());
}
// Get a reference to a Hello instance
try {
_hello = helloHome.create();
}
catch (Throwable exception) {
throw new ServletException(
"Unable to create Hello instance: " + exception.toString());
}
// Insanity check: Make sure we have a valid reference
if (_hello == null) {
throw new ServletException(
"Unable to create Hello instance, create() returned null");
}
}
// Handles the HTTP GET request
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
trace("doGet");
ServletOutputStream out = response.getOutputStream();
response.setContentType("text/html");
// Get the answer from the bean
String answer;
try {
answer = _hello.sayHello("HD");
}
catch (Throwable exception) {
out.println("");
out.println("Time stamp: " + new Date().toString());
out.println("
Hello type: " + _hello.getClass().getName());
out.println("Error calling the Hello bean");
out.println(exception.toString());
out.println("");
out.println("");
return;
}
out.println("");
out.println("Time stamp: " + new Date().toString());
out.println("
Hello type: " + _hello.getClass().getName());
out.println("
Answer: " + answer);
out.println("");
out.println("");
}
// Displays a trace message to System.out
private void trace(String methodName) {
System.out.print(methodName);
System.out.println("() called");
}
}
這我們在servlet中先使用context = new InitialContext();來得到環境上下文,通過這句話我們就可以來向JNDI數據樹中進行查詢了,而又使用Object boundObject = context.lookup("java:comp/env/ejb/HelloHome");來得到了我們在系統的web.xml中設置的參數,這個參數是在JNDI目錄數中的節點名,再通過一個對以下操作來得到EJB的Home接口:helloHome = (HelloHome) PortableRemoteObject.narrow(boundObject,HelloHome.class);而Home接口的create方法會返回Remote接口,這個Remote接口可以來直接調用Server上的實際方法。