给你个例子,看看吧
servlet端
/**
 * type class' function
 * 
 * @author Likestar
 */
public class ServletToApplet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("GBK");
try {
PrintWriter out = response.getWriter();
out.println(" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/octest-stream");
try {
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
Object obj = in.readObject();
in.close();
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject("成功");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(obj);
} public static void main(String[] args) {
}
}

解决方案 »

  1.   

    applet端
    /**
     * type class' function
     * 
     * @author Likestar
     */
    public class AppletToServlet extends Applet { private String msg = "你好"; public void init() {
    Button but = new Button("测试");
    but.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try {
    URL url = new URL(getDocumentBase(),
    "servlet/ServletToApplet");
    System.out.println(url);
    URLConnection con = url.openConnection();
    con.setUseCaches(true);
    con.setDoOutput(true);
    con.setDoInput(true);
    con.setRequestProperty("Content-type",
    "application/octest-stream");
    sendServletObject(con,"真实的世界");
    System.out.println("发送信息");
    msg=(String) getServletObject(con);
    repaint();
    } catch (MalformedURLException e1) {
    e1.printStackTrace();
    } catch (IOException e2) {
    e2.printStackTrace();
    }
    } });
    add(but);
    } public void paint(Graphics g) {
    g.drawString(msg, 20, 20);
    } /**
     * 给Servlet传送对象
     * 
     * @param output
     * @param obj
     * @throws IOException
     */
    public void sendServletObject(URLConnection con, Object obj)
    throws IOException {
    OutputStream output = con.getOutputStream();
    ObjectOutputStream dataout = new ObjectOutputStream(output);
    dataout.writeObject(obj);
    dataout.flush();
    dataout.close();
    }
    /**
     * 获得Servlet的传送对象
     * 
     * @param input
     * @return @throws
     *         IOException
     */
    public Object getServletObject(URLConnection con) throws IOException {
    InputStream input = con.getInputStream();
    ObjectInputStream datain = new ObjectInputStream(input);
    Object obj = null;
    try {
    obj = datain.readObject();
    } catch (ClassNotFoundException e) {
    e.printStackTrace();
    System.out.println("对象读取错误!");
    }
    datain.close();
    return obj;
    }
    }