简单的EJB访问,Hello为例,服务器端为EJB3.0,客户端分se,servlet,jsp三种访问,服务器为Glassfish4
服务器端代码为:
接口:
package com.chx.Hello;
import javax.ejb.Remote;
@Remote
public interface Hello {
public String sayHello(String name,int i );
}
对应的类为:
package com.chx.Hello;
import javax.ejb.Stateful;
@Stateful
public class HelloBean implements Hello {
@Override
public String sayHello(String name, int i) {
// TODO Auto-generated method stub
System.out.println("the test is"+i);
return name+"say : hello !";
}
}
客户端为se时:需要把glassfish所需的对应jar包引入工程中,可直接导入modules文件下的所有jar包
package com.chx.client;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import com.chx.Hello.Hello;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
InitialContext ctx = new InitialContext();
Hello hello = (Hello) ctx.lookup("com.chx.Hello.Hello");
System.out.println(hello.sayHello("cx", 1));
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
客户端为servlet时:接口需要导入,但是bean文件不要导入
package com.chx.client;
import java.io.IOException;
import java.io.PrintWriter;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.chx.Hello.Hello;
public class Client extends HttpServlet {
private static final long serialVersionUID = -8752044383967257668L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
// out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
// out.println("<HTML>");
// out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
// out.println(" <BODY>");
try{
InitialContext ctx = new InitialContext();
for(int i=1;i<=9;i++){
Hello hello = (Hello) ctx.lookup("com.chx.Hello.Hello");
out.print(hello.sayHello("曹海西 cao ", i));
out.println("<br/>");
}
}catch(Exception e){
e.printStackTrace();
}
// out.print(" This is ");
// out.print(this.getClass());
// out.println(", using the GET method");
// out.println(" </BODY>");
// out.println("</HTML>");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
客户端为jsp时:即把se客户端中main函数中的方法引入<body>即可,它是上下文查找。
<%@page import="com.chx.Hello.Hello"%>
<%@page import="javax.naming.InitialContext"%>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
<%
try{
InitialContext ctx = new InitialContext();
for(int i=1;i<=9;i++){
Hello hello = (Hello) ctx.lookup("com.chx.Hello.Hello");
%>
<br/>
<%
out.print(hello.sayHello("cao", i));}
}catch(Exception e){
e.printStackTrace();
}
%>
</body>
</html>