A simple example of calling EJB by JSP in JBoss 4.0

Source: Internet
Author: User
Tags jboss

JBoss 4.0 is automatically integrated with Tomcat 5.0, so it is not only 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, which can be used to perform some basic settings on Tomcat. However, the JBoss designer recommends that you do not even need to access this directory, let alone modify and add any files, because Tomcat and JBoss are so closely linked, so that all the settings can be completed in the JBoss configuration file. But I still found a point worth modifying: Modify the server. the port number in XML, for example, changed from the default port 8080 to port 4000, because I found that my "8080" software often occupies port 5.0, and I already have a tomcat.

 

Enter:

Http: // localhost: 4000/

Visit the welcome page of JBoss.

 

This article demonstrates how to call the EJB component in JBoss 4.0 on the JSP page.

 

Total structure:

 

J2EE application/
|__ EJB component/(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

 

1. compile Java files as EJB files

 

Java file Compilation:

[Assume that the classpath of the system environment variable contains the javax. EJB. * package. You can find this package in the following places:

Jboss_home/Server/default/lib/jboss-j2ee.jar

Jboss_home/client/jboss-j2ee.jar]

 

[Java source file directory]>: javac-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 ){}
}

 

 

Ii. Create an EJB component:

 

Haiejb. Jar :( EJB component)

Package command: [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:

<? 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> Hai EJB instance. </description>
<Display-Name> Hai EJB </display-Name>
<Enterprise-beans>
<Session>
<EJB-Name> haiejb </EJB-Name>
<Home> ejbs. haihome </Home>
<Remote> ejbs. haiclient </remote>
<EJB-class> ejbs. haibean </EJB-class>
<Session-type> stateless </session-type>
<Transaction-type> bean </transaction-type>
</Session>
</Enterprise-beans>
</EJB-jar>

 

JBoss. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<JBoss>
<Enterprise-beans>
<Session>
<EJB-Name> haiejb </EJB-Name>
<JNDI-Name> haiejb </JNDI-Name>
</Session>
</Enterprise-beans>
</JBoss>

 

 

3. Create a web application

 

Haiejb. War :( Web Application)

Package command: [web application catalog]>: 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" %>

<HTML>
<Body>
<%
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 ();
}
%>

<H1> <% = message %> </Body>
</Html>

 

Web. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype web-app public '-// Sun Microsystems, Inc. // DTD web application 2.3 // en' http: // java.sun.com/dtd/web-app_2_3.dtd'>

<Web-app>
<EJB-ref>
<EJB-ref-Name> haiejb </EJB-ref-Name>
<EJB-ref-type> session </EJB-ref-type>
<Home> ejbs. haihome </Home>
<Remote> ejbs. haiclient </remote>
</EJB-ref>
</Web-app>

 

Jboss-web.xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<JBoss-web>
<EJB-ref>
<EJB-ref-Name> haiejb </EJB-ref-Name>
<JNDI-Name> haiejb </JNDI-Name>
</EJB-ref>
</JBoss-web>

 

 

4. Create a J2EE Application

 

Haiejb. Ear: (J2EE Application)

Copy the haiejb. jar and haiejb. War packages created above to the J2EE application home directory you created, create a new META-INF directory and create the application. xml file in it:

Package command: [J2EE application directory]>: jar CVF haiejb. Ear haiejb. Jar haiejb. War META-INF/

|__ Haiejb. Jar

|__ Haiejb. War

|__ META-INF/

|__ Application. xml

 

Application. xml:

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

<Application>
<Display-Name> haiejb J2EE application </display-Name>
<Module>
<Web>
<Web-Uri> haiejb. War </Web-Uri>
<Context-root>/haiejb </context-root>
</Web>
</Module>
<Module>
<EJB> haiejb. Jar </EJB>
</Module>
</Application>

 

 

5. Deploy J2EE applications:

 

 

Copy haiejb. Ear

Jboss_home/Server/default/deploy/

Start JBoss 4.0. Check whether exceptions exist in the command line window. If any exceptions occur, check the log file:

Jboss_home/Server/default/log/server. Log

Locate the problem and correct it until no exception is displayed.

 

Finally, in the browser address bar, type:

Http: // localhost: 8080/haiejb. jsp

 

Result:

 

Hai, EJB technology!

Related Article

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.