Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Bean question

 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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?
 
Marshal
Posts: 67451
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • 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.
 
You’ll find me in my office. I’ll probably be drinking. And reading this tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic