码上敲享录 > java常见报错解答 > java的webservice使用PrintWriter打印的问题

java的webservice使用PrintWriter打印的问题

上一章章节目录下一章 2019-02-27已有1637人阅读 评论(0)

java的webservice使用PrintWriter打印的问题


解决方法:

在java的webservice的方法中使用以下代码时且我的方法没返回值void,但还是报错java.lang.IllegalStateException: getWriter() has already been called for this response


response.setContentType(request.getContentType());

response.setHeader("Pragma", "No-cache");

response.setHeader("Cache-Control", "no-cache");

response.setCharacterEncoding(request.getCharacterEncoding());

PrintWriter out= null;

try {

out = response.getWriter();

out.print("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/


\"><soap:Header/><soap:Body><ns2:sendMsgResponse xmlns:ns2=\"http://test.com/\"><_return><entry><key>respdesc</key><value xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"


xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xs:string\">成功</value></entry></_return></ns2:sendMsgResponse></soap:Body></soap:Envelope>");

out.flush();

out.close();

} catch (IOException e) {

e.printStackTrace();

}


修改为:

response.setContentType("text/xml;charset=utf-8");

    response.setCharacterEncoding("UTF-8");

    OutputStream out = response.getOutputStream();

        out.write("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Header/><soap:Body><ns2:sendMsgResponse xmlns:ns2=


\"http://test.com/\"><_return><entry><key>respdesc</key><value xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"


xsi:type=\"xs:string\">成功</value></entry></_return></ns2:sendMsgResponse></soap:Body></soap:Envelope>".getBytes("UTF-8"));

        out.flush();

out.close();


0

有建议,请留言!

  • *您的姓名:

  • *所在城市:

  • *您的联系电话:

    *您的QQ:

  • 咨询问题:

  • 提 交