applet和servlet通信的菜鸟问题(一定给分!)

fallingkent 2002-03-15 10:39:38
在一个project里编了一个applet和一个servlet,把他们放到服务器端了。在applet中应该怎样调用那个servlet呢?看一个例子说这样:

URL theURL = new URL("http://servername/myservlet");
theURL.openStream();

可是不成啊,那个myservlet的名字用.class或者不用都不行。
应该如何通信?
...全文
46 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fallingkent 2002-03-16
服务器不是我配置的,我也不知道。

看来这是一个错误的选择,不如在服务器上直接放一个application算了
回复
magus_yang 2002-03-16
你用jswdk还是tomcat?
回复
fallingkent 2002-03-15
你这里也是通过
url=new URL("http://localhost/PostServer");
con=url.openConnection();
来操作的,可是我把它放在服务器上就
Exception : java.io.FileNotFoundException: File not found
要怎么设置它的名字吗?
回复
peacock_king 2002-03-15
去www.cn-java.com看看,讲得很清楚。
回复
magus_yang 2002-03-15
rmi看了,不过一直没时间实践一下

socket用vc和vb写多了,java应该不会有很大差别。这就是没有实践的理由,呵呵

corba没看过,你要另请高明了,呵呵
回复
magus_yang 2002-03-15
下面是我写的post的例子,三部分applet,servlet和嵌有applet的网页
//PostClient.java
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;


class PostClientPanel extends JPanel implements ActionListener
{
public PostClientPanel()
{
strOutput=new String("输出初始化!");
vctFromServ.clear();
vctFromServ.add("输入初始化!");

cmdStep1=new JButton("第一步");
cmdStep2=new JButton("第二步");

add(cmdStep1);
add(cmdStep2);

cmdStep1.addActionListener(this);
cmdStep2.addActionListener(this);

}

public void actionPerformed(ActionEvent evt)
{
Object source=evt.getSource();
if(source==cmdStep1)
{
strOutput="第一步";
try
{
//向server发送消息
System.out.println("Before Step1 connection!");
url=new URL("http://localhost/PostServer");
con=url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
ByteArrayOutputStream byteout=new ByteArrayOutputStream();
DataOutputStream out=new DataOutputStream(byteout);
out.writeUTF("第一步");
out.flush();
byte buf[]=byteout.toByteArray();
con.setRequestProperty("Content-type","application/octet-stream");
con.setRequestProperty("Content-length",""+buf.length);
DataOutputStream dataout=new DataOutputStream(con.getOutputStream());
dataout.write(buf);
dataout.flush();
dataout.close();
byteout.close();
out.close();

}
catch(IOException e)
{
System.out.println("OUTPUT ERROR:"+e);
}
try
{
//从server接收消息
DataInputStream in=new DataInputStream(con.getInputStream());
vctFromServ.clear();
String strInput;
while((strInput=in.readUTF())!=null)
{
vctFromServ.add(strInput);
}
in.close();
}
catch(IOException e)
{
System.out.println("INPUT Error " + e);
}
}
else
{
strOutput="第二步";
try
{

System.out.println("Before Step2 connection!");
url=new URL("http://localhost/PostServer");
con=url.openConnection();
con.setUseCaches(false);
con.setDoOutput(true);
con.setDoInput(true);
ByteArrayOutputStream byteout=new ByteArrayOutputStream();
DataOutputStream out=new DataOutputStream(byteout);
out.writeUTF("第二步");
out.flush();
byte buf[]=byteout.toByteArray();
con.setRequestProperty("Content-type","application/octet-stream");
con.setRequestProperty("Content-length",""+buf.length);
DataOutputStream dataout=new DataOutputStream(con.getOutputStream());
dataout.write(buf);
dataout.flush();
dataout.close();

DataInputStream in=new DataInputStream(con.getInputStream());
vctFromServ.clear();
String strInput;
while((strInput=in.readUTF())!=null)
{
vctFromServ.add(strInput);
}
}
catch(IOException e)
{
System.out.println("Error " + e);
}
}
repaint();
}

public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawString(strOutput,200,200);
int tmpX=200;
int tmpY=250;
for(int i=0;i<vctFromServ.size();i++)
{
g.drawString((String)vctFromServ.elementAt(i),tmpX,tmpY);
tmpY+=50;
}
}

private String strOutput;
private Vector vctFromServ=new Vector();
private JButton cmdStep1;
private JButton cmdStep2;
private URL url;
private URLConnection con;
}

public class PostClient extends JApplet

{ public void init()
{ Container contentPane = getContentPane();
contentPane.add(new PostClientPanel());


}


}


//PostServer.java
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostServer extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse resp)
{
try
{
//接收从客户端发来的数据
in=new DataInputStream(req.getInputStream());
strChoice=in.readUTF().trim();
System.out.println("strChoice="+strChoice);


//向客户端发送数据
resp.setContentType("application/octet-stream");
byteout=new ByteArrayOutputStream();
out=new DataOutputStream(byteout);
if(strChoice.equals("第一步"))
{
out.writeUTF("第一步回复1!");
out.writeUTF("第一步回复2!");
}
else if(strChoice.equals("第二步"))
{
out.writeUTF("第二步回复1!");
out.writeUTF("第二步回复2!");
out.writeUTF("第二步回复3!");
out.writeUTF("第二步回复4!");
}
out.flush();

buf=byteout.toByteArray();
resp.setContentLength(buf.length);
servletout=resp.getOutputStream();
servletout.write(buf);



}
catch(IOException ex)
{
System.out.println("out3 ERROR:"+ex);
}

}

public void destroy()
{
try
{
in.close();
servletout.close();
byteout.close();
out.close();
}
catch(Exception e)
{
System.out.println(e);
}
}

private DataInputStream in;
private String strChoice;
private byte[] buf;
private ServletOutputStream servletout;
private ByteArrayOutputStream byteout;
private DataOutputStream out;

}

//PostClient.html
<html>
<title>xyAxisApplet</title>
<body>

<!--"CONVERTED_APPLET"-->
<!-- CONVERTER VERSION 1.3 -->
<p>Applet和Servlet通讯测试:Post方法</p>
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
WIDTH = 400 HEIGHT = 400 codebase="http://java.sun.com/products/plugin/1.3/jinstall-13-win32.cab#Version=1,3,0,0">
<PARAM NAME = CODE VALUE = "PostClient" >

<PARAM NAME="type" VALUE="application/x-java-applet;version=1.3">
<PARAM NAME="scriptable" VALUE="false">

<COMMENT>
<EMBED type="application/x-java-applet;version=1.3" CODE = "PostClient" WIDTH = 400 HEIGHT = 400 scriptable=false pluginspage="http://java.sun.com/products/plugin/1.3/plugin-install.html"><NOEMBED></COMMENT>

</NOEMBED></EMBED>
</OBJECT>

<!--
<APPLET CODE = "CheckTestApplet" WIDTH = 950 HEIGHT = 342>


</APPLET>
-->
<!--"END_CONVERTED_APPLET"-->
</body>
</html>
回复
magus_yang 2002-03-15
http://www.csdn.net/expert/topic/298/298350.xml?temp=.5500605

这里有个get的例子,我再找找post的,好久以前写的了,呵呵
回复
fallingkent 2002-03-15
我靠,这么牛比,呵呵
我想用最简单的,对服务器要求最少的。
谢谢前辈!
回复
magus_yang 2002-03-15
applet和servlet通讯的四种方法
http的get或post
socket
rmi
corba
你决定选哪一种呢?
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告