我不明白你的意思!
EJB Test Client实际上就是一个Application啊!!
EJB Test Client实际上就是一个Application啊!!
解决方案 »
- 启动tomcat报错:
- 上传下载时报了这样一个错,但是服务器其他文件下载都不报错,在本地测试也不报错,这个是偶发事件,有谁遇到过。。
- 跪求一个愿意帮助j2ee新手的qq群
- java继承的疑问
- 这个servlet
- 关于久其软件有人知道这个公司吗?
- 请问jdom怎么实现在指定位置插入节点?
- tomcat5.0.27连接池配置问题(orcale),500分,在线 (下),错误日志少了一点(谢谢 btut2004(《电子产品世界》程序员)
- weblogic里面删不掉部署的ejb怎么办?(在线等待)
- Structs中的问题?
- 大家来比较JBOSS和weblogic\websphere
- 在Windows2000下安装J2EE和部署J2EE应用程序的问题
我说的是独立采用Applet或者Application来调用这个EJB.
我是新手,可能意思表达不清楚:
EJB已经发布了,能不能用Applet在另外一抬机器上调用这个EJB呢?
在Applet中怎么调用?
并且EJB Test Client可以在另外一抬机器上调用这个EJB!!(EJB必须实现remote接口)
EJB我已实现了remote接口。
但不知道怎么使用EJB Test Client在另外一抬机器上调用这个EJB
只要EJB实现remote接口,那EJB Test Client就可以远程调用了。
我想知道的是,如果菜用Applet的话,程序里怎么调用这个EJB呢?
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;import com.todaytech.sb.testdb.*;public class TestSvlt extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=EUC_CN";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/octest-stream");
request.setCharacterEncoding("gb2312");
ServletInputStream input = request.getInputStream();
DataInputStream dis = new DataInputStream(input);
String sTmp = dis.readUTF();
System.out.println("È¡µÃµÄ×Ö·û´®Î¢£º " + sTmp);
StringTokenizer st = new StringTokenizer(sTmp,"|");
int length = st.countTokens();
String sCmd = st.nextToken();
String rtnStr = "init is ok!";
TSB tsb = getSb();
if ("add".equals(sCmd)){
int id = 1;
if (length > 1){
String sid = st.nextToken();
if (!(sid == null || "".equals(sid))){
try
{
id = Integer.parseInt(sid);
}
catch (Exception ex)
{
id = 1;
}
}
}
tsb.add(id);
rtnStr = "ok ! ³É¹¦£¡";
}
else if ("set".equals(sCmd)){
String s50 = "Ò»Ò»Ò»Ò»Ò»Ò»Ò»Ò»Ò»Ò»Ò»¶þ¶þ¶þ¶þ¶þ¶þ¶þ¶þ¶þ¶þÈýÈýÈýÈýÈýÈýÈýÈýÈýÈýËÄËÄËÄËÄËÄËÄËÄËÄËÄËÄÎåÎåÎåÎåÎåÎåÎåÎåÎåÎå";
if (length > 1){
String s50Tmp = st.nextToken();
if (!(s50Tmp == null || "".equals(s50Tmp))){
s50 = s50Tmp;
}
}
int id = 1;
if (length > 2) {
String sid = st.nextToken();
if (! (sid == null || "".equals(sid))) {
try {
id = Integer.parseInt(sid);
}
catch (Exception ex) {
id = 1;
}
}
}
tsb.setTest50(s50, id);
rtnStr = "ok ! ³É¹¦£¡";
}
else if ("get".equals(sCmd)){
int id = 1;
if (length > 1) {
String sid = st.nextToken();
if (! (sid == null || "".equals(sid))) {
try {
id = Integer.parseInt(sid);
}
catch (Exception ex) {
id = 1;
}
}
}
rtnStr = tsb.getTest50(id);
}
else {
try{
tsb.setTest50(tsb.getTest50(1), 2);
rtnStr = "ok ! ³É¹¦!";
}
catch(Exception ex){
ex.printStackTrace();
rtnStr = ex.getMessage();
}
}
DataOutputStream dos = new DataOutputStream(response.getOutputStream());
dos.writeUTF(rtnStr);
dos.close();
}
//Clean up resources
public void destroy() {
} private TSB getSb(){
try
{
TSBHome tsbh = null;
InitialContext jndiContext = new InitialContext();
Object ref = jndiContext.lookup("TSB");
tsbh = (TSBHome) PortableRemoteObject.narrow(ref, TSBHome.class);
return tsbh.create();
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
} }
}
import java.awt.event.*;
import javax.swing.*;import java.io.*;
import java.net.*;
import java.util.*;
public class mainFram extends JFrame {
JPanel contentPane;
JButton call = new JButton();
JTextField cad = new JTextField(); //Construct the frame
public mainFram() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel) this.getContentPane();
call.setBounds(new Rectangle(109, 227, 225, 59));
call.setText("jButton1");
call.addActionListener(new mainFram_call_actionAdapter(this));
contentPane.setLayout(null);
this.setSize(new Dimension(455, 379));
this.setTitle("²âÊÔ");
cad.setText("jTextField1");
cad.setBounds(new Rectangle(11, 27, 429, 36));
contentPane.add(call, null);
contentPane.add(cad, null);
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
} private void callCmd(){
try{
URL url = new URL("http://localhost:7001/twebapp/testsvlt");
URLConnection urlcon = url.openConnection();
urlcon.setUseCaches(true);
urlcon.setDoInput(true);
urlcon.setDoOutput(true);
//urlcon.connect();
urlcon.setRequestProperty("Content-type", "application/octest-stream");
DataOutputStream dos = new DataOutputStream(urlcon.getOutputStream());
dos.writeUTF(cad.getText());
dos.flush();
dos.close();
DataInputStream dis=new DataInputStream(urlcon.getInputStream());
String rtnCode = dis.readUTF();
dis.close();
System.out.println(rtnCode);
}
catch(Exception ex){
ex.printStackTrace();
} } void call_actionPerformed(ActionEvent e) {
System.out.println(cad.getText());
callCmd();
}}class mainFram_call_actionAdapter implements java.awt.event.ActionListener {
mainFram adaptee; mainFram_call_actionAdapter(mainFram adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.call_actionPerformed(e);
}
}
一下是客户端代码,调用weblogic7.0应用服务器EJB对象方法..
package examples;import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import java.util.*;/**
* Client test application on a Container-Managed Entity Bean, Product.
*/
public class ProductClient { public static void main(String[] args) throws Exception { ProductHome home = null; try {
/*
* Get a reference to the Product Home Object - the
* factory for Product EJB Objects
*/
Context ctx = new InitialContext(System.getProperties());
home = (ProductHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("RemoteProductHome"), ProductHome.class); /*
* Use the factory to create the Product EJB Object
*/
home.create("123-456-7890", "P5-350", "350 Mhz Pentium", 200);
home.create("123-456-7891", "P5-400", "400 Mhz Pentium", 300);
home.create("123-456-7892", "P5-450", "450 Mhz Pentium", 400);
home.create("123-456-7893", "SD-64", "64 MB SDRAM", 50);
home.create("123-456-7894", "SD-128", "128 MB SDRAM", 100);
home.create("123-456-7895", "SD-256", "256 MB SDRAM", 200); /*
* Find a Product, and print out it's description
*/
Iterator i = home.findByName("SD-64").iterator();
System.out.println("The following product descriptions match the product name SD-64:");
while (i.hasNext()) {
Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);
System.out.println(prod.getDescription());
} /*
* Find all products that cost $200
*/
System.out.println("Calling finder to find all products that cost $200");
i = home.findByBasePrice(200).iterator(); while (i.hasNext()) {
Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);
System.out.println(prod.getDescription());
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (home != null) {
System.out.println("Destroying products.."); /*
* Find all the products
*/
Iterator i = home.findAllProducts().iterator();
while (i.hasNext()) {
try {
Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);
if (prod.getProductID().startsWith("123")) {
prod.remove();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
}
* Entity Bean which demonstrates Container-Managed persistence.
*
* This is a product that's persistent. It has an ID #, a name,
* a description, and a base price.
*/
public abstract class ProductBean implements EntityBean { protected EntityContext ctx; public ProductBean() {
} //----------------------------------------------------------------
// Begin abstract get/set methods
//---------------------------------------------------------------- public abstract String getName();
public abstract void setName(String name);
public abstract String getDescription();
public abstract void setDescription(String description);
public abstract double getBasePrice();
public abstract void setBasePrice(double price);
public abstract String getProductID();
public abstract void setProductID(String productID); //----------------------------------------------------------------
// End abstract get/set methods
//---------------------------------------------------------------- //----------------------------------------------------------------
// Begin EJB-required methods. The methods below are called
// by the Container, and never called by client code.
//---------------------------------------------------------------- /**
* Called by Container.
* Implementation can acquire needed resources.
*/
public void ejbActivate() {
System.out.println("ejbActivate() called.");
} /**
* EJB Container calls this method right before it
* removes the Entity Bean from the database.
* Corresponds to when client calls home.remove().
*/
public void ejbRemove() {
System.out.println("ejbRemove() called.");
} /**
* Called by Container.
* Releases held resources for passivation.
*/
public void ejbPassivate() {
System.out.println("ejbPassivate () called.");
} /**
* Called from the Container. Updates the entity bean
* instance to reflect the current value stored in
* the database.
*
* Since we're using Container-Managed Persistence, we
* can leave this method blank. The EJB Container will
* automatically set our public fields to the correct values.
*/
public void ejbLoad() {
System.out.println("ejbLoad() called.");
} /**
* Called from the Container. Updates the database to
* reflect the current values of this in-memory Entity Bean
* instance representation.
*
* Since we're using Container-Managed Persistence, we can
* leave this method blank. The EJB Container will
* automatically save our public fields into the database.
*/
public void ejbStore() {
System.out.println("ejbStore() called.");
} /**
* Called by Container. Associates this Bean instance with
* a particular context. Once done, we can query the
* Context for environment info
*/
public void setEntityContext(EntityContext ctx) {
System.out.println("setEntityContext called");
this.ctx = ctx;
} /**
* Called by Container. Disassociates this Bean instance
* with a particular context environment.
*/
public void unsetEntityContext() {
System.out.println("unsetEntityContext called");
this.ctx = null;
} /**
* Called after ejbCreate(). Now, the Bean can retrieve
* its EJBObject from its context, and pass it as a 'this'
* argument.
*/
public void ejbPostCreate(String productID, String name, String description, double basePrice) {
System.out.println("ejbPostCreate() called");
} /**
* This is the initialization method that corresponds to the
* create() method in the Home Interface.
*
* When the client calls the Home Object's create() method,
* the Home Object then calls this ejbCreate() method.
*
* NOTE: Since we're using Container-Managed persistence,
* this method returns void. With Bean-Managed Persistence,
* we returned the PK. This is because our Bean was
* responsible for dealing with PKs and accessing
* the database. Now that we let the Container handle
* persistence, the Container makes the Primary Key.
*
* We still need to initialize our Bean's fields with the
* parameters passed from the client, so that the Container
* can inspect our Bean and create the corresponding database
* entries.
*/
public String ejbCreate(String productID, String name, String description, double basePrice) {
System.out.println("ejbCreate() called"); setProductID(productID);
setName(name);
setDescription(description);
setBasePrice(basePrice); return productID;
} // No finder methods - they are implemented by Container //----------------------------------------------------------------
// End EJB-required methods
//----------------------------------------------------------------
}
在EJB2.0中,从应用程序中查找HOME对象:Properties prop = new Properties( );
prop.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
prop.put(Context.PROVIDER_URR,"t3://localhost:7001");
Context ctx = new InitialContext(prop);
home = (ProductHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("RemoteProductHome"), ProductHome.class);home.create("123-456-7890", "P5-350", "350 Mhz Pentium", 200);//创建并初始化EJB对象
C:\bea\user_projects\mydomain\myserver\upload\EJBTest\EJBTest.jar
其中有2个自己写的方法getName和getMyText.
要在Applet中调用EJB中的这2个方法,怎么调用?大家一下贴这么多,谢谢了,不过,反倒不明白了。
javac -classpath ".;weblogic.jar" test\TestClient.java
java -classpath ".;weblogic.jar" test.TestClienttest中的文件清单:
testRemote.class <--------EJB远程接口
testRemoteHome.class 〈-------EJB Home接口
TestClient.java weblogic.jar要包括在类路径中!!如何调用自己写的方法getName和getMyText,可以研究一下jb8自动生成的例子
用Applet的话也差不多
C:\bea\user_projects\mydomain\myserver\upload\EJBTest\EJBTest.jar StringBuffer sbuf1 = new StringBuffer();
URL url1 = new URL("http://localhost:7001/"); //在这里应该什么?
this.getStringFromUrl(url1,sbuf1);