Context ctx=new InitialContext();
ds=(DataSource)ctx.lookup("MyJNDI"); // MyJNDI为前面配置的Weblogic的JDBC的JNDI名
}
catch(Exception e)
{ }
}
·然后。在可视化设计窗口 testsqlBean上右键 add method 取名为getMyConn 返回类型为前面自建的TestString
参数为String sql
最后修改该函数代码如下:
public TestString getMyConn(String sql) {
TestString t1=new TestString(sql);
String returnString=t1.getA1(ds,sql);
return new TestString(returnString);
}
.完成后 Make Project 一次编译通过后
·在项目文件列表上,点击web run
此时不要人工启动Weblogic 让JB7去启动它
如果没有错误,则EJB被自动部署到weblogic上,
· 编一个Servlet作为客户端
用JB7的New 一个Servlet 起名为testpoolServlet
全部代码如下
package testpool;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.naming.*;
import javax.ejb.*;
import java.rmi.RemoteException;
import java.rmi.Remote;
import testpool.testsql;
import testpool.testsqlHome;
public class testpoolServlet extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
TestVector myStr;
file://Initialize global variables
public void init() throws ServletException {
}
file://Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
String sql="select * from table3"; file://客户端给EJB的sql语句
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>testpoolServlet</title></head>");
out.println("<body>");
out.println("<table><tr>");
try {
Context context=new InitialContext();
Object objref = context.lookup("testsqlBean");// EJB中那个SessionBean的名字
testsqlHome home = (testsqlHome)javax.rmi.PortableRemoteObject.narrow(objref,testsqlHome.class);
testsql servletsql=home.create();
TestString myTStr=servletsql.getMyConn(sql);
String myStr=myTStr.returnStr();
out.println("<td>This is=="+myStr+"</td>");
out.println("</tr></table>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
} catch (Exception e) {
out.println("A problem has occurred with the servlet.");






