2012-11-22 129 views
0

這裏相同輸出的代碼示例我想獲取無狀態和有狀態會話bean

<%@ 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分十一秒悄悄話 報價 下面是示例代碼我試圖

<%@ 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> 

上點擊提交,控制導航到SERVLET命名的ActionServlet

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上的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

回答

0

我的假設是,當我的bean被定義爲有狀態,bean的查找第二次時我應該得到的名稱。

這不是有狀態的會話bean的工作方式。當您使用有狀態會話bean時,您可以將代理存儲到「某處」的bean,即當容器在某段時間內不調用它的方法時,該容器會將其鈍化。如果在bean銷燬之前隨時通過代理髮出後續請求,容器將「記住」客戶端(您的情況爲servlet)。換句話說,後續的調用會記住會話狀態,因此會記錄會話bean的狀態。您可以將狀態存儲在會話Bean中,即使是跨HTTP請求(因此,存儲對代理的引用的原因)。

請注意,這與查找很少有關。在您的代碼中:

//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(); 

您已經創建了會話bean的兩個實例,並且您有兩個代理。第二個bean顯然不會知道第一個存儲的狀態。

要考慮無狀態會話bean,那麼你需要記住,存儲對其代理的引用是有點沒有意義的,特別是在HttpSession對象中。這主要是因爲存儲在bean中的狀態只在方法調用期間有效。 EJB容器管理一個無狀態會話Bean池,每個調用可能涉及池中不同的未使用實例。

PS - 你有什麼理由說明你現在在學習EJB 2.x嗎?它不僅舊,在Java EE 6應用程序中不再需要它的一部分,而且在Java EE 7中正在修剪某些部分(實體bean)。