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();