Ejb Programe Run On Jboss

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

Starting with JBOSS

Set the JAR File in classpath for EJBs

D:\jboss-4.2.0.GA\server\default\lib\jboss-j2ee.jar
For Servlets
D:\jboss-4.2.0.GA\client\servlet-api.jar

Set the environmental variables

JAVA_HOME
JBOSS_HOME

Set the classpath by adding BIN folder of JBOSS

D:\jboss-4.2.0.GA\bin

J2EE Application
Web Client  JSP OR Servlet ->Java Server (Tomcat)  mapping (web.xml)
 class file  server container  EJB  Database

Create the EJB Appilcation and covert into JAR


- Place the package with Meta-INF in one folder (e.g. first)
- Go inside the folder from DOS prompt and issue
o Jar cvf wizard.jar .

Create the Web Application (.war)

- Create your JSP and Servlet


- Create on one folder (e.g. second)
o Create WEB-INF with web.xml
- Create the .war file including the WEB-INF
o JAR cvf wizard.war .
Create the Application (.ear)
- Create the application.xml under META-INF and place both .jar and .war
inside a folder

JAR cvf wizard.ear .

Deployment inside the JBOSS


- Copy file to D:\jboss-4.2.0.GA\server\default\deploy
- Run the jboss from run.bat from bin folder

Run the application


http://localhost:8080/wizard/call

Ejb programe run on jboss


First we will make four java file
Three files will be--------- ejb files

Ejb files--
1. wizardSession.java---------Remote interface

2. wizardSessionHome.java-------Home Interface

3. wizardSessionBean.java-------Bean File

One File will be -----Servlet file

4. wizardServlet.java-----------UI

All file compile

wizardSession.java--------------------after compile----------------- wizardSession.class


wizardSessionHome.java------------after compile----------------- wizardSessionHome.class

wizardSessionBean.java-------------after compile----------------- wizardSessionBean.class

wizardServlet.java-------------------after compile----------------- wizardServlet.class

wizardServlet.java

package com.wizard.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wizard.WizardSession;
import com.wizard.WizardSessionHome;

/**
* author mohd zaid khan
* @Email zaid.khan@wizardinfosolutions.co.in
*/

