Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HTTPrequest object. getAttribute()

 
John Davis
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have long been successful communicating information from servlets to JSPs. I usually setup a dataBean object and then request.setAttribute ("dataBean",beanObject); Then in the JSP, i simply getAttribute ("dataBean") and cast the returned object to the bean's type and gain access to the dataBean's contents.
Recently, I attempted the same operation but in reverse. I setAttribute ("parmName",parmValue); in a JSP scriptlet (Java is the scripting language), and then attempted a getAttribute ("parmName") in the servlet. The resulting object is always null. I am able to do a request.getAttribute ("parmName") in the JSP and have verified that the attribute is indeed being set in the request successfully by the JSP.
Why cannot I access the attribute from the request in the servlet?
 
Tony Alicea
Desperado
Sheriff
Posts: 3226
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that the request ends shortly before the JSP is displayed, which is the response. It you click on the JSP again, that's the next (and therefore a separate) request.
Since the object has only a single request scope, you don't get it in the next request to the servlet.
(HTTP)session scope is different, of course, but then you have to take care of eliminating the object(s) from the HttpSession before it gets bloated with unneeded objects.
Let's see what others say...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic