<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Java Interview Questions &#187; Example of stateful session bean</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/ejb-interview-questions/example-of-stateful-session-bean/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Creating a Stateful session Bean</title>
		<link>http://www.bestjavainterviewquestions.com/creating-a-stateful-session-bean/</link>
		<comments>http://www.bestjavainterviewquestions.com/creating-a-stateful-session-bean/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 15:00:30 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Example of stateful session bean]]></category>
		<category><![CDATA[ejb]]></category>
		<category><![CDATA[ejb sessions example]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[rmi]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[session bean example]]></category>
		<category><![CDATA[stataeful]]></category>
		<category><![CDATA[statefulsessionbean]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=564</guid>
		<description><![CDATA[To send two values to a stateful session bean and getting their sum and difference. The program makes two conversations(calls) with the session bean. In one call it gets the sum and in the second call it gets the difference. Between the calls state is to be maintained. 1 Step) Remote interface : import javax.ejb.*; [...]]]></description>
			<content:encoded><![CDATA[<p>To send two values to a stateful session bean and getting their sum and difference.</p>
<p>The program makes two conversations(calls) with the session bean. In one call it gets the sum and in the second call it gets the difference. Between the calls state is to be maintained.</p>
<p><em><strong>1 Step) Remote interface :</strong><br />
</em></p>
<p><em>import javax.ejb.*;<br />
import java.rmi.*;</em></p>
<p>//No change from Stateless</p>
<p>public interface Calc extends EJBObject<br />
{<br />
public int add(int a,int b) throws RemoteException;<br />
public int sub(int a, int b) throws RemoteException;<br />
}</p>
<p><strong>2 Step: Home interface;</strong></p>
<p>import javax.ejb.*;<br />
import javax.rmi.*;</p>
<p>public interface CalcHome extends EJBHome<br />
{<br />
public Calc create() throws CreateException, RemoteException;</p>
<p>//Observe the change in the implementation of business methods<br />
}</p>
<p><strong>3 Step: Bean class file;</strong></p>
<p>import javax.ejb.*;<br />
import javax.rmi.*;</p>
<p>public class CalcBean implements SessionBean<br />
{</p>
<p>//change here, creating some instance variables to be used in between method calls<br />
SessionContext ctx;<br />
int a,b;</p>
<p>public int add(int a ,int b)<br />
{</p>
<p>//initialize local ot instance variables</p>
<p>this.a=a;<br />
this.b=b;</p>
<p>return a+b;<br />
}</p>
<p>//for subtraction, instance variables assigned in the previous call are used<br />
public int sub()<br />
{<br />
return a-b;<br />
}</p>
<p>//override all the 4 SessionBean methods</p>
<p>public void ejbCreate(){  }<br />
public void ejbActivate(){  }<br />
public void ejbPassivate(){  }<br />
public void setSessionContext(SessionContext ctx)<br />
{<br />
this.ctx = ctx;<br />
}<br />
}</p>
<p><strong>4 Step: Write XML files needed to create a manifest file</strong></p>
<p>The template of XML files with default setting are available in :<br />
C:\ weblogic\examples\ejb\basic\statefulsession\build\meta-Inf<br />
Copy the Meta-Inf directory to your working directory. That is this Meta-Inf directory becomes subdirectory to your working directory.</p>
<p><strong>1. ejb-jar.xml:</strong></p>
<p>//on this name bean will be deployed<br />
&lt;ejb-name&gt; Calculation &lt;/ejb-name&gt;</p>
<p>//write your home interface name<br />
&lt;home&gt; CalcHome &lt;/home&gt;</p>
<p>//write your remote interface name<br />
&lt;remote&gt; Calc &lt;/remote&gt;</p>
<p>//write your bean class name<br />
//earlier it is Stateless<br />
&lt;session -type&gt; Stateful &lt;/session-type&gt;</p>
<p>comedown and write again<br />
&lt;ejb-name&gt; Calculation &lt;/ejb-name&gt;</p>
<p><strong>2. weblogic-ejb-jar.xml :</strong></p>
<p>&lt;ejb-name&gt; Calculation &lt;/ejb-name&gt;<br />
comedown and write again</p>
<p>//this is reference name to be mentioned in client<br />
//program and with this reference name bean and client will communicate.</p>
<p>&lt;jndi-name&gt; XXX &lt;/jndi-name&gt;</p>
<p><strong>5  Step : Write JAR file :</strong></p>
<p>jar cvf CalcJar.jar *.class META-INF\.xml<br />
<strong><br />
6  Step: Compile JAR file and obtain compiled JAR file :</strong></p>
<p>ejbc CalcJar.jar CalcJar1.jar</p>
<p><strong>7  Step: Copy the compiled JAR file to myserver directory:</strong></p>
<p><em>copy CalcJar1.jar c:\weblogic \myserver</em></p>
<p><strong>8  Step: Deploy the bean: (run from start menu)</strong></p>
<p>start a weblogic 5.1.0 a Utilities a Edit Server Properties<br />
Use Search menu option to locate Weblogic EJB Demo Properties</p>
<p><strong>Write the following statement :</strong></p>
<p>c:\weblogic \myserver \CalcJar1.jar \<br />
<strong><br />
9  Step Run weblogic server (run from start menu )</strong></p>
<p>start weblogic 5.1.0 a weblogic Server</p>
<p>Observer the server displays: 1 deployed. 0 failed to deploy</p>
<p>What we have done so far is, we have created the bean and deployed in weblogic server. Now<br />
we can call and use it.</p>
<p><strong>10 Step: to write the client program, compile and run it to call the bean:</strong></p>
<p>//Observe the change in the implementation of business methods</p>
<p>import javax.ejb.*;<br />
import javax.rmi.*;<br />
import javax.naming.*;<br />
import java.util.*;<br />
class CalcClient<br />
{<br />
public static void main(String args[] ) throws Exception<br />
{<br />
Properties p=new Properties();<br />
p.put(Context.INITIAL_CONTEXT_FACTORY, &#8221; weblogic.jndi.T#InitialContextFactory&#8221;);<br />
p.put(Context.PROVIDER_RUL, &#8220;t3://local:7001&#8243;);<br />
//InitialContext() method throws javax.naming.NamingException<br />
Context ctx = new InitialContext(p);<br />
CalcHome chome = (calcHome) ctx.lookup(&#8220;SNRao&#8221;);<br />
//create() method throws RemoteException and javax.ejb.CreateException<br />
Calc rref = chome.create();</p>
<p>//observe change here<br />
int sum = rref.add(20,5);<br />
int diff = rref.sub();</p>
<p>System.out.println(&#8220;sum :&#8221;+sum);<br />
System.out.println(&#8220;Diff : &#8220;+diff);<br />
}<br />
}</p>
<h2  class="related_post_title">Related Post</h2><ul class="related_post"><li>February 15, 2009 -- <a href="http://www.bestjavainterviewquestions.com/providing-current-date-and-time-using-stateless-session-bean/" title="Providing current date and time using Stateless session bean">Providing current date and time using Stateless session bean</a> (0)</li><li>June 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/19/" title="Java Basic Questions">Java Basic Questions</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/core-java-interview-questions/" title="Core Java Interview Questions">Core Java Interview Questions</a> (1)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/basic-java-interview-question/" title="Basic Java Interview Question">Basic Java Interview Question</a> (1)</li><li>March 20, 2009 -- <a href="http://www.bestjavainterviewquestions.com/life-cycle-of-stateless-session-bean/" title="Life Cycle of stateless session bean">Life Cycle of stateless session bean</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/creating-a-stateful-session-bean/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

