wood burning stoves 2.0*
The moose likes Servlets and the fly likes Bean question 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 » Servlets
Bookmark "Bean question" Watch "Bean question" New topic
Author

Bean question

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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?


GenRocket - A Test Data Generation Platform
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15286
    
    6

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

Joined: Jan 10, 2002
Posts: 60041
    
  65

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


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

Joined: Jul 11, 2001
Posts: 15286
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bean question
 
Similar Threads
Bean Problem
Doubt with <jsp:useBean> having only type no class attribute
session scopt problem
JSP:Include question
how to use scope in target version of c:set