Search code examples
jspservletsjsp-tags

JSP, useBean action


By means of the useBean action I create a bean instance in one JSP page and woudl like to access this instance in another JSP page but the instance seems not to exist there, however I set the scope of the bean to "session". Could you advise why?

The code in the JSP where the bean is created:

<jsp:useBean id="littleBean" class="beans.MyBean" scope="session">
  <jsp:setProperty name="littleBean" property="name" value="The value set in the independent JSP page"/>
</jsp:useBean>

The code to access the bean:

<jsp:useBean id="littleBean" class="beans.MyBean" scope="session">
    <jsp:getProperty name="littleBean" property="name" />
</jsp:useBean>

The filed String name exists in the bean with the appropriate public setter and getter methods. Thanks in advance. Tamas


Solution

  • Sorry, I have just seen it after sending the post. The problem is that I wrapped the getProperty in the useBean action and in this case it creates a new bean instance. The correct code that works for the access is without the <jsp:useBean ...> action:

    <jsp:getProperty name="littleBean" property="name" />
    

    I have already tested it. Maybe it will also be useful to someone else.