开发者

applet servlet communication

I am trying to read a xml in the jsp and pass the same over network as char[] to the applet but i am getting java.io.StreamCorruptedException : invalid stream header :3C3F786D

my jsp :

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "java.util.*" %> 
<%@ page import = "java.io.*" %> 
<%@ page trimDirectiveWhitespaces="true" %> 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%  String xmlname=(String)request.getAttribute("xmlname");
     int ch;    
    System.out.println("the value of the xml is "+xmlname);
    String filepath="C:/Users/ashutosh_k/idoc/docRuleTool/WebContent/data/Malaria.xml";
    FileReader fis = ne开发者_StackOverflow中文版w FileReader(new File(filepath));
    char bin[] = new char[(int) new File(filepath).length()];
    fis.read(bin);
    response.getWriter().write(bin);
    fis.close();
%>
</body>
</html>

My applet code :

package com.vaannila.utility;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import prefuse.util.ui.JPrefuseApplet;

public class dynamicTreeApplet extends JPrefuseApplet {

    private static final long serialVersionUID = 1L;
    public static int i = 1;

    public void init() {
        System.out.println("the value of i is " + i);
        URL url = null;
        try {
            url = new URL("http://localhost:8080/docRuleTool/XmlResponseReading.jsp");
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setUseCaches(false);
            //con.setRequestProperty("Content-TYpe", "application/octet-stream");
            ObjectOutputStream oos =  new ObjectOutputStream(con.getOutputStream());
            oos.writeObject("Malaria");
            oos.flush();
            oos.close();
            InputStream ois =  con.getInputStream();
        //  ByteArrayOutputStream bos = new ByteArrayOutputStream();
            while (true) {
                byte b[] = new byte[1024];
                int retval = ois.read(b);
                if (retval < b.length) {
                    if (retval > 0) {
                        byte b1[] = new byte[retval];
                        System.arraycopy(b, 0, b1, 0, retval);

                        ois.read(b1);
                        System.out.println(new String(b1));
                    }
                    break;
                } else {
                    ois.read(b);
                    System.out.println(new String(b));

                }

            }

//          ByteArrayInputStream bis = new ByteArrayInputStream(ois.toByteArray());
            this.setContentPane(dynamicView.demo(ois, "name"));
            ois.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException f) {
            f.printStackTrace();

        } catch (IOException io) {
            io.printStackTrace();
        }
        ++i;
    }

}


You have many many problems in your code:

  • You're sending XML inside an HTML page to your client. You shouldn't have html markup around your XML. Else it's not XML anymore.
  • you're using scriptlets in your JSPs. JSPs should be used to generate markup, and nothing else. Put the code that reads request parameters, reads a file, etc. in a servlet, where Java code can be properly written, parsed, refactored and designed rather than in JSPs. JSPs should contain markup, JSP tags and EL expressions. Not Java code.
  • You're using the platforme default encoding to read an XML file, which is perhaps encoded in some other encoding. You should read the XML file as bytes, and send it to the response OutputStream. An XML file defines its encoding, so the receiver's XML parser can use the appropriate encoding to transform the stream of bytes into an XML document
  • You don't read the file properly. A single call to fis.read doesn't guarantee that the whole file has been read. Read the javadoc of the methods you're using, and rea the Java tutorial about IO.
  • You're using an ObjectOutputStream to send an HTTP request parameter. An ObjectOutputStream is used to send serialized objects. It's not used to send HTTP parameters. The URL should be http://localhost:8080/docRuleTool/XmlResponseReading.jsp?xmlname=Malaria, and you shouldn't send anything in the connection's output stream. You should learn how HTTP works.
  • the code which reads from the inputstream (in the applet) is also wrong. Read the Java tutorial about IO.
  • once again, in the applet, you're using the default platform encoding to transform a byte array into a String. Use an XML parser to read your XML.


You need to set the context type to 'text/xml' for the response on the server.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