[an error occurred while processing this directive]

SOAP Header


Project Requirement

Make a project in netbeans.In it develop an application that gives create a SOAP message along with several headers (use SOAP 1.1 here)

Solution :

SOAP Header is the optional Element in SOAP Message . It contains data for application in XML form.For example, transaction semantics, authentication information, and so on.It can be specified as the content of a SOAPHeader object.

Create a java application project

SOAP Header

                                                    Figure. 1

SOAP Header

Figure 2


Creating the Java File

SOAP Header

   Figure 3

SOAP Header

         Figure. 4

package pack2;
import javax.xml.soap.*;
import java.util.*;
import javax.xml.namespace.QName;
public class Header1 {

    public static void main(String[] args) {
           String version = "1.2";
        System.out.println("SOAP version is " + version);

        if (!(version.equals("1.1") || version.equals("1.2"))) {
            System.err.println("Value must be 1.1 or 1.2");

        try {
            MessageFactory messageFactory = null;
            if (version.equals("1.1")) {
                messageFactory = MessageFactory.newInstance();
            } else {
                messageFactory = MessageFactory.newInstance(
            SOAPMessage message = messageFactory.createMessage();
            SOAPHeader header = message.getSOAPHeader();
            String nameSpace = "ns";
            String nameSpaceURI = "http://www.roseindia.net";
            QName train1 = new QName(nameSpaceURI, "trainDesk", nameSpace);
            SOAPHeaderElement orderHeader = header.addHeaderElement(train1);

            if (version.equals("1.1")) {
            } else {

            QName ejb = new QName(nameSpaceURI, "ejbDesk", nameSpace);
            SOAPHeaderElement shippingHeader = header.addHeaderElement(

            if (version.equals("1.1")) {
            } else {
            QName xml = new QName(
            SOAPHeaderElement confirmationHeader = header.addHeaderElement(

            if (version.equals("1.1")) {
            } else {

            QName struts = new QName(nameSpaceURI, "strutsDesk", nameSpace);
            SOAPHeaderElement billingHeader = header.addHeaderElement(struts);

            if (version.equals("1.1")) {
            } else {
            if (version.equals("1.2")) {
            SOAPBody body = message.getSOAPBody();
            System.out.println("\n----- Request Message ----\n");
            Iterator allHeaders = header.examineAllHeaderElements();
            while (allHeaders.hasNext()) {
                SOAPHeaderElement headerElement = (SOAPHeaderElement) allHeaders
                QName headerName = headerElement.getElementQName();
                System.out.println("\nHeader name is " + headerName.toString());

                if (version.equals("1.1")) {
                    System.out.println("Actor is " + headerElement.getActor());
                } else {
                    System.out.println("Role is " + headerElement.getRole());

                        "mustUnderstand is "
                        + headerElement.getMustUnderstand());

                if (version.equals("1.2")) {
                    System.out.println("relay is " + headerElement.getRelay());
        } catch (Exception ex) {

Running the file <

Figure. 5

APIs used in this project

MessageFactory:- It is a factory for creating SOAPMessage objects
A MessageFactory object  is created by  newInstance () method in SAAJ client file.
SOAPMessage:- It is the root class for all SOAP messages.
A SOAPMessage object consists of a SOAP part and optionally one or more attachment parts. The SOAP part for a SOAPMessage object is a SOAPPart object.  SOAP part contains information used for message routing and identification. All data in the SOAP Part of a message must be in XML format. 
SOAPHeader:- SOAP Header is the optional Element. It contains data for application in XML form.For example, transaction semantics, authentication information, and so on, can be specified as the content of a SOAPHeader object
SOAPBody :- It is the required Element of the SOAP.It has the main message contents in the XML form.
Qname:- This class  represents a qualified name as defined in the XML specifications
SOAPHeaderElement:-This object represents the contents in the SOAP header part of the SOAP envelope.It is used after SOAP Header.So the children of a SOAPHeader object can be represented only as SOAPHeaderElement objects. A SOAPHeaderElement object can have other SOAPElement objects as its children.

Download Code

[an error occurred while processing this directive]