Discussions

General J2EE: Can I pass HttpServletRequest to SessionBean?

  1. Can I pass HttpServletRequest to SessionBean? (5 messages)

    Hi,
    I try to pass javax.servlet.http.HttpServletRequest to my SessionBean. But I received error message :
    "java.lang.ClassCastException"

    Is that means I cannot pass HttpServletRequest as a parameter to SessionBean?

    Actually the reason I pass HttpServletRequest to SessionBean is because the SessionBean will act as my LoginController.
    It will store user information into HttpSession (therefore I need the HttpRequest as my parameter).
    Or is there any better way to store user info into HttpSession?

    Thank You
    Regards
    Ray
  2. teng,

    u can only pass serialized objects as parameters to EJB's and HttpServletRequest isnt one.

    also ur bean shud not be aware of the layer sitting on top of it, in ur case servlets. by trying to pass the request object to ur bean, u r doin that. u need to look at ur design.

    kapil
  3. I agree,u should probably let ur bean just do the login part and create a domain object contain any login information and return to ur servlet and in the servlet u can add the object into your httpsession.U can pass the session id to the EJB in case u want it stored in the domain object.This way ur EJB becomes resusable with any frontend and the design is layered and u can add any more data u want stored in the session just by changing the domain object and dont have to deal with httpSession.setAttribute all the time.
  4. Or alternatively, create an interface which represents what it is you need to pass, and then an implementation of that interface which holds the HTTP specific stuff.

    That allows your EJB to remain ignorant of the specifics.

    However, it does not solve the problem of being unable to serialize the object.

    Most implementations of the interface you mention are serializable, plus, the error you quote is not one I would associate with serialization. Are you sure you are not casting the wrong object, or something simple like that?

    You normally see very clear specific Marshall or UnMarshall errors when you can't serialize something.

    Chz

    Tony
  5. I think it is usually recommended not to blur the different layers of a J2EE type application. I would not advise passing in an HttpServletRequest into a session bean (you can't). I doubt that this is a practice that is used much, for the very reason that is not serializable. This information should be handled at the controller, and then passed in as login information to the session bean. You could store the login information in a java bean that is serializable, and then pass that object to the session bean. Just one more thing to think about. Where is it best to track state information for a web application? There are several different thoughts on the subject, but the best solution that I have come up with is to only track state information at the presentation layer using the MVC 2 design pattern (Struts is a good example of this). It is very tempting to try and store state information at the presentation layer and the business logic layer.
  6. The problem is that HttpServletRequest & HttpServletResponse are interfaces. To use you must wrap interface as class. Use the following class constructor: HttpServletRequestWrapper(HttpServletRequest request) so could write as: HttpServletRequestWrapper reqClass = new HttpServletRequestWrapper(request); However, there is one limitation. You can only pass as paramter to bean on same server/jvm since is passed as reference rather than value. I couldn't get it to work with remote bean. Best of luck, Mark Bobsin bobsinm@hotmail.com