可以考虑
Applet-Servlet-Database
Applet-RMI-Database
等通信方式。Applet-Servlet方式有个我总结的例子,你可以参考之。
其中需要Tomcat之类的支持Servlet的东东。//:Applet-Applet和Servlet通过Http通信.txt1、Servlet的基础类
//:ServletSide.javapackage citi.project.a2s;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;public class ServletSide{
//发送数据给Applet
public static boolean Send(HttpServletResponse res,String strres){
try{
res.setContentType("text/plain");
res.setHeader("Pragma", "no-cache");
DataOutputStream dos = new DataOutputStream(res.getOutputStream());
dos.writeUTF(strres);
dos.flush();
dos.close();
return true;
}catch(Exception e){e.printStackTrace();return false;}
}
}2、Applet的基础类
//:AppletSide.javapackage citi.project.a2s;
import java.net.*;
import java.io.*;
import java.util.*;public class AppletSide{
//String strurl="http://localhost:8080/tsc/A2SServlet";//样例
//发送数据给Servlet,并读回数据。数据存放在strreq中
public String SendAndRead(String strurl,String strreq){
try{
URL url=new URL(strurl);
URLConnection ucon=url.openConnection();
ucon.setDoOutput(true);
ucon.setDoInput(true);
//发送数据,即写数据到数据流中。数据是UTF-8格式
DataOutputStream dos = new DataOutputStream(ucon.getOutputStream());
dos.writeBytes(strreq);
dos.close();
//读数据,即从数据流中读数据
DataInputStream dis = new DataInputStream(ucon.getInputStream());
String temp = dis.readUTF();
dis.close();
return temp;
}catch(Exception e){e.printStackTrace();return new String();}
}
//发送数据给Servlet,并读回数据。数据存放在reqs中
public String SendAndRead(String strurl,Properties reqs){
try{
String req=new String();
String item=new String();
Enumeration eall=reqs.propertyNames();
//生成数据,并使用UTF-8格式编码
while(eall.hasMoreElements()){
item=(String)eall.nextElement();
req=req+item +"="+ URLEncoder.encode(reqs.getProperty(item),"UTF-8")+"&";
//System.out.println(req);
}
req=req.substring(0,req.length()-1);
//System.out.println(req);
return SendAndRead(strurl,req);
}catch(Exception e){e.printStackTrace();return new String();}
}
//发送数据给Servlet,并读回数据。数据存放在reqsvalue中
public String SendAndRead(String strurl,String[] reqskey,String[] reqsvalue){
try{
String req=new String();
int i=0;
//生成数据,并使用UTF-8格式编码
for(;i<reqskey.length-1;i++)
req=req+reqskey[i]+"="+URLEncoder.encode(reqsvalue[i],"UTF-8")+"&";
req=req+reqskey[i]+"="+URLEncoder.encode(reqsvalue[i],"UTF-8");
return SendAndRead(strurl,req);
}catch(Exception e){e.printStackTrace();return new String();}
}
}3、测试Servlet源代码
//:A2SServlet.java
//**** Servlet ***************************************
package test.a2s;
import citi.project.a2s.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;//在服务器端需要将此Servlet注册
public final class A2SServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
try{
//接收请求后,从请求中解析出所需参数
String name = req.getParameter("name");
String pwd = req.getParameter("password"); System.out.println("name:"+name);
System.out.println("pwd:"+pwd);
//简单返回给Applet一个字符串
ServletSide.Send(res,name+pwd+"kaokao");
}catch(Exception e){e.printStackTrace();}
}
public void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,res);
}
}4、测试Applet的源代码
//:A2SApplet.java
//**** Applet ***************************************
package test.a2s;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import citi.project.a2s.*;
import java.util.*;public class A2SApplet extends Applet {
JButton
b1 = new JButton("JButton 1"),
b2 = new JButton("JButton 2");
JTextArea t = new JTextArea(20,20);
public void init() {
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e){
String name = ((JButton)e.getSource()).getText();
//t.setText(doit());
t.setText(getCodeBase().toString());
}
};
b1.addActionListener(al);
add(b1);
b2.addActionListener(al);
add(b2);
add(t);
}
private String doit(){
//A2SServlet是在服务器端注册的Servlet
String strurl="http://localhost:8080/tsc/A2SServlet";
Properties p=new Properties();
p.setProperty("co","0");
p.setProperty("m","0");
return new AppletSide().SendAndRead(strurl,p);
}
public static void main(String args[]) {
A2SApplet applet = new A2SApplet();
JFrame frame = new JFrame("TextAreaNew");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(300,100);
applet.init();
applet.start();
frame.setVisible(true);
}
} ///:~
Applet-Servlet-Database
Applet-RMI-Database
等通信方式。Applet-Servlet方式有个我总结的例子,你可以参考之。
其中需要Tomcat之类的支持Servlet的东东。//:Applet-Applet和Servlet通过Http通信.txt1、Servlet的基础类
//:ServletSide.javapackage citi.project.a2s;
import java.net.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;public class ServletSide{
//发送数据给Applet
public static boolean Send(HttpServletResponse res,String strres){
try{
res.setContentType("text/plain");
res.setHeader("Pragma", "no-cache");
DataOutputStream dos = new DataOutputStream(res.getOutputStream());
dos.writeUTF(strres);
dos.flush();
dos.close();
return true;
}catch(Exception e){e.printStackTrace();return false;}
}
}2、Applet的基础类
//:AppletSide.javapackage citi.project.a2s;
import java.net.*;
import java.io.*;
import java.util.*;public class AppletSide{
//String strurl="http://localhost:8080/tsc/A2SServlet";//样例
//发送数据给Servlet,并读回数据。数据存放在strreq中
public String SendAndRead(String strurl,String strreq){
try{
URL url=new URL(strurl);
URLConnection ucon=url.openConnection();
ucon.setDoOutput(true);
ucon.setDoInput(true);
//发送数据,即写数据到数据流中。数据是UTF-8格式
DataOutputStream dos = new DataOutputStream(ucon.getOutputStream());
dos.writeBytes(strreq);
dos.close();
//读数据,即从数据流中读数据
DataInputStream dis = new DataInputStream(ucon.getInputStream());
String temp = dis.readUTF();
dis.close();
return temp;
}catch(Exception e){e.printStackTrace();return new String();}
}
//发送数据给Servlet,并读回数据。数据存放在reqs中
public String SendAndRead(String strurl,Properties reqs){
try{
String req=new String();
String item=new String();
Enumeration eall=reqs.propertyNames();
//生成数据,并使用UTF-8格式编码
while(eall.hasMoreElements()){
item=(String)eall.nextElement();
req=req+item +"="+ URLEncoder.encode(reqs.getProperty(item),"UTF-8")+"&";
//System.out.println(req);
}
req=req.substring(0,req.length()-1);
//System.out.println(req);
return SendAndRead(strurl,req);
}catch(Exception e){e.printStackTrace();return new String();}
}
//发送数据给Servlet,并读回数据。数据存放在reqsvalue中
public String SendAndRead(String strurl,String[] reqskey,String[] reqsvalue){
try{
String req=new String();
int i=0;
//生成数据,并使用UTF-8格式编码
for(;i<reqskey.length-1;i++)
req=req+reqskey[i]+"="+URLEncoder.encode(reqsvalue[i],"UTF-8")+"&";
req=req+reqskey[i]+"="+URLEncoder.encode(reqsvalue[i],"UTF-8");
return SendAndRead(strurl,req);
}catch(Exception e){e.printStackTrace();return new String();}
}
}3、测试Servlet源代码
//:A2SServlet.java
//**** Servlet ***************************************
package test.a2s;
import citi.project.a2s.*;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;//在服务器端需要将此Servlet注册
public final class A2SServlet extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
try{
//接收请求后,从请求中解析出所需参数
String name = req.getParameter("name");
String pwd = req.getParameter("password"); System.out.println("name:"+name);
System.out.println("pwd:"+pwd);
//简单返回给Applet一个字符串
ServletSide.Send(res,name+pwd+"kaokao");
}catch(Exception e){e.printStackTrace();}
}
public void doPost(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res) throws javax.servlet.ServletException, java.io.IOException {
doGet(req,res);
}
}4、测试Applet的源代码
//:A2SApplet.java
//**** Applet ***************************************
package test.a2s;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import citi.project.a2s.*;
import java.util.*;public class A2SApplet extends Applet {
JButton
b1 = new JButton("JButton 1"),
b2 = new JButton("JButton 2");
JTextArea t = new JTextArea(20,20);
public void init() {
ActionListener al = new ActionListener() {
public void actionPerformed(ActionEvent e){
String name = ((JButton)e.getSource()).getText();
//t.setText(doit());
t.setText(getCodeBase().toString());
}
};
b1.addActionListener(al);
add(b1);
b2.addActionListener(al);
add(b2);
add(t);
}
private String doit(){
//A2SServlet是在服务器端注册的Servlet
String strurl="http://localhost:8080/tsc/A2SServlet";
Properties p=new Properties();
p.setProperty("co","0");
p.setProperty("m","0");
return new AppletSide().SendAndRead(strurl,p);
}
public static void main(String args[]) {
A2SApplet applet = new A2SApplet();
JFrame frame = new JFrame("TextAreaNew");
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
frame.getContentPane().add(
applet, BorderLayout.CENTER);
frame.setSize(300,100);
applet.init();
applet.start();
frame.setVisible(true);
}
} ///:~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货