My first EJB example

来源:互联网 发布:浅析网络舆论监督 编辑:程序博客网 时间:2024/06/03 12:27

1. Download JBoss (I used 1.4.2.2) and configure the environment variable in the computer

2.

(Note: Please tick the ejb-jar.xml generator checkbox):

 

 

 


Right click ejbModule and create the session bean:

 

 

Define HelloWorldRemote interface as below:
@Remote
public interface HelloWorldRemote {
  
 public int ejbCalculate(int input);
}

Implement it in HelloWorld:
@Override
 public int ejbCalculate(int input) {
  return 2 + input;
 }

 

3. Export EJB jar
:

 

 

Deploy the EJB jar 
Copy the jar into server/default/deploy folder,Start JBOSS,When you see below, it means the deployment is successful。

5. Create client web application to to invoke the EJB

Create a new Dynamic Web Applilcation, and create a test.jsp under WebContent

test.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="com.zou.*, javax.naming.*, java.util.Properties" %>     
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<hr>
 <center>Hello World</center>
<hr>
<%
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");


try {
InitialContext ctx = new InitialContext(props);
  HelloWorldRemote testbean = (HelloWorldRemote)ctx.lookup("HelloWorld/remote");
  int first = testbean.ejbCalculate(3);
  int second = testbean.ejbCalculate(3);
  StringBuffer sb = new StringBuffer();
  sb.append("Value:").append(first).append("/").append(second);
out.println(sb.toString());
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




%>


</body>
</html>

, Then export this project as war, and deploy it in the JBoss, then test it.(For this webapp, it should have same session bean structure like EJB prject, copy them into this project, same package name etc.)_

原创粉丝点击