This week's giveaway is in the OCPJP forum.
We're giving away four copies of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) and have Paul Anilprem on-line!
See this thread for details.
Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Servlet --> Jsp with beans

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I'm setting the properties of a bean from some get request parameters in a servlet, and using a request dispatcher to forward the request to a jsp page.
I'm using the bean in the jsp page, but when I try and retreive the values from the bean, they are null.
I've set the bean as a session bean, is there something I'm missing.
Richard
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yo richy!!
i'll drop you an e-mail to catch up.
What I think is your problem is the scope of your javabean, make it scope="request" in your to and from jsp's.
Alternatively, keep it session, and implicily set the javabeans properties in the destination jsp page from the request(to do this your input field names have to match up with your javabean variables) -and you'll have the bonus of not having to set them in your servlet
later,
Greg
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like your JSP is building a new bean instead of using the one attached to the session. Are you sure you are using the same name in both JSP and servlet?
Bill
 
Richard Scothern
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,
Thanks for your replies. Greg, I didn't know you where still spreading your knowledge, good to hear from you.
I don't think i explained myself so here goes...
In my servlet, I am creating an instance of a bean, populating the variables from a get request and then I use:
HttpSession session = request.getSession(true);
session.putValue("transformer", transformerBean);
In the jsp page, I have the following:
<jsp:useBean id="transformer" class="TransformerBean" scope="application" />
Except now I get the error:
Attempted a bean operation on a null object
So I assume I'm missing something somewhere, probably in the servlet.
Richard
 
Sheriff
Posts: 67641
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are storing the bean in session scope, yet looking for it in application scope!
Change your useBean to specify session scope and you should hook up to the same instance of the bean.
hth,
bear
 
Richard Scothern
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried putting the bean in session scope but the problem persists. Could there be an error within the bean class?
Richard
 
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does your "null object" error message
come from the "useBean" tag?
[ March 10, 2002: Message edited by: Michael Yuan ]
 
Michael Yuan
author
Posts: 1436
6
Python TypeScript Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, you might want to try
session.setAttribute()
instead of the depreciated putValue()
I do not know what difference it makes
but it does not hurt to try.
 
pie. tiny ad:
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
reply
    Bookmark Topic Watch Topic
  • New Topic