各位大牛:
  如何根据Servlet 来做xml 的请求分发。举例说明:一个第三方的公司请求我写的一个Servlet ,它传过来一个参数,这个参数代表是请求什么类型的数据,请求的内容是一个XML文件的格式,比如type=1是用户信息,我在接收的时候根据参数则调用 解析用户信息XML 的类,这样的代码如何在Servlet写以及当参数没传给我,如何将提示信息返回给第三方公司,不知道我描述清楚了没有,求指教,在线等XMLDOMServlet

解决方案 »

  1.   

    xml进行数据传输,是J2EE的一种标准。也就是用上层http传协议。通过request,response来实现。我给个例子
    package nc.plugin.test;  /** 
     * 此处插入类型说明。 
     * 创建日期:(2004-12-5 15:49:22) 
     * @author:Administrator 
     */ 
     import java.lang.*; 
     import java.util.*; 
     import java.sql.*; 
     import java.io.*; 
     import java.net.*;  public class PostFile { 
     HttpURLConnection connection = null; 
     /** 
     * PostFile 构造子注解。 
     */ 
    public PostFile() { 
     super(); 
     } 
     /** 
     * PostFile 构造子注解。 
     */ 
     //获得HTTP连接 
    public HttpURLConnection getConnection(String url) throws Exception{                  try { 
     URL realURL = new URL(url); 
     URLConnection conn = realURL.openConnection();  conn.setRequestProperty("Content-type", "text/xml"); 
     connection = (HttpURLConnection) conn; 
     connection.setDoOutput(true); 
     connection.setRequestMethod("POST"); 
     System.out.println("获得连接"+url); 
     } catch (MalformedURLException mex) { 
     mex.printStackTrace(); 
     throw mex; 
     } catch (ProtocolException pex) { 
     pex.printStackTrace(); 
     throw pex; 
     } catch (IOException iex) { 
     iex.printStackTrace(); 
     throw iex; 
     } 
     return connection; 
     }  //调用过程,方法入口. 
    public static void main(String[] args){ 
     PostFile pf = new PostFile();  String str=null; 
     String url="http://192.168.16.46:9088/service/XChangeServlet?account=004&receiver=1@1-0002"; 
     String[] m_allSelectedFiles = {"D:/voucher/101200012009070063.xml"};  for (int i = 0; i < m_allSelectedFiles.length; i++) { 
     //获得连接 
    try{ 
     pf.getConnection(url); 
     pf.sendFile(new File(m_allSelectedFiles[i]),"gb2312"); 
     str += "\n" + pf.receiveResponse("gb2312");  }catch(Exception e){ 
     System.out.println("getConnection Exception"); 
     System.out.println("aaaaaaaaaaaaaa"); 
     } 
     //发送文件 
     //接收回执 System.out.println(str); 
     } // end for 
     }  //发送文件 
    public BufferedReader readFile(String file) throws Exception{ 
     BufferedReader in1=null; 
     try { 
     //从文件获得输入 
    in1=new BufferedReader(new InputStreamReader(new FileInputStream(file),"gb2312")); 
     //循环获得文件中的内容,并out发出  } catch (Exception ex) { 
     ex.printStackTrace(); 
     throw ex; 
     } 
     return in1; 
     } 
     //获取数据响应 
    public String receiveResponse(String output_coding) { 
     BufferedReader in; 
     StringBuffer response = new StringBuffer(); 
     //File file=null; 
     try { 
     //此处获得响应回执 
                 in = new BufferedReader(new InputStreamReader(connection.getInputStream(),output_coding)); 
     String line = "";  while ((line = in.readLine()) != null) { 
     //line = new String(line.getBytes("utf-8"),"gb2312"); 
     response.append(line + "\n"); 
     //System.out.println(line); 
     } 
     in.close(); 
     } catch (Exception exception) { 
     exception.printStackTrace(); 
     } 
     return response.toString(); 
     } 
     //发送文件 
    public void sendFile(File file,java.lang.String input_coding) throws Exception{ 
     try { 
     PrintWriter out = new PrintWriter(connection.getOutputStream());//获得一个输出流 
     //从文件获得输入 
    BufferedReader in1=new BufferedReader(new InputStreamReader(new FileInputStream(file),input_coding)); 
     //循环获得文件中的内容,并out发出 String s; 
     while ((s = in1.readLine()) != null) { 
     out.println(s);  out.flush(); 
     } 
     out.close(); 
                 System.out.println("已经成功发送XML文件!"); 
     }catch (UnknownHostException uhe){ 
     uhe.printStackTrace(); 
     throw uhe; 
     } catch (Exception ex) { 
     ex.printStackTrace(); 
     throw ex; 
     } 
     }  } 
    这是用http调用servlet的方法. 
    服务器端写一个servlet处理xml文件就行. 
      

  2.   


    不是实时处理。那你的觉得什么困难?不知道参数是什么还是参数是否传过来,再者就是把处理结果返回给请求方这步?我的意思很简单,当有请求过来时,我要在Servlet根据请求参数跳转到相应的业务实现类,xml 请求过来,我要做什么??
      

  3.   


    不是实时处理。那你的觉得什么困难?不知道参数是什么还是参数是否传过来,再者就是把处理结果返回给请求方这步?我的意思很简单,当有请求过来时,我要在Servlet根据请求参数跳转到相应的业务实现类,xml 请求过来,我要做什么??没明白难点在哪。你所有的业务实现类应该有个统一接口,这个接口定义了一个方法用来解析XML,不同的业务此方法实现不同。servlet内部,你根据参数 new 不同的业务对象 给这个接口,接口调那个解析方法就可以了。 BusinessInterface business;
    if("type1".equals(param)){
    business = new BusinessType1();
    }else{
    business = new BusinessType2();
    }

    business.doBusiness();
      

  4.   


    不是实时处理。那你的觉得什么困难?不知道参数是什么还是参数是否传过来,再者就是把处理结果返回给请求方这步?我的意思很简单,当有请求过来时,我要在Servlet根据请求参数跳转到相应的业务实现类,xml 请求过来,我要做什么??没明白难点在哪。你所有的业务实现类应该有个统一接口,这个接口定义了一个方法用来解析XML,不同的业务此方法实现不同。servlet内部,你根据参数 new 不同的业务对象 给这个接口,接口调那个解析方法就可以了。 BusinessInterface business;
    if("type1".equals(param)){
    business = new BusinessType1();
    }else{
    business = new BusinessType2();
    }

    business.doBusiness();

    是的,情况就是你说的这样,但是代码写不出来啊,求代码。。
      

  5.   

    beichui 讲的很明显了,都是伪代码了,最简单的if..else..然后里面执行不同的逻辑。
      

  6.   


    我要的就是servlet 处理XML 的例子。
      

  7.   

    首先解析xml啊,获得type值,根据不同的值调用不同的业务方法解析xml的方法百度下,一大堆的