RSS

EJB Project与Java Project,Web Project通信

02 Jan

开发环境: JDK 5, MyEclipse, JBoss 5(如果用JDK6,会报错)
A.
创建
EJB Project
创建一个无状态会话Bean,选择LocalRemote
接口。
clip_image001

编写该会话Bean的本地和远程接口及该会话Bean的代码
代码如下

package chapter2;
import javax.ejb.Local;

@Local
public interface
FileListLocal {
    String list(String dirname);
}

package chapter2;
import javax.ejb.Remote;

@Remote
public interface
FileListRemote {
    String list(String dirname);
}

package chapter2;
import
java.io.File;
import javax.ejb.Stateless;

@Stateless
public class FileList implements
FileListLocal, FileListRemote {
    public
String list(String dirname) {
        File dir = new
File(dirname);
        if
(!dir.isDirectory()) {
            return dirname + ” is not a directory!”
;
       
}

        String[] fs = dir.list();
        String result = “”
;
        for
(String f : fs) {
            result += f + “;”
;
       
}
        return
result;
   
}
}


jar文件形式部署到JBoss的服务器。clip_image003

EJB的部署JNDI信息如下:
FileList/remote – EJB3.x Default Remote Business Interface
FileList/remote
-chapter2.FileListRemote – EJB3.x Remote Business Interface
FileList/local – EJB3.x Default Local Business Interface

FileList/local-chapter2.FileListLocal – EJB3.x Local Business Interface

注意:配置JBOSS的时候,Optional program arguments要添加-b 0.0.0.0,这样任何IP地址都可以访问,默认的是只允许本地访问。如果使用集群,这个Server name要改写为all,而不是defaultclip_image004

B.   创建Java Project 客户端访问该EJB
创建一个Java Project
named “EJB – Java Project”
创建 User Libraries – jboss_client,添加
jbossall-client.jar
clip_image006

添加项目依赖以及库依赖clip_image008

clip_image010

在根目录添加jndi.properties文件clip_image012

创建客户端测试程序 – FileListTest.java

package chapter2;
import
javax.naming.Context;
import javax.naming.InitialContext;

public class FileListTest {
    public static void main(String[] args) throws
Exception {
        Context ctx = new
InitialContext();
       
FileListRemote fileList = (FileListRemote) ctx
            .lookup(“FileList/remote”
);
        String fs = fileList.list(“D:”
);
        String str = fs.replaceAll(“;”, “\n”
);
        System.out
.println(str);
   
}
}

运行该程序,输出服务器D盘的文件列表。

C.   创建Web Project访问该EJB
创建一个
Web Project – EJB3 – Web Project
添加项目依赖,不需要添加库依赖
clip_image014

编写index.jsp

<form action=“FileListServlet” method=“get”>
输入路径:<input type=“text” name=“dirname”/>
<
input type=“submit” value=
提交/>
</
form
>
<%=request.getAttribute(“result”)
%>


编写FileListServlet文件

package controller;
import
java.io.IOException;
import
javax.naming.Context;
import
javax.naming.InitialContext;
import
javax.naming.NamingException;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import chapter2.FileListLocal;

public class FileListServlet extends HttpServlet {

    private static final long serialVersionUID = 188327496433338504L;
    private FileListLocal fileList
;
    public void
doGet(HttpServletRequest request, HttpServletResponse response)
        throws
ServletException, IOException {
        String dirname = request.getParameter(“dirname”
);
        String fs = fileList
.list(dirname);
        fs = fs.replaceAll(“;”, “<BR/>”
);
        request.setAttribute(“result”
, fs);
        request.getRequestDispatcher(“index.jsp”
).forward(request, response);
    }

    public void init() throws ServletException {

        try {
            Context ctx = new
InitialContext();
            fileList = (FileListLocal) ctx.lookup(“FileList/local”
);
        } catch
(NamingException e) {
           
e.printStackTrace();
       
}
   
}
}


更新web.xml文件

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app version=
“2.5”
xmlns=
http://java.sun.com/xml/ns/javaee
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;>

<servlet>
    <description>This is the description of my J2EE component
</description>
    <display-name>This is the display name of my J2EE component
</display-name>
    <servlet-name>FileListServlet
</servlet-name>
    <servlet-class>controller.FileListServlet
</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>FileListServlet
</servlet-name>
    <url-pattern>/FileListServlet
</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.jsp
</welcome-file>
</welcome-file-list>
</web-app>


部署该Web Project
clip_image015

运行该程序:
clip_image016

Advertisements
 
Leave a comment

Posted by on 01/02/2008 in EJB

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: