applet能否发送一个对象到jsp,如果可以的话,也许能解决上面的问题

解决方案 »

  1.   

    1。你使用如下方法试试。对你的问题我还不太明白。
                String result = .....;            PrintWriter out = new PrintWriter(resp.getWriter());
                out.write(result);
                out.flush();
                out.close();下面附上我处理applet和servlet数据接口。传输序列化对象,用ArrayList封装。
    servlet:
        public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            try {
                ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(req.getInputStream()));            String strAction = (String)ois.readObject();
                ArrayList inList = (ArrayList)ois.readObject();
                ois.close();            ArrayList outList = processPostAction(req,strAction,inList);            ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(resp.getOutputStream()));
                oos.writeObject(outList);
                oos.flush();
                oos.close();        } catch(ClassNotFoundException cnfe) {
                cnfe.printStackTrace();
            }
        }applet:
        private ArrayList sendMessage(String name,ArrayList list) throws Exception {
            URL url = new URL(strBaseUrl);        URLConnection con = url.openConnection();        //Initialize the connection
            con.setUseCaches(false);
            con.setDoOutput(true);
            con.setDoInput(true);        //Set the content that we are sending
            con.setRequestProperty("Content-type", "application/octet-stream");        //Get the output stream to the server and send our data buffer
            ObjectOutputStream oos = new ObjectOutputStream(
                    new BufferedOutputStream(con.getOutputStream()));
            oos.writeObject(name);
            if(list == null) {
                list = new ArrayList();
            }
            oos.writeObject(list);        //Flush the output stream and close it
            oos.flush();
            oos.close();        //Get the input stream we can use to read the response
            ObjectInputStream ois = new ObjectInputStream(
                    new BufferedInputStream(con.getInputStream()));        ArrayList retList = (ArrayList)ois.readObject();        //Close the input stream
            ois.close();        return retList;
        }
    2.test.xsl好像要在webapps下找吧,不清楚。3。       
    con.setRequestProperty("Content-type", "application/octet-stream");
    就会触发servlet的doPost动作。
      

  2.   

    applet中的注释不对,有些方法已删了,注释还没删。
      

  3.   

    to nil2000(我爱北京天安门) :
    非常感谢,对于第2个问题,需要使用getRealPath来确定路径
    对于第1个问题,你的servlet接收了一个ArrayList对象,经过处理后又向applet返回了一个ArrayList对象,这样做是没有问题的,但我的servlet接收一个对象后,经过处理,在服务器端生成了一个htm临时文件(每次调用servlet都要生成一个临时文件,服务器上有太多的垃圾),所以我就想生成一个数据流,直接发送到applet,但是applet如何接收呢?接收到以后该如何处理呢?
      

  4.   

    servlet和applet接收发送的动作是一一对应的,你的servlet生成一个htm临时文件,或者是一个String,可以写到servlet的response的输出流中,同时,在applet中必须写相应的接收流动作(又con.getInputStream()得到),两端必须一致,如果是对象传输,两端都要是对象流。如:
    servlet发送htm数据
            String strHtm = ....;
            PrintWriter out = new PrintWriter(new BufferedOutputStream(resp.getOutputStream()));
            out.write(strHtm);
            out.flush();
            out.close();applet接收htm数据
    ...
            URLConnection con = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(new BufferedInputStream(con.getInputStream())));
            StringBuffer buff = new StringBuffer();
            String str = null;
            while((str = in.readLine()) != null) {
                buff.append(str);
            }
            String strHtm = buff.toString();
            in.close();
    至于接收的strHtm要如何处理,就看你的业务逻辑了,只个我帮不上忙。