有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java通过文档生成器提交post请求

Document xmlDocument = builder.parse(request.getInputStream());

这里的请求是邮寄的; 但是我无法在servlet中获取请求的进程。 抛出以下异常

org.xml.sax.SAXParseException: Content is not allowed in prolog.
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:98)

谁能告诉我答案吗

提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    Document xmlDocument = builder.parse(request.getInputStream());
    

    此Java代码用于解析XML

    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    this.xmlHttp.open("POST",url, this.async);
    this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    this.xmlHttp.send("sessionid=324trt");
    

    此浏览器JavaScript代码发送application/x-www-form-urlencoded数据。有效负载sessionid=324trt不是XML

    读取参数的正确方法是via the parameter map

    String sessionid = request.getParameter("sessionid");
    
  2. # 2 楼答案

    按照上面@McDowell的建议,将xml作为post参数发布。然后读取servlet中的参数并执行此操作

    String postedXml = request.getParameter("postedXml");
    StringReader reader = new StringReader( postedXml );
    InputSource inputSource = new InputSource( reader );
    Document doc = builder.parse( inputSource );
    reader.close();