Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : J2EE : EJB : Reading and Parsing XML Files with Enterprise JavaBeans
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC	"-//Sun	Microsystems,
	Inc.//DTD Enterprise JavaBeans 2.0//EN"
	"http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd">

<ejb-jar>
   <enterprise-beans>
    <session>
    <ejb-name>XMLReader</ejb-name>
    <home>com.stardeveloper.ejb.session.XMLReaderHome</home>
    <remote>com.stardeveloper.ejb.session.XMLReader</remote>
    <ejb-class>com.stardeveloper.ejb.session.XMLReaderEJB</ejb-class>
    <session-type>Stateless</session-type>
    <transaction-type>Container</transaction-type>
    </session>
   </enterprise-beans>

   <assembly-descriptor>
    <container-transaction>
    <method>
      <ejb-name>XMLReader</ejb-name>
      <method-name>*</method-name>
    </method>
    <trans-attribute>Supports</trans-attribute>
    </container-transaction>

    <security-role>
     <description>Users</description>
     <role-name>users</role-name>
    </security-role>
   </assembly-descriptor>
</ejb-jar>

Explanation :
I explained what ejb-jar.xml is all about in "Building your first Enterprise JavaBeans" article. So if you don't feel great to see the XML code above, consult that article.

viii. jboss.xml :
Till now all the files that we have created including the ejb-jar.xml file, are generic J2EE files. To deploy EJBs to a specific EJB container we have to add more XML files as required by that container. JBoss requires one additional XML file, 'jboss.xml' in the META-INF folder.

In the 'META-INF' folder create a new 'jboss.xml' file. The complete path to 'jboss.xml' should be :

 C:\Projects\EJB\XMLReaderEJB\META-INF\jboss.xml

jboss.xml :
Copy and paste the following text in 'jboss.xml' file :

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS//EN"
	"http://www.jboss.org/j2ee/dtd/jboss.dtd">
<jboss>
	<enterprise-beans>
		<session>
			<ejb-name>XMLReader</ejb-name>
			<jndi-name>ejb/XMLReader</jndi-name>
		</session>
	</enterprise-beans>
</jboss>

The text above simply tells the JBoss Server to bind the XMLReader Session bean with "ejb/XMLReader" JNDI name.

Downloading JDOM.jar file :
You'll have to download 'jdom-b7.tar.gz' file from JDOM web site. Current latest version is beta-7. Once downloaded, unzip it into a temporary folder. For example's sake I'll assume that you unzipped it into C:\jdom-b7 folder. Now copy following files from C:\jdom-b7 folder and paste them into C:\JBoss\lib\ext folder :

C:\jdom-b7\build\jdom.jar
C:\jdom-b7\lib\collections.jar
C:\jdom-b7\lib\crimson.jar
C:\jdom-b7\lib\jaxp.jar
C:\jdom-b7\lib\xalan.jar
C:\jdom-b7\lib\xerces.jar

Installation of JDOM libraries is done. Now stop and restart JBoss Server.

Creating XMLReaderEJB.jar JAR file :
As we learned earlier, EJB files have to be packaged inside a JAR file. To do that we'll have to first compile the Java source files we have created till now. Use following set of commands to do that :

C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\session>javac *.java
	-d C:\Projects\EJB\XMLReaderEJB
C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\util>javac *.java
	-d C:\Projects\EJB\XMLReaderEJB
C:\Projects\EJB\XMLReaderEJB\src\com\stardeveloper\ejb\session>javac *.java 
	-classpath C:\JBoss\client\jboss-j2ee.jar;C:\Projects\EJB\XMLReaderEJB;
		C:\JBoss\lib\ext\jdom.jar;C:\JBoss\lib\ext\xerces.jar 
	-d C:\Projects\EJB\XMLReaderEJB
Note: Make sure in the last command that the -classpath argument is all in one line i.e. there is no space between ..XMLReaderEJB; and C:\JBoss\lib\.. .

