aspose file tools*
The moose likes JSP and the fly likes Bean not getting populated? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Bean not getting populated?" Watch "Bean not getting populated?" New topic
Author

Bean not getting populated?

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

I have a JSP page and am populating a Bean with a value. Then I call a servlet and try and print out this value, and I am getting a NullPointerException.
Can someone look at this code and tell me what might be the problem.
JSP Code

SERVLET Code

BEAN Code

Thanks for any help. The NullPointerException occurs on the line where I print out bean.getTitle()) in the Servlet.


GenRocket - A Test Data Generation Platform
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

You are creating the bean and placing it on the request instance that is processing the JSP. The JSP produces its HTML output and sends it off to the browser. At this point your request instance goes out of scope taking your bean with it.
Then you click the link on your page and invoke the servlet in an entirely new request instance. No bean. Not only is it attached to another request instance, it's probably long gone and been garbage collected.
If you need to pass data to a servlet from an HTML page, you need to either submit a form or tack request parameters onto the URL.
If you want a bean to stay around longer than the scope of a request, you'll need to attach it to the session or application scopes which stay around a lot longer than the request instances.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

Thanks Bear.
If you need to pass data to a servlet from an HTML page, you need to either submit a form or tack request parameters onto the URL.
Not if you need the data persisted for any length of time thought, right?
At this point your request instance goes out of scope taking your bean with it.
Then what is the point in ever using the "request" scope of a bean then? In what instance would the request not be out of scope?
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65


Not if you need the data persisted for any length of time thought, right?

Generally correct, but your JSP can populate your page with hidden parameters in a form to propagate data along when apprpriate.

Then what is the point in ever using the "request" scope of a bean then? In what instance would the request not be out of scope?

The most common use to which I put request attributes (including beans) is when using the Model 2 pattern. The request hits a controller servlet (rather than a JSP) which performs the front-end processing for the request (DB queries and so on) and tacks any data that is needed in the JSP onto the request. Then the request is forwarded to the JSP which obtains the data and uses it to create the HTML display page.
Request attributes are also useful for passing data to sub-pages included via the jsp:include tag.
hth,
bear
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Bean not getting populated?
 
Similar Threads
Question 1030885226407
Configuring JNDI for JavaBean in Tomcat 5
jspSetProperty question
Using a JavaBean tags in a JSP page
jsp:useBean and jsp:getProperty