Share on Google+Share on Google+

SOAP Header

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)

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 :

  • Create a java application project
  • Create a java class file
  • Add the code for Header
  • Make Compilation of the file
  • Build the project
  • Run the project

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
  • Take a new Java Application   Project as given in below in Figure1

SOAP Header

    Figure. 1

  • Give the project name as SOAP-Header as given in below in Figure2

SOAP Header

Figure 2


  • Click on the finish button. This creates a Java Application project.
Creating the Java File
  • Now create a new java file.
  • Right click on the project
  • Select the new java Class as shown in below  Figure 3

SOAP Header

 Figure 3

  • Give the name of the java class as Header1.
  • Give the package name as pack2 as given below in Fig 4

SOAP Header

   Figure. 4

  • Click on the finish .It will create the file
  • Now edit the code of the as shown   below.

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 = "";
  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 <
  • After creation of the and Editing save it .
  • Run the file.Right Click and select run file in
  • You will see the output as given below in fig 5.
  • SOAP Header

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




Posted on: October 21, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: SOAP Header  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
vijay kumar
January 7, 2012

Actualy i am new at this project, my project is related to soc Right now i want to know about soap header that has been used in this project, i mean what is the use of this ,, thank you