这是我尝试过的示例代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Sample1</title>
</head>
<body>
<form action="ActionServlet" method="post">
<h1>This is inside sample1</h1>
<input type="submit">
</form>
</body>
</html>
发表于 2012 年 11 月 19 日星期一 10:58:11 PM 私信 引用 这是我尝试的示例代码
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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>Sample1</title>
</head>
<body>
<form action="ActionServlet" method="post">
<h1>This is inside sample1</h1>
<input type="submit">
</form>
</body>
</html>
单击提交时,控件导航到名为 ActionServlet 的 servlet
public class ActionServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public ActionServlet() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
System.out.println("This is inside doPost method for action servlet");
System.out.println("Before calling EJB for action servlet");
try{
Context ic = new InitialContext();
//first instance creation
StateFulServiceHome home=(StateFulServiceHome)ic.lookup("Stateful1234");
StateFulService sample=(StateFulService)home.create();
sample.setName("Hello1223");
sample.getName();
//second instance creation
Context ic1 = new InitialContext();
StateFulServiceHome home1=(StateFulServiceHome)ic1.lookup("Stateful1234");
StateFulService sample1=(StateFulService)home1.create();
sample1.getName();
}catch (Exception e) {
e.printStackTrace();
}
System.out.println("After calling EJB in action servlet1");
request.getRequestDispatcher("Sample2.jsp").forward(request,response);
}
}
ActionServlet 在名为 stateful session bean 的 bean 上调用业务方法
public class StateFulBean implements SessionBean{
String name;
public void setName(String name)throws RemoteException{
this.name =name;
}
private SessionContext context;
public void ejbActivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
System.out.println("*********This is inside ejbActivate method***********");
}
public void ejbPassivate() throws EJBException, RemoteException {
// TODO Auto-generated method stub
System.out.println("*********This is inside ejbpassivate method***********");
}
public void ejbRemove() throws EJBException, RemoteException {
// TODO Auto-generated method stub
System.out.println("*********This is inside Ejb remove method***********");
}
public void ejbCreate() throws EJBException,RemoteException{
System.out.println("*********This is inside create method***********");
}
public void setSessionContext(SessionContext ctx) throws EJBException,
RemoteException {
// TODO Auto-generated method stub
context=ctx;
System.out.println("*********This is for set session context***********");
}
public void getName()throws RemoteException{
System.out.println("***********This is for start of getting business method*****");
System.out.println("The name obtained is"+this.name);
System.out.println("***********This is for end of getting business method*****");
}
}
public interface StateFulService extends EJBObject {
public void getName()throws RemoteException;
public void setName(String name)throws RemoteException;
}
public interface StateFulServiceHome extends EJBHome {
public StateFulService create() throws RemoteException,CreateException;
}
最后是我的 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>
<enterprise-beans>
<session>
<ejb-name>Stateful1234</ejb-name>
<home>StateFulServiceHome</home>
<remote>StateFulService</remote>
<ejb-class>StateFulBean</ejb-class>
<session-type>Stateful</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
</ejb-jar>
对于有状态和无状态会话 bean,我得到了相同的输出,我的假设是当我的 bean 被定义为有状态时,我应该在第二次查找 bean 时获得名称。
如果我在某个地方错了,请告诉我。
-Shyam