This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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?
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?
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
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.