Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Bean question

 
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 on that page I have the following:

Now this is the first time this Bean is requested. What I am wondering is, when I submit this page to my Servlet, is the Bean already in the request? I tried the following to access my Bean from my servlet but got a nullpointerexception, so I am assuming a coupld of things:

name, btw, has a value already.
1. The Bean is no longer in the request scope from submitting to the Servlet from the JSP using the Form tag and a submit button.
2. The bean was never instantiated
Can someone shed some light on this for me?
 
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
After a little bit of testing I have figured out that I believe the bean is not in the same request scope as when I put it there in the JSP. I changed the request values to session, and it worked. But I don't need that in the session, I only need it in the request.
So now my question is a question of request relationship between JSP's and Servlets.
If I do what I stated above and instantiate a Bean in the request of a JSP page, how do I get that Bean to the Servlet I am submitting my form to without using the Session?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I do what I stated above and instantiate a Bean in the request of a JSP page, how do I get that Bean to the Servlet I am submitting my form to without using the Session?

You can't. The bean on your JSP is associated with the request that is using the JSP to build a response to send back to the browser. Once the response is dispatched, the request goes out of scope and takes your bean with it.
When you submit your form from the page that was sent to the browser, that instantiates a new request that has nothing at all to do with the request that created the page.
The session is the means by which requests can be tied to one another in the stateless HTTP protocol.
So in order for your bean to survive across multiple requests, it must be handled by a context higher than request (e.g. session or application).
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
Ok, I understand. Thanks Bear. The only reason I need the Bean in the JSP page to begin with is if the Servlet finds errors in the parameters, I need to forward back to the JSP page and still retain the form values. So just instantiating a Bean in the Servlet with new should suffice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic