GWT using a Stateful EJB in the HTTP Session via a Servlet

I’m happy to have finally got my stateful session bean stored in the client session and accessed by a GWT client. (Yes, I’ve dropped facelets in favor of GWT).

The stateful session bean code is pretty simple, here is the class declaration with the annotation.

public class MyAppSessionImpl implements MyAppSession
{ ... }

The MyAppSession interface includes the doLogin() and isLoggedIn() methods, which are implemented by MyAppSessionImpl. It has the @Remote attribute.

I expose these methods using a GWT-style servlet to handle the RPC calls (see code below, it’s a full listing). The servlet has to provide an interface that uses only simple serializable types that are known to the GWT client code; obviously, this won’t include my database entities because they use annotations and are in a different Jar. Thus, all the database objects have to repackaged into a more GWT-digestable form for the client.

The MyAppSession interface will provide a user’s view of the database. It is backed by a the Dao and shares objects with it. The servlet has no transactions surrounding its methods, so any database entity returned by the session interface has to be considered read-only, or modified only through calls to the session interface. Database updates will probably be done by passing object IDs to the session interface methods along with new values/states to store for that object, which will make the session interface generally usable across different types of RPC.

This session is stored in the Servlet’s HTTP Session for the client; a new one is created if there isn’t currently one, or if the old one expired or was deleted.

Here’s the sample code, I hope you all find it helpful:

import javax.ejb.EJB;
import javax.ejb.NoSuchEJBException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.http.HttpSession;
import com.mycompany.myApp.model.MyAppSession;
@EJB(name="ejb/myAppSession", beanInterface=MyAppSession.class)
public class MyAppServiceImpl extends RemoteServiceServlet implements
MyAppService {
private static final long serialVersionUID = 2561932002220614615L;
public MyAppSession getMyApp() {
final HttpSession httpSession = getThreadLocalRequest().getSession();
MyAppSession sess = (MyAppSession) httpSession.getAttribute("myAppSession");
if(sess != null) try {
// Test if the session timed out or was destroyed, and re-create it if so
} catch(NoSuchEJBException ex) {
sess = null;
if(sess == null) {
Object objRef;
try {
InitialContext ic = new InitialContext();
objRef = ic.lookup("java:comp/env/ejb/myAppSession");
} catch (NamingException e) {
throw new Error(e);
sess = (MyAppSession)PortableRemoteObject.narrow(objRef, MyAppSession.class);
httpSession.setAttribute("myAppSession", sess);
return sess;
public boolean doLogin(String username, String password) {
System.out.println("Login for " + username);
return getMyApp().doLogin(username, password);
public boolean isLoggedIn() {
return getMyApp().isLoggedIn();


15 Responses to GWT using a Stateful EJB in the HTTP Session via a Servlet

  1. Targe says:

    Por favor me podrias enviar el codigo de la clase MyAppSession
    muy agradecida

  2. dobes says:

    Halo Targe,

    The code above is only an example I made based on my actual application – unfortunately, I can’t give you the source to my application, so you’ll have to figure out how to use this as-is.

    Also, I’ve since changed my application so that it doesn’t use a stateful bean any more; instead, I pass everything that’s needed for a method as a parameter. This made for a bit more code, but is more reliable and scalable.

  3. Dush says:

    If u can make a sample app which will call a session bean method from remote servlet that will help us gratly.
    pls do that.

  4. dobes says:

    Hi! This code is included as a class (and somewhat updated, too) in my new open source project “Kiyaa!”. See it here:

  5. ilango says:

    Does Kiyaa still use Facelets?
    You said” Also, I’ve since changed my application so that it doesn’t use a stateful bean any more; instead, I pass everything that’s needed for a method as a parameter. This made for a bit more code, but is more reliable and scalable.”
    Does this mean that in Kiyaa you actually pass everything that’s needed as a parameer to (a GWT servlet?)


  6. Hi ilango:

    1. Kiyaa doesn’t use facelets, it has a template system similar to facelets

    2. Kiyaa doesn’t require you to use a Stateful or Stateless bean, but my own application now uses only Stateless beans and I pass in the necessary session information with each RPC call going to the stateless session bean.

  7. ilango says:

    Thanks for your generous reply.
    I assume that Kiya is using GWT 1.5. I will check out the website though

  8. ilango says:

    I forgot to ask you! Is the GWT-SessionBean sample code available on
    I went this page and found jar file for download there. Is this the entire app with the sample included in it?
    Please guide me on this.And thanks againn

  9. Yes, it’s using GWT 1.5 and Java 1.5 Generics. Kind of annoying how the GWT 1.5 compiler is so slow, but generics make coding easier, so … it’s worth it overall. I hope the GWT compiler gets fast soon!

  10. dobes says:

    It does not include any sample applications, no. I only released the code that could be used in others’ GWT applications, but kept my application-specific code to myself.

  11. ilango says:

    Am I right in understanding that the code that is released does indeed come with GWT-session bean/Hibernate, that I can extend. At least your application could be the starting point for integrating my GWT front end with the backend.
    What is the easiest way to run the jar file. Which container should I use?
    Are there installation instructions.

  12. dobes says:


    There are some instructions, you’ll have to read the documentation on the Kiyaa! site and the javadocs. The EJB wrapper servlet is this one:

    You’ll probably have to look at the source code to get a better idea of how it works and how to use it.

  13. ilango says:

    Thanks very much. Not sure if I can make it work, but I will try.At least I will learn some GWT in the process.

  14. ilango says:

    Lol. I am actually learning the stuff. I think I will make it work and post my results so that others can benefit as well.

  15. Bookmarks speichern…

    […]GWT using a Stateful EJB in the HTTP Session via a Servlet « dobesland[…]…

%d bloggers like this: