This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Gopakumar Naryanan
Ranch Hand
Posts: 72
Eclipse IDE Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gopakumar Naryanan
Ranch Hand
Posts: 72
Eclipse IDE Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Eclipse IDE Hibernate Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I sounds as if reading these two articles:
  • The Secret Life of JSPs
  • The Front Man

  • might be helpful to you.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic