Applet向Servlet传递对象方法-已运行通过

自定义的测试类

public class aData implements Serializable{
  int d=100;
  public aData() {
  }
  public int getD()
  {
    return this.d;
  }
}

Applet 端程序

        try {
            URL url = new URL("http://127.0.0.1:8080/ServletTest");
            URLConnection urlcon = url.openConnection();
            urlcon.setDoOutput(true);
            urlcon.setDoInput(true);
            urlcon.setUseCaches(false);

            urlcon.setRequestProperty("Content-Type",
                                      "application/x-java-serialized-object");
            OutputStream out = urlcon.getOutputStream();
           
            ObjectOutputStream oos = new ObjectOutputStream(out);
            oos.writeObject(new aData());
            oos.flush();
            oos.close();
            urlcon.getHeaderField(0);//这一句非常重要,如果没有此句服务器端没反应
        } catch (Exception ex) {
            ex.printStackTrace();
        }

Servlet端程序

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.servlet.ServletException;

public class ServletTest extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
    //Initialize global variables
    public void init() throws ServletException {
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws
            ServletException, IOException {
        ObjectInputStream oin = new ObjectInputStream(req.getInputStream());
        aData vo = null;
        try {
            vo = (aData) oin.readObject();
            oin.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("============" + vo.getD());
    }

    public void doGet(HttpServletRequest req, HttpServletResponse response) throws
            ServletException, IOException {

        response.setContentType("application/x-java-serialized-object");

        doPost(req, response);
    }

    //Clean up resources
    public void destroy() {
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值