Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 72
Hibernate Eclipse IDE 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?

 
Marshal
Posts: 67430
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Hibernate Eclipse IDE 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
Marshal
Posts: 67430
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Hibernate Eclipse IDE 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
Marshal
Posts: 67430
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67430
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
     
    The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
      Bookmark Topic Watch Topic
    • New Topic