Using JavaBeans in Servlets/JSP


Web tier: servlets, JSP, Web frameworks: Using JavaBeans in Servlets/JSP

  1. Using JavaBeans in Servlets/JSP (2 messages)

    I'm thinking of letting the servlet which verifies my user login to my site creating a bean which will later be used by my JSP documents to interact with my database.

    This leads me to a couple of questions:

    1. I'm thinking of letting the bean constructor create the database connection and setting some parameters that is user specific. How can I use the bean in JSP after I've created it in the servlet (I don't want my JSP to create a new bean).

    2. Is this a good approach, concerning J2EE patterns?

    3. When is it an option to use CMP EJB's instead of simple JavaBeans? My site do not need to handle a lot of simultaneus users (yet) and there is still a lot of optimization for me to do with the database connection etc.

  2. Using JavaBeans in Servlets/JSP[ Go to top ]

    hey there markus,

    you can store the bean in the ServletRequest, HttpSession, or ServletContext object which are implicitly defined as 'request', 'session', and 'application', respectively (i'm not 100% about ServletContext = application, but to knowledge, it's the same).

    Each of the aforementioned classes have a setAttribute and getAttribute method. In your servlet, you can instantiate a bean and store it in the object of your choice depending on the scope you wish the bean to exist. For example, suppose I want to store the bean for the lifecycle of a session.

    My servlet code would look something like:
    // get the session
    HttpSession session = request.getSession();

    // instantiate and set up bean
    SomeBean bean = new SomeBean();

    // store bean in the session object
    session.setAttribute("beanRef", bean);

    Now that I have a bean in my session, I can access it in my JSP via:
      SomeBean bean = (SomeBean)session.getAttribute("beanRef");

    First Name: <%= bean.getFirstName() %>
    Last Name: <%= bean.getLastName() %>

    Or you can use the JSP API's built in tags:
        scope="session" />

    First Name: <jsp:getProperty id="beanRef" property="firstName" />

    To answer your second question, it all depends on what you're making exactly. MVC is probably the most widely adopted pattern when it comes to JSP-Bean-Servlet interaction, but in the end, just make sure your source code is manageable and easy to update in case changes need to be made.

    I've never used CMP EJB's so I couldn't really offer much in that regard.

    Hope this helps. If you would like more examples, feel free to ask.

  3. Using JavaBeans in Servlets/JSP[ Go to top ]

    Thankyou! That was exactly what I was looking for. I haven't tried it in my application yet but I will later today.

    Regarding J2EE patterns, I think that it might not be neccesary to go with the "right" chocie directly if you have a small application since it's mostly cut 'n' paste to change your architecture. I've gone from servlets to JavaBeans containing my business logic and that was piece of cake to get it up and running again with the new architectue. Well, I'm not writing the big stuff. Yet. :-)

    Thanks again!