Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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 Header1.java
  • Now edit the code of the Header1.java 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");
            System.exit(1);
        }

        try {
            MessageFactory messageFactory = null;
            if (version.equals("1.1")) {
                messageFactory = MessageFactory.newInstance();
            } else {
                messageFactory = MessageFactory.newInstance(
                            SOAPConstants.SOAP_1_2_PROTOCOL);
            }
            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")) {
                orderHeader.setActor("http://www.roseindia.net/training");
            } else {
                orderHeader.setRole("http://www.roseindia.net/training");
            }

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

            if (version.equals("1.1")) {
                shippingHeader.setActor("http://www.roseindia.net/ejb/");
            } else {
                shippingHeader.setRole("http://www.roseindia.net/ejb/");
            }
            QName xml = new QName(
                        nameSpaceURI,
                        "xmlDesk",
                        nameSpace);
            SOAPHeaderElement confirmationHeader = header.addHeaderElement(
                        xml);

            if (version.equals("1.1")) {
                confirmationHeader.setActor("http://www.roseindia.net/xml/");
            } else {
                confirmationHeader.setRole("http://www.roseindia.net/xml/");
            }

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

            if (version.equals("1.1")) {
                billingHeader.setActor("http://www.roseindia.net/struts/");
            } else {
                billingHeader.setRole("http://www.roseindia.net/struts/");
            }
            if (version.equals("1.2")) {
                billingHeader.setRelay(true);
            }
            SOAPBody body = message.getSOAPBody();
            message.saveChanges();
            System.out.println("\n----- Request Message ----\n");
            message.writeTo(System.out);
            System.out.println();
            Iterator allHeaders = header.examineAllHeaderElements();
            while (allHeaders.hasNext()) {
                SOAPHeaderElement headerElement = (SOAPHeaderElement) allHeaders
                    .next();
                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());
                }

                System.out.println(
                        "mustUnderstand is "
                        + headerElement.getMustUnderstand());

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

Running the file <
  • After creation of the Header1.java and Editing save it .
  • Run the file.Right Click and select run file in Header1.java
  • 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

                         
» View all related tutorials
Related Tags: c gui web ide netbeans ui service help services vi deploy id tool developer oo beans to developers bean e

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.