Note: The point to remember is to make sure jboss-j2ee.jar ( which contains J2EE package classes ), jdom.jar ( which contains JDOM files ), xerces.jar ( contains few SAX files required by JDOM ) and top most folder of our com.stardeveloper.* package is in the CLASSPATH, or you will get errors when trying to compile these classes.

To create a JAR file containing these class files and deployment descriptor XML files, run following command at command prompt :

C:\Projects\EJB\XMLReaderEJB>jar cvfM XMLReaderEJB.jar com META-INF

Now you should have 'XMLReaderEJB.jar' file under the C:\Projects\EJB\XMLReaderEJB folder.

Deploying XMLReaderEJB.jar on JBoss Server :
Start JBoss Server by using following command :

C:\JBoss\bin>run

Now copy XMLReaderEJB.jar file and paste it into the C:\JBoss\deploy folder. You should see some messages appearing on your JBoss Server command prompt window saying that XMLReaderEJB.jar has been deployed.

Now one more step remaining is to copy the same XMLReaderEJB.jar into the C:\Projects\TomcatJBoss\WEB-INF\lib folder ( in the WEB-INF/lib folder of your web app ). This is required for the Tomcat Server to know about our EJB.

ix. xmlReaderEJB.jsp :

Let's now create the client JSP page which will call the readXML() method of our XMLReaderEJB. Create a new 'xmlReaderEJB.jsp' page in the C:\Projects\TomcatJBoss folder ( in your web app folder ). We are using C:\Projects\TomcatJBoss because we had created this folder and other directory structures in "Building your first Enterprise JavaBean" article. You can substitute it with your own web app folder but if you get into trouble, don't forget to read "Building your first Enterprise JavaBean" article.

Copy and paste following code in it :

<%@ page	import="javax.naming.InitialContext,
				 javax.naming.Context,
				 java.util.Properties,
				 com.stardeveloper.ejb.session.XMLReaderHome,
				 com.stardeveloper.ejb.session.XMLReader,
				 com.stardeveloper.ejb.util.Contact,
				 java.io.File"%>
<%
	long t1	= System.currentTimeMillis();
	Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		props.put(Context.PROVIDER_URL,	"localhost:1099");

	Context	ctx	= new InitialContext(props);
	XMLReaderHome home = (XMLReaderHome)ctx.lookup("ejb/XMLReader");
	XMLReader bean = home.create();
	Contact[] contacts =
		bean.readXML("E:\\Projects\\TomcatJBoss\\xml\\Contacts.xml");
	bean.remove();
	ctx.close();
	long t2	= System.currentTimeMillis();
%>
<html>
<head>
	<style>
	p {	font-family:Verdana;font-size:12px;	}
	.contacts {	color:#808080;padding-left:100;	}
	</style>
</head>
<body>
<p>Contacts read from	XML	file : <%= contacts.length %>.<br>
	Time taken : <%=	(t2	- t1) %>	ms.</p>

<%
	int	n =	contacts.length;
	for(int	i=0; i <	n; i++)	{
%>
<p class="contacts">
	( <%= i+1 %> ) :<br>
	Name : <%= contacts[i].getName()	%>.<br>
	Email :	<%= contacts[i].getEmail() %>.<br>
	Phone No : <%= contacts[i].getPhone() %>.<br>
	State :	<%= contacts[i].getState() %>.<br>
	Country	: <%= contacts[i].getCountry() %>.<br>
	Address	:<%=	contacts[i].getAddress() %>
</p>
<%
	}
%>

</body>
</html>

Previous ( 2 Gone )( 1 Remaining ) Next

Comments/Questions ( Threads: 4, Comments: 5 )
    Contains 1 or more replies by the Author of this Article.
    Contains 1 or more replies by Faisal Khan.

  1. EJB Transactions.....
  2. There is a problem while compiling
  3. Reading XML Files with Enterprise JavaBeans from Xindice with XPath
  4. problems... ( 1 Reply )

Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.