protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=GBK");
PrintWriter printwriter = response.getWriter();

printwriter.println("<?xml version=\"1.0\" encoding=\"GB2312\"?>\n"); 
printwriter.println("<用户登录>\n"); 
printwriter.println(" <返回结果>\n");
printwriter.println(" <返回值>result</返回值>\n"); 
printwriter.println(" <错误描述>message</错误描述>\n"); 
printwriter.println(" </返回结果>\n");
printwriter.println("</用户登录>");
}如何在另一个服务器中接受到这servlet返回的XML

解决方案 »

  1.   

    你想用流来写?
    一般都是通过socket或者soap两种方式!
    socket是通过传输层来传输;soap是应用层
    你根据你的需求,这2中都可以
      

  2.   

    xml其实可以作为字符串传递的。看看dom4j
      

  3.   

    你们说的方法有些我知道,但这个XML是别人给我做的接口,我访问他给好的路径后在页面看到的就是XML,如何把这个打印出来的XML获取到,我们用的是不同的服务器
      

  4.   

    一、首先提一点建议:响应类型应该改为XML类型,因为你返回的页面你内容是xml结构的。 response.setContentType("application/xml; charset=GBK");二、获取该Servlet返回的内容,可采用如下代码:
    public class Test {
    public static void main(String[] args) {
    String content = ContentGetter.getContent("你需要访问的Servlet URI");
    System.out.println(content);
    }
    }import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.URL;
    import java.net.URLConnection;public class ContentGetter {    public static String getContent(String urlStr) {
            return getContent(urlStr, "UTF-8");
        }    public static String getContent(String urlStr, String encode) {
            BufferedReader reader = null;
            InputStream is = null;
            try {
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection();
                conn.setConnectTimeout(5000);
                conn.connect();
                is = conn.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, encode));
                StringBuffer buffer = new StringBuffer();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line);
                }
                return buffer.toString();
            } catch (IOException e) {
                return null;
            } finally {
                try {
                    if (reader != null)
                        reader.close();
                } catch (IOException e) {
                }
                try {
                    if (is != null)
                        is.close();
                } catch (IOException e) {
                }
            }
        }
    }