Manually deploying EJBS to experience the EJB development process personally

Source: Internet
Author: User
JBoss 4.0 automatically integrates Tomcat 5.0, so it's not just a professional EJB container, but also a professional Jsp/servlet container and Web server.

TOMCAT 5.0 is integrated in the following directory of JBoss:

Jboss_home\server\default\deploy\jbossweb-tomcat50.sar

It provides a server.xml and Web.xml file that allows you to make some basic settings for Tomcat. But JBoss's designers recommend that users not even touch the directory, let alone modify and add any files, because Tomcat is so tightly connected that all setup work can be done in JBoss's own configuration file. But I still found a place worth modifying: Change the port number in the Server.xml, for example, from the default 8080 port to 4000 port, because I found that my "Baidu down Bar" software often takes up 8080 ports, and I already have a Tomcat 5.0.

Type this:

http://localhost:4000/

Access to JBoss's Welcome page is available

This article demonstrates how a JSP page invokes an EJB component in JBoss 4.0.

General structure:

Java application/
|__EJB components/(Haiejb.jar)
| |__meta-inf/
| | | |__ejb-jar.xml
| | | |__jboss.xml
| |__ejbs/
| |__haihome.class
| |__haiclient.class
| |__haibean.class
|__web application/(Haiejb.war)
| |__haiejb.jsp
| |__web-inf/
| |__web.xml
| |__jboss-web.xml
|__meta-inf/
|__application.xml

   I. Compiling java files as EJB class files

Java File compilation:

[Assuming that the javax.ejb.* package is included in the classpath of the system environment variable, the package can be found in the following places:

Jboss_home\server\default\lib\jboss-j2ee.jar

Jboss_home\client\jboss-j2ee.jar]


[Java source file directory]>:javac-classpath%classpath%-d [Output directory: EJB component Directory] *.java

Haihome.java:
Package EJBS;

Import java.io.Serializable;
Import java.rmi.*;
Import javax.ejb.*;

Public interface Haihome extends EJBHome {
Haiclient Create () throws RemoteException, createexception;
}


Haiclient.java:

Package EJBS;

Import javax.ejb.*;
Import java.rmi.RemoteException;
Public interface Haiclient extends Ejbobject {
Public String Sayhai () throws remoteexception;
}
Haibean.java:
Package EJBS;

Import javax.ejb.*;
Import javax.naming.*;

public class Haibean implements Sessionbean {

Public String Sayhai () {
Return "Hai, EJB technology!";
}
 
public void Ejbcreate () throws Ejbexception {}
public void Ejbremove () throws Ejbexception {}
public void Ejbpassivate () {}
public void Ejbactivate () {}
public void Setsessioncontext (Sessioncontext sc) {}
}


   Second, create EJB components:

Haiejb.jar: (EJB component)

Packaging commands: [EJB component directory]>:jar CVF Haiejb.jar meta-inf/ejbs/


|__meta-inf/

| |__ejb-jar.xml

| |__jboss.xml

|__ejbs/

|__haihome.class

|__haiclient.class

|__haibean.class

Ejb-jar.xml:

! DOCTYPE Ejb-jar Public '-//sun Microsystems, Inc.//dtd Enterprise, JavaBeans 2.0//en ' http://java.sun.com/dtd/ejb-jar_2 _0.dtd ' >

Hai EJB instance.
Hai EJB


HAIEJB
EJBs. Haihome
EJBs. Haiclient
EJBs. Haibean
Stateless
Bean



Jboss.xml:




HAIEJB
HAIEJB



   third, create Web applications

Haiejb.war: (Web application)

Packaging commands: [WEB application directory]>:jar CVF haiejb.war haiejb.jsp web-inf/

|__haiejb.jsp

|__web-inf/

|__web.xml

|__jboss-web.xml

HAIEJB.JSP:

<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%>
<%@ page import= "Ejbs.*,javax.ejb.*,javax.naming.*,javax.rmi.portableremoteobject,java.rmi.remoteexception"%>


<% String message = "nothing!";
try {
InitialContext IC = new InitialContext ();
Object ObjRef = Ic.lookup ("HAIEJB");
Haihome home = (haihome) portableremoteobject.narrow (OBJREF,EJBS. Haihome.class);
Haiclient hairemote = Home.create ();
message = Hairemote.sayhai ();
catch (RemoteException re) {
Re.printstacktrace ();
catch (createexception CE) {
Ce.printstacktrace ();
catch (Namingexception ne) {
Ne.printstacktrace ();
}
%>
<%=message%>


Xml:

! DOCTYPE Web-app Public '-//sun Microsystems, INC.//DTD Web Application 2.3//en ' ' Http://java.sun.com/dtd/web-app_2_3.dtd '


HAIEJB
Session
EJBs. Haihome
EJBs. Haiclient


Jboss-web.xml:



HAIEJB
HAIEJB


   Four, create the Java EE application

Haiejb.ear: (Java EE application)

Copy the Haiejb.jar and Haiejb.war packages you created above to the Java EE Application Home directory you created, create a new Meta-inf directory and make application.xml files inside:

Packaging commands: [Java application directory]>:jar CVF haiejb.ear haiejb.jar Haiejb.war meta-inf/

|__haiejb.jar

|__haiejb.war

|__meta-inf/

|__application.xml

Application.xml:


HAIEJB Java Application


Haiejb.war
/HAIEJB



Haiejb.jar


   v. Deployment of Java application:

Copy the Haiejb.ear to the jboss_home\server\default\deploy\.

Start JBoss 4.0, notice if there are any exceptions in the Command Line window, and check the log file if there are any exceptions:

Jboss_home\server\default\log\server.log


Find out where to analyze the problem, and then correct it until no exceptions appear

Finally, in the browser address bar, type:

http://localhost:8080/haiejb/haiejb.jsp

Results:

Hai, EJB technology!

Good luck!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.