public class WizardServlet extends HttpServlet {

WizardSessionHome wizardSessionHome;

public void init(ServletConfig config) throws ServletException{


// Look up home interface
try {
InitialContext ctx = new InitialContext();
Object objref = ctx.lookup("ejb/test/WizardSessionBean");
wizardSessionHome =
(WizardSessionHome)PortableRemoteObject.narrow(objref,WizardSessionHome.class);
} catch (Exception NamingException) {
NamingException.printStackTrace();
}

public void doGet (HttpServletRequest request,


HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
response.setContentType("text/html");
String title = "EJB Example";
out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title>Hello World Servlet!</title>");
out.println("</head>");
out.println("<body>");
out.println("<p align=\"center\"><font size=\"4\" color=\"#000080\">Servlet
Calling Session Bean</font></p>");

try{

WizardSession wizardSession;
wizardSession = wizardSessionHome.create();
out.println("<p align=\"center\"> Result is: <b>" +
wizardSession.addNumber(20,400) + "</b></p>");

wizardSession.remove();
}catch(Exception CreateException){
CreateException.printStackTrace();
}
out.println("<p align=\"center\"><a href=\"javascript:history.back()\">Go to
Home</a></p>");
out.println("</body>");
out.println("</html>");
out.close();
}

public void destroy() {


System.out.println("Destroy");
}
}

wizardSession.java
package com.wizard;

/**
* author mohd zaid khan
* @Email zaid.khan@wizardinfosolutions.co.in
*/

public interface WizardSession extends javax.ejb.EJBObject{


public int addNumber(int x,int y) throws java.rmi.RemoteException;
}

WizardSessionHome.java

package com.wizard;

/**
* author mohd zaid khan
* @Email zaid.khan@wizardinfosolutions.co.in
*/

public interface WizardSessionHome extends javax.ejb.EJBHome{

public static final String COMP_NAME="java:comp/env/ejb/test/MyTestSession";

public static final String JNDI_NAME="ejb/test/MyTestSessionBean";

public WizardSession create() throws javax.ejb.CreateException, java.rmi.RemoteException;

WizardSessionBean.java
package com.wizard;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

/**
* author mohd zaid khan
* @Email zaid.khan@wizardinfosolutions.co.in
*/

public class WizardSessionBean implements SessionBean{

private int i=1;

public void ejbCreate() throws CreateException {


}

public int addNumber(int x,int y)


{
return x+y;
}

public void setSessionContext( SessionContext aContext ) throws EJBException {


}

public void ejbActivate() throws EJBException {


}

public void ejbPassivate() throws EJBException {


}

public void ejbRemove() throws EJBException {

1---

First Step- we will make a folder web-inf

Two file and one folder will be in web-inf folder

WEB-INF----
1. jboss-web.xml
2. web.xml
3. classescomwizardservletWizardServlet.class

i. jboss-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.2//EN"
"http://www.jboss.org/j2ee/dtd/jboss-web.dtd">

<jboss-web>

</jboss-web>

ii. web.xml(servlet configuration)

<?xml version="1.0" encoding="ISO-8859-1"?>


<!--
Copyright 2004 The Apache Software Foundation

Licensed under the Apache License, Version 2.0 (the "License");


you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software


distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

<!-- JSPC servlet mappings start -->

<servlet>
<servlet-name>zaid</servlet-name>
<servlet-class>com.wizard.servlet.WizardServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>zaid</servlet-name>
<url-pattern>/call</url-pattern>
</servlet-mapping>

<!-- JSPC servlet mappings end -->

</web-app>

iii. classescomwizardservletWizardServlet.class

After this
We will create a .war file of WEB-INF folder

Wizard.jar---- WEB-INF--
1. jboss-web.xml
2. web.xml
3. classescomwizardservletWizardServlet.class

2--
Second Step-
we will make Two folder  Ist Meta-inf and com folder
META-INF folder hv three file
And com hv one folder wizard hv three .class file
1. META-INF--
i. ebj-jar.xml
ii. jboss.xml
iii. MANIFEST.MF
2. com-wizard--
i. WizardSession.class
ii. WizardSessionHome.class
iii. wizardSessionBean.class

1. META-INF

i. ebj-jar.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"
"http://java.sun.com/dtd/ejb-jar_2_0.dtd">

<ejb-jar>

<description>Example 3</description>
<display-name>Example 3</display-name>

<enterprise-beans>

<!-- Session Beans -->


<session>
<display-name>Wizard Session Bean</display-name>
<ejb-name>wizardSessionBean</ejb-name>
<home>com.wizard.WizardSessionHome</home>
<remote>com.wizard.WizardSession</remote>
<ejb-class>com.wizard.WizardSessionBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
</assembly-descriptor>
</ejb-jar>

ii. jboss.xml

<?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>wizardSessionBean</ejb-name>
<jndi-name>ejb/test/WizardSessionBean</jndi-name>
</session>
</enterprise-beans>
<resource-managers>
</resource-managers>
</jboss>

iii. MANIFEST

Manifest-Version: 1.0
Created-By: 1.5.0_06 (Sun Microsystems Inc.)

2.. com-wizard--

i. WizardSession.class
ii. WizardSessionHome.class
iii. wizardSessionBean.class

After this
We will create a .jar file of WEB-INF folder

zaid.jar-------------- 1. META-INF--
ii. ebj-jar.xml
iii. jboss.xml
iv. MANIFEST.MF
2. com-wizard--
iv. WizardSession.class
v. WizardSessionHome.class
vi. wizardSessionBean.class

zaid.jar--------------- META-INF+com

3--
third Step-
we will make one folder  Meta-inf

1.META-INF-- application.xml
application.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<application>
<display-name>Example 3 </display-name>
<module>
<web>
<web-uri>wizard.war</web-uri>
<context-root>/wizard</context-root>
</web>
</module>

<module>
<ejb>zaid.jar</ejb>
</module>

</application>

After this

We have three file 1. wizard.war ( first step)


2. zaid.jar (second step)
3. META-INF(folder) (third step)

4. Final-------- sir.ear------- wizard.war+ zaid.jar+ META-INF


I have final file name-----------------------sir.ear

Right click my computer--click advance


Click envirment variables-
Click new –and config the jboss

Setting the classpath-------- C:\jboss-4.2.0.GA\bin

Jboss configuration

sir.ear file will be deploy in this folder

C:\jboss-4.2.0.GA\server\default\deploy/sir.ear

Run file
======
Open the brower http://localhost:8080/wizard/call

output
close

You might also like