当前位置: 技术问答>java相关
applet和servlet通信的菜鸟问题(一定给分!)
来源: 互联网 发布时间:2015-04-17
本文导语: 在一个project里编了一个applet和一个servlet,把他们放到服务器端了。在applet中应该怎样调用那个servlet呢?看一个例子说这样: URL theURL = new URL("http://servername/myservlet"); theURL.openStream(); 可是不成啊,那个myservlet的名...
在一个project里编了一个applet和一个servlet,把他们放到服务器端了。在applet中应该怎样调用那个servlet呢?看一个例子说这样:
URL theURL = new URL("http://servername/myservlet");
theURL.openStream();
可是不成啊,那个myservlet的名字用.class或者不用都不行。
应该如何通信?
URL theURL = new URL("http://servername/myservlet");
theURL.openStream();
可是不成啊,那个myservlet的名字用.class或者不用都不行。
应该如何通信?
|
下面是我写的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
//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
Applet和Servlet通讯测试:Post方法
|
去www.cn-java.com看看,讲得很清楚。