Author: Han QW, reproduced, please indicate the source if inappropriate, please point out
Install JSDK first, then install JBoss.
Install JSDK, you must get a JDK that corresponds to the user's operating system.
My installation file directory is
Windows2000: d: /s1studio_jdk/j2sdk1.4.1
Linux: /root/s1studio_jdk/j2sdk1.4.1
In order to use EJB, you need a J2EE-1.3.jar or J2EE-1.2.jar,
If Sun One Studio or J2EE (www.sun.com) is installed.
Place this file on the ClassPath path.
Or use jboss-j2ee.jar, after installing JBoss, you can find in $ JBOSS / CLIENT.
It is recommended to install Sun One Studio, compile the Java source program with Sun One Studio,
Do not set ClassPath, save a lot of processes.
Install JBoss:
Unlock the JBoss's compressed package, put it on either directory,
My installation file directory is
/DOSE/Jboss-3.0.4_tomcat-4.1.12 (redhat8.0)
E: /JBOSS-3.0.4_Tomcat-4.1.12 (Windows2000)
Windows2000, Linux share the same set of JBoss.
Configure jboss:
Start JBoss needs to perform a script file:
Linux: run.sh
Windows corresponds: run.bat
(1)
Insert a line in jboss / bin / run.bat (for windows)
SET JAVA_HOME = D: /s1studio_jdk/j2sdk1.4.1
Insert a line in jboss / bin / run.sh (for linux)
Java_home = "/ root / s1studio_jdk / j2sdk1.4.1"
or
(2) Set the system environment variable java_home, point to JDK
Run jboss, run.sh or run.bat
When you see the information that starts JBOSS, the description is started.
Server simple test:
JBoss default web port is 8080, we can open a browser input address
Http: // localhost: 8080 / jmx-console
When you see the JBoss information in the browser, the installation configuration JBoss is successful.
Establish the following directory and files (accounting case).
First.ear
|
| ----- Meta-inf (Application.xml)
|
| ----- first.jar
| | ----- Meta-INF (EJB-JAR.XML, JBOSS.XML)
| `----- Dev
| | ----- First (FirstSession.Java, FirstsessionHome.java, FirstsessionBean.java)
| | ----- delegate (NewdeLegate.java)
| `----- Dao (MySQLDAO.JAVA)
|
`----- first.war (index.jsp)
|
`----- Web-INF (JBoss-Web.xml, Web.xml)
| ----- Classes
`----- lib
/ *
**
** mysqldao.java
**
* /
package Dev.Dao; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; import javax.naming.InitialContext; import javax.sql.DataSource; public class MysqlDao {public Connection getConnection () throws Exception {InitialContext ctx = new InitialContext (); DataSource ds = (DataSource) ctx.lookup ( "java: / PostgresDS"); Connection conn = null; Statement stmt = null; try {conn = ds .getConnection ();} catch (SQLException sqlEx) {System.out.println ( "Error connect to pool.");} return conn;} public String getName (String id) throws Exception {Connection conn = null; Statement stmt = NULL; ResultSet RS = NULL; String Name = ""; try {conn = getConnection (); if (conn! = null) System.out.println ("Get CONECTTION." Conn.toString ()); STMT = conn .CREATESTATEMENT (); IF (stmt! = null) System.out.Println ("get stating." stmt.toString ()); string sql = "select * from users where id = '" ID ""; system.out.println ("SQL from getId ():" SQL); rs = stmt.executeQuery (SQL); if (RS! = Null) System.out.println ("Get Result)); if (rs.next ()) {Name = rs.getstring ("name");}}}} catch ("Error from getname ()."); System.out.Println ("Error from Dao.getname () "
SQLEX.GetMessage ());} finally {if (conn! = Null) {Try {conn.close ();} catCH (Exception SQLEX) {}}}}}} Return Name;} PUBLIC STRING GETCOUNTRY (STRING ID) THROWS Exception {Connection conn = null; STATEMT = NULL; STRING NAME = ""; try {conn = getConnection (); stmt = conn.createstatement (); string sql = "select * from users where id = '" id "' "; System.out.println (" SQL from getCountry (): " SQL); java.sql.resultset = stmt.executeQuery (SQL); if (rs.next ()) {name = rs.getstring (" ");}}} Catch (SQLEXCEPTION SQLEX) {system.out.println (" Error from getCountry (). ");} Finally {if (conn! = Null) {TRY {Conn.close ();} catch EXCEPTION SQLEX) {}}} return name; } / *
**
** NewdeLegate.java
**
* /
package Dev.Delegate; import java.lang *;. import Dev.First *;. public class NewDelegate {Dev.First.FirstSession bean = null; public NewDelegate () {try {javax.naming.InitialContext ctx = new javax.naming .InitialContext (); Object objref = ctx.lookup ( "ejb / FirstSession"); Dev.First.FirstSessionHome testBean = (Dev.First.FirstSessionHome) javax.rmi.PortableRemoteObject.narrow (objref, Dev.First.FirstSessionHome.class ); Bean = testbean.create (); system.out.println ("from jsp");} catch (exception namingexception) {namingexception.printStackTrace ();}} public string welcome () {string msg = ""; try {msg = bean.Welcome ();} catch (Exception NamingException) {NamingException.printStackTrace ();} return msg;} public String getName (String id) {String name = ""; try {name = bean.getName (id } tch (Exception NamingException) {NamingException.printStackTrace ();} return name;} public String getCountry (String id) {String country = ""; try {country = bean.getCountry (id);} catch (Exception NamingException) {NamingException .printstacktrace ();} return country;}} / *
**
** FirstSession.java
**
* /
package Dev.First; import java.lang *;. import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public interface FirstSession extends javax.ejb.EJBObject {public String Welcome () throws java.rmi.RemoteException; public String getName (String id) throws java.rmi.RemoteException; public String getCountry (String id) throws java.rmi.RemoteException;} / *
**
** FirstSessionHome.java
**
* /
package Dev.First; import java.lang *;. import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public interface FirstSessionHome Extends javax.ejb.ejbhome {public firstsession create () throws javax.ejb.createException, java.rmi.RemoteException;}
/ *
**
** FirstSessionBean.java
**
* /
package Dev.First; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.EJBException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class FirstSessionBean implements SessionBean {public void ejbCreate ( ) throws CreateException {} public String Welcome () {String msg = "Hello This My Session Bean From Jboss!."; System.out.println (msg); return msg;} public String getName (String id) {String name = ""; System.out.println ("from bean before getname:" Name); try {dev.dao.mysqldao Dao = new dev.dao.mysqldao (); name = DAO.GETNAME (ID); system.out .println ("from bean after getname:" Name);} catch (exception e) {system.out.println (E.GetMessage ());} return name;} public string getCountry (String id) {string country = ""; Try {dev.dao.mysqldao DAO = new dev.dao.mysqldao (); country = DAO.GETCOUNTRY (ID);} catCH (Exception E) {} Return C ountry;} public void setSessionContext (SessionContext aContext) throws EJBException {} public void ejbActivate () throws EJBException {} public void ejbPassivate () throws EJBException {} public void ejbRemove () throws EJBException {}} / * Do not put the following Lines INTO INDEX.JSP
**
** index.jsp
**
* / Don't put the Above lines INTO INDEX.JSP
<% @ page language = "java"%> <% string msg = ""; string msg1 = "; dev.delegate.newdelegate nn = new dev.dlegate.newdeLegate (); if (Request.GetParameter) )! = Null && request.getParameter ("ID")! = "" &&! Request.getParameter ("ID"). Equals (")) {string id = request.getParameter (" id "); string name = ""; Dev.dao.mysqldao Dao = new dev.dao.mysqldao (); name = nn.getname (ID); // access database through session bean // name = DAO.GETNAME (ID); // access data Directly if (name! = null&&! name.equals (")) {msg1 =" Welcome " Name "! You are from " DAO.GETCOUNTRY (ID) ";} else {msg1 = " Please check your ID.: " ID;}} msg = nn.welcome ();%>