Voici l'exemple de code i essayéObtenir la même sortie pour Stateless et haricot stateful session
<%@ 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>
affichés les lundis, 19 Novembre, 2012 22:58:11 message privé Citation Voici l'exemple de code que j'ai essayé
<%@ 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>
en cliquant sur soumettre, le contrôle accède à 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 appelle la méthode d'affaires sur un haricot appelé bean session stateful
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;
}
et enfin mon 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>
Je reçois la même sortie pour bean session stateful et sans état, mon l'hypothèse est quand mon bean est défini comme stateful, je devrais obtenir le nom pendant la recherche de haricot pour la deuxième fois. Faites-moi savoir si je me trompe quelque part.
-Shyam