繁体   English   中英

小程序Servlet通信

[英]applet servlet communication

我正在尝试读取jsp中的xml,并通过网络将与char []相同的内容传递给applet,但是我正在获取java.io.StreamCorruptedException:无效的流头:3C3F786D

我的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 = new FileReader(new File(filepath));
    char bin[] = new char[(int) new File(filepath).length()];
    fis.read(bin);
    response.getWriter().write(bin);
    fis.close();
%>
</body>
</html>

我的小程序代码:

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;
    }

}

您需要为服务器上的响应将上下文类型设置为“ text / xml”。

您的代码中有很多问题:

  • 您正在将HTML页面内的XML发送给客户端。 您不应该在XML周围使用html标记。 否则,它不再是XML。
  • 您在JSP中使用scriptlet。 应该使用JSP来生成标记,而别无其他。 将读取请求参数,读取文件等的代码放在Servlet中,可以在Java Servlet中正确编写,解析,重构和设计Java代码,而不是在JSP中。 JSP应包含标记,JSP标记和EL表​​达式。 不是Java代码。
  • 您正在使用平台默认编码来读取XML文件,该XML文件可能是以其他某种编码进行编码的。 您应该以字节为单位读取XML文件,并将其发送到响应OutputStream。 XML文件定义了其编码,因此接收方的XML解析器可以使用适当的编码将字节流转换为XML文档。
  • 您没有正确读取文件。 单独调用fis.read并不能保证已读取整个文件。 阅读您正在使用的方法的javadoc,并获取有关IO的Java教程。
  • 您正在使用ObjectOutputStream发送HTTP请求参数。 ObjectOutputStream用于发送序列化的对象。 它不用于发送HTTP参数。 该URL应该为http://localhost:8080/docRuleTool/XmlResponseReading.jsp?xmlname=Malaria ,并且您不应在连接的输出流中发送任何内容。 您应该了解HTTP的工作原理。
  • 从输入流(在applet中)读取的代码也是错误的。 阅读有关IO的Java教程。
  • 再次,在applet中,您使用默认的平台编码将字节数组转换为String。 使用XML解析器读取XML。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM