• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bean not getting populated?

 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64833
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic