File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to access a bean that has bean created by <jsp:useBean> tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to access a bean that has bean created by <jsp:useBean> tag?" Watch "how to access a bean that has bean created by <jsp:useBean> tag?" New topic
Author

how to access a bean that has bean created by <jsp:useBean> tag?

Gopakumar Naryanan
Ranch Hand

Joined: Jan 15, 2011
Posts: 72

<jsp:useBean> tag creates bean instance if the specified bean is not found in the specified scope and set the values using <jsp:setProperty>.
How to retrieve the bean that has been created as i said above?

I have one form as follows:

<jsp:useBean id="usebean" class="com.example.UseBean"scope="request">
<jsp:setProperty name="usebean" property="*"/>
</jsp:useBean>
<form action="./getName">
Name<input type="text" name="name"/>
<input type="submit">
</form>

I'm trying to access the bean instance using request.getAttribute("usebean"); in my servlet.. it is displaying null.

Can anybody explain what's wrong with above code?


Thanks & Regards
Gopakumar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

Where have you placed the code to retrieve the bean? It's not in your code example at all.

P.S. There's shouldn't be any Java code in your JSP, you should be using the EL to access the bean, but one step at a a time.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gopakumar Naryanan
Ranch Hand

Joined: Jan 15, 2011
Posts: 72

In the above question, there is no bean instance in request scope.
So, the container will create one and populates the values..right!!
when the user enters the name and submits, the request will come to my servlet..

i'm trying access the container created bean as follows :

(User)request.getAttribute("userbean");

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

The bean only exists in the JSP. Once the HTML is sent to the browser for the user to be able to submit the form, the bean is long gone.
Gopakumar Naryanan
Ranch Hand

Joined: Jan 15, 2011
Posts: 72

Bear Bibeault wrote:The bean only exists in the JSP. Once the HTML is sent to the browser for the user to be able to submit the form, the bean is long gone.


Then how does the container assigns the properties of the bean with the values entered in the html componentss?
mean to say : <jsp:setProperty name="usebean" property= "*"/>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

gopakumar king wrote:Then how does the container assigns the properties of the bean with the values entered in the html componentss?
mean to say : <jsp:setProperty name="usebean" property= "*"/>

It doesn't.

You are confusing the request whose response sends the HTML form to the page, with the request that is generated when the form is submitted. They are different and distinct requests and any request-scoped beans created in the first are not available in the second.

You should be submitting the form to a servlet that gathers the data using Java code.

The property= "*" format for useBean is a hold-over from 10 years ago when it was thought to be ok to submit a form to a JSP. That is no longer considered an acceptable practice.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61205
    
  66

I sounds as if reading these two articles:
  • The Secret Life of JSPs
  • The Front Man

  • might be helpful to you.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to access a bean that has bean created by <jsp:useBean> tag?