Catalfano Anthony wrote:
>
> With A SFSB do you need to explicitly save the bean in the Http session
> between client interactions or is the container supposed to serialize it
> and store it automatically?
>
You need to save the Handle.
Here's a simple no-frills servlet and jsp that does the job...
========================================
Ian McCallion
Alexis Systems Limited
Romsey, UK
Tel: +44 1794 514883
Fax: +44 1794 501692
========================================
import javax.servlet.http.*;
public class SfsbWebClient extends HttpServlet
{
SfsbHome sh; // remember the Bean's Home
// Called when the web server initialise the Servlet
public void init() throws javax.servlet.ServletException
{
javax.naming.InitialContext ic;
try {
System.setProperty("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
System.setProperty("java.naming.provider.url",
"localhost");
System.setProperty("java.naming.factory.url.pkgs",
"org.jboss.naming;");
ic = new javax.naming.InitialContext();
sh = (SfsbHome)javax.rmi.PortableRemoteObject.narrow
(ic.lookup("sfsb"),SfsbHome.class);
}
catch (Exception e)
{
throw new javax.servlet.ServletException();
}
}
// called to return the response using a Java Server Page
protected void doAnswer (HttpServletRequest req,
HttpServletResponse rsp,
String message)
throws javax.servlet.ServletException, java.io.IOException{
req.setAttribute("message",message);
getServletConfig().getServletContext().
getRequestDispatcher("/index.jsp").forward(req, rsp);
}
// Called when HTTP Get request arrives
protected void doGet(HttpServletRequest req,
HttpServletResponse rsp)
throws javax.servlet.ServletException, java.io.IOException{
Sfsb s;
HttpSession hs;
String function = req.getParameter("function"); // get input
//
if (function.equals("Begin")) {
hs = req.getSession(true);
if (!hs.isNew()){ // Sesion not new, so clean it up first
try{
sh.remove((javax.ejb.Handle) hs.getAttribute("sfsb"));
}
catch (Exception e){} //ignore exceptions
}
try{ // to create EJB
s = sh.create("clicks of the Middle button");
hs.setAttribute("sfsb", s.getHandle());
}
catch(Exception e){
doAnswer(req, rsp, "Error creating EJB" + e);
return;
}
}
else{ // HttpSession and EJB should exist
try{ // to locate EJB
hs = req.getSession(false);
javax.ejb.Handle h;
if (hs == null || (h = (javax.ejb.Handle)
hs.getAttribute("sfsb")) == null){
doAnswer(req, rsp, "Please click 'Begin' first");
return;
}
s = (Sfsb)h.getEJBObject();
}
catch(Exception e){
doAnswer(req, rsp, "Internal Error: " + e);
return;
}
}
// preparations complete - s is an instance of the sfsb
if (function.equals("Begin")) {
doAnswer(req, rsp, "Count started");
}
else if (function.equals("Middle")) {
s.increment();
doAnswer(req, rsp, "'Middle' pressed");
}
else if (function.equals("End" )) {
doAnswer(req, rsp, s.howMany());
try{
s.remove();
hs.removeAttribute("sfsb");
}
catch (javax.ejb.RemoveException e){} // ignore exceptions here
}
else {
doAnswer(req, rsp, "invalid function");
}
}
}
========================
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Author" content="Ian McCallion">
<title>Stateful Session Bean Example</title>
</head>
<form ACTION="/sfsbweb/servlet/go" METHOD="GET">
<p><b><font size=+1 color=#ff6633> <%= (String)request.getAttribute("message")
%> </font></b>
<p>Press Begin, then press Middle a few times, then press End:
<table WIDTH="50%" >
<tr><td ALIGN=CENTER COLSPAN="2"><input type="submit" VALUE="Begin"
name="function"></td>
<td ALIGN=CENTER COLSPAN="2"><input type="submit" VALUE="Middle"
name="function"></td>
<td ALIGN=CENTER COLSPAN="2"><input type="submit" VALUE="End"
name="function"></td></tr>
</table>
</form>
</body>
</html>
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".