The Servlet Sender and Receiver Xbeans

Function

The Servlet Sender is a sink Xbean that sends a DOM document to a Servlet Receiver. The Servlet Receiver is a source Xbean that receives the DOM document and passes it on to the next Xbean in the Xbean channel. The Servlet Receiver can be deployed in any J2EE compliant web container, such as Tomcat, Resin, Weblogic, etc.

Servlet Sender XBean Properties

id The URL of the servlet receiver Xbean. 

 compression

Setting this property to true causes the sender to compress the document using GZIP compression prior to sending it. 

Servlet Receiver XBean

The servlet receiver does not have any Java Bean properties. Instead, all configuration is done in the web.xml file.

id

The servlet's URL is established in the web.xml file using the <servlet-mapping> tag.

 compression

Compression is determined by examining the URL used to invoke the servlet. If the URL ends in ".compressed", then the servlet decompresses the incoming data. 
DOMListener The DOMListener is set in web.xml as a servlet initialization parameter, domListener. If you have a single Xbean as a DOMListener, then you can set the parameter to be that Xbean. If on the other hand, you have several Xbeans forming a channel, you need to write a class that implements the DOMListener interface, instantiates and configures the channel and calls the first Xbean in the channel when documentReady is called by the receiver servlet.

Sample web.xml file

The Xbeans release comes with a sample web.xml file for deploying the servlet receiver. The file is in tests/source/WEB-INF/web.xml and shown below. Note the setting of the domListener in the <init-param> tag and the two servlet mappings, one for a non-compressed document and the other for a compressed document.

<web-app>
  <display-name>Xbean Servlet Demo</display-name>
  <description>Demonstrates Xbean receiver for servlets</description>
  <servlet>
    <servlet-name>receiver-servlet</servlet-name>
    <display-name>Xbean ReceiverServlet</display-name>
    <description>Xbean receiver for servlets</description>
    <servlet-class>org.xbeans.communication.servlet.receiver.ReceiverServlet</servlet-class>
    <init-param>
      <param-name>domListener</param-name>
      <param-value>org.xbeans.tests.ReceiverDemoClass</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>receiver-servlet</servlet-name>
    <url-pattern>/receiver</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>receiver-servlet</servlet-name>
    <url-pattern>/receiver.compressed</url-pattern>
  </servlet-mapping>
</web-app>

Using the Servlet Sender

The sender Xbean is in the org.xbeans.communication.servlet.sender package. The sender can be imported into Java source code as follows:

import org.xbeans.communication.servlet.sender.SenderBean;

Creating a sender Xbean is accomplished by instantiating the class:

SenderBean sender = new SenderBean();

Note that if you are using a Java Bean design tools found in many Java Integrated Development Environments, the above code is generated automatically when you drag and drop a sender Xbean into your application.

After creating the sender Xbean, you must set the id property. For example:

sender.setId("http://localhost:8080/ReceiverDemo/receiver");

Your URL may differ depending on how the receiver servlet is deployed.

You need to get a DOM document by registering the sender with the previous Xbean in the channel.

previousXbean.setDOMListener(sender);

Again, if you are using a Java Bean design tool, the above calls to set properties may be automatically generated for you.

Source files

source/org/xbeans/communication/servlet/*.java
tests/source/WEB-INF/web.xml