如何在asp中调用java组件(50分)

  • 主题发起人 主题发起人 hozen
  • 开始时间 开始时间
H

hozen

Unregistered / Unconfirmed
GUEST, unregistred user!
我改写了一个查询域名的程序。想实现在asp中调用编译好的class。
我的步骤如下:
先把下面的程序编译成class文件。
import java.net.*;
import java.io.*;
public class whois
{
public final static int port = 43;
public final static String hostname = "whois.internic.net";
public String s;
public String getInfo(String url){
Socket theSocket;
DataInputStream theWhoisStream;
PrintStream ps;

try{
theSocket = new Socket(hostname,port,true);
ps = new PrintStream(theSocket.getOutputStream());
ps.print(url);
ps.print("/r/n");
theWhoisStream = new DataInputStream(theSocket.getInputStream());

while((s = theWhoisStream.readLine()) != null){
s = s + "/n";
}
theWhoisStream.close();
ps.close();
theSocket.close();

}
catch(IOException e){
System.err.println(e);
}
return s;
}
}
然后将其拷贝到C:/WINNT/java/trustlib下,并运行注册程序
"C:/Program Files/Microsoft Visual Studio/VIntDev98/bin/JAVAREG.EXE" /register /class:whois /progid:javacom.whois
注册成功后,在asp中
<%
set who=server.CreateObject("javacom.whois")
Response.Write who.getInfo("www.sohu.com")
%>
没有输出:(
我的java写的有问题,因为我按以上步骤写个简单的程序就可以执行。如下:
public class javacom
{
public String szHello = "Hello World" ;
public String anyFunc()
{
String n = "As returned by anyFunc !" ;
return n ;
}
}
注册为javacom.text,成功后在asp中写入下代码
<%
set cc= Server.CreateObject("javacom.test")
response.write cc.szHello &amp;
"<br>"
response.write cc.anyFunc &amp;
"<br>"
%>
成功输出字符串。
-------------------------------------------------
还请高手指点。
附上java查询域名的源程序。
import java.net.*;
import java.io.*;
public class whois
{
public final static int port = 43;
public final static String hostname = "whois.internic.net";
public static void main(String[] args){
Socket theSocket;
DataInputStream theWhoisStream;
PrintStream ps;

if(args.length<1){
System.out.println("/n java whois by busyhozen ");
System.out.println("Parameters:");
System.exit(1);
}
try{
theSocket = new Socket(hostname,port,true);
ps = new PrintStream(theSocket.getOutputStream());
for(int i=0;i<args.length;i++){
ps.print(args + " ");
ps.print("/r/n");
theWhoisStream = new DataInputStream(theSocket.getInputStream());
String s;
while((s = theWhoisStream.readLine()) != null){
System.out.println(s);
}
theWhoisStream.close();
ps.close();
theSocket.close();
}

}
catch(IOException e){
System.err.println(e);
}
}

}
 
第一次看见这种用法,真是大开眼界。建议去掉 System.err.println(e);
然后在 ps.print("/r/n");
加个空格:ps.print(" /r/n");

 
yysun,谢谢您的答复。我修改了一下程序,然后在asp中调用
--------------------------------
<%
set jini=server.CreateObject("jini.dd")
Response.Write jini.port &amp;
"<br>"
Response.Write jini.hostname &amp;
"<br>"
Response.Write jini.getInfo("www.sina.com") &amp;
"<br>"
Response.Write jini.hehe
%>
返回如下结果
--------------------------------
43
whois.internic.net
nulls comes from jini
hehe from jini
--------------------------------
修改后的程序如下:
import java.net.*;
import java.io.*;
/**
* This class is designed to be packaged with a COM DLL output format.
* The class has no standard entry points, other than the constructor.
* Public methods will be exposed as methods on the default COM interface.
* @com.register ( clsid=6401B786-B54F-435F-9F0A-449CA5E071E6, typelib=2D6E0E60-589E-4E0D-BEB9-258E65BF13FA )
*/
public class jini
{
public final static int port = 43;
public final static String hostname = "whois.internic.net";
public String hehe = "hehe from jini";
public String s;
public String getInfo(String url){
Socket theSocket;
DataInputStream theWhoisStream;
PrintStream ps;

try{
theSocket = new Socket(hostname,port,true);
ps = new PrintStream(theSocket.getOutputStream());
ps.print(url);
ps.print(" /r/n");
theWhoisStream = new DataInputStream(theSocket.getInputStream());

while((s = theWhoisStream.readLine()) != null){
s = s + "/n";
}
theWhoisStream.close();
ps.close();
theSocket.close();

}
catch(IOException e){
// System.err.println(e);
}
return s + "s comes from jini";
}
}
--------------------------------
奇怪为什么s是nulls呢?
我在用原来的java程序执行查询时输出了一大堆东西呀。
我刚开始学习java,不是很了解他的机制,还请您再指点一二。
 
哈,我已解决。谢谢大家。
 
怎么解决的?
 
不好意思,我原来犯了个错误。在
while((s = theWhoisStream.readLine()) != null){
s = s + "/n";
}
循环中,s变量被重复赋值,最后一次赋值应该是s=null+null(应该是这样吧)
所以返回值就像我上面贴的一样s为null。
知道错误在哪,也就很容易解决了。
在循环前先定义一个变量
String replyWhoIS;
while((s = theWhoisStream.readLine()) != null){
replyWhoIS = replyWhoIS + s + "/n";
}
最后
return replyWhoIS;
就可以了。
嗬嗬,小弟初学java,还请大家指正。
 
好玩
asp <--> java
 
你的类是不是用Vj++ 6.0写的???
 
后退
顶部