Monday, July 23, 2012

Invoking the Servlet through HttpAdapter with xml input/output in Oracle SOA Suite

Invoking the Servlet through HttpAdapter with xml input/output in Oracle SOA Suite

Sometimes we may require invoking the Java servlet with XML as input/output; this blog will explain how to invoke the servlet with XML as input/output  through HttpAdapter  in Oracle SOA Suite 11g. The same will work in Oracle SOA Suite 12c - the screens will be little different.

The sample java servlet will receive the emp name and emp no as XML input and return the employee details as XML output.

Defining the HttpAdapter to invoke the servlet:

  • Create a composite with Synchronous BPEL
  • Define the HttpAdapter as shown below
  • Make sure the configuration are defined as below

  • Define the Input/output schema as per the requirement.
  • Deploy the composite

Retrieving the XML in Servlet:

The below code snippet will help us to retrieve the XML payload in the Servlet

    public void doPost(HttpServletRequest request,
                       HttpServletResponse response) throws ServletException,
                                                            IOException {
        PrintWriter out = response.getWriter();
        BufferedReader br = request.getReader(); 
            String line = null; 
            StringBuffer inputXML = new StringBuffer(); 
            PrintWriter pout = response.getWriter(); 
            while ((line = br.readLine()) != null) { 
            System.out.println("Input "+inputXML);
           sbis = new;

        javax.xml.parsers.DocumentBuilderFactory b = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        org.w3c.dom.Document doc = null;
        javax.xml.parsers.DocumentBuilder db = null;

        try {
            db = b.newDocumentBuilder();
            doc = db.parse(sbis);
        } catch (Exception e) {
        org.w3c.dom.Element element = doc.getDocumentElement();
        String empName=getPayloadValue(element, "EmpName");
        String empNo=getPayloadValue(element, "EmpNo");     
       String output="<EmployeeCollection xmlns=\"\"><Employee><EmpName>"+empName+"</EmpName><EmpNo>"+empNo+"</EmpNo><EmpAge>27</EmpAge></Employee><Employee><EmpName>"+empName+"</EmpName><EmpNo>"+empNo+"</EmpNo><EmpAge>28</EmpAge></Employee></EmployeeCollection>";
    public String getPayloadValue(Element taskPayloadElement, String nodeName){
     String output= "";  
    NodeList nodeList = taskPayloadElement.getElementsByTagName(nodeName);
    if(nodeList!=null && nodeList.getLength() > 0){
    Element myElement = (Element)nodeList.item(0);
    output= myElement.getFirstChild().getNodeValue();
    return output;


DOWNLOAD HttpService.rar

No comments:

Post a Comment