Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

request.setAttribute  RSS feed

 
Eric Sexton
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have these jsps that must be in the scope of request only. I want to keep this one attribute carrying through though. Here's how I'm trying to do it:

The servlet that handles this gets a "null" on this attribute when it checks it out. What's the correct approach to this? Should this JSP work and I'm missing carrying the request through or something? How can I make it work?
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you call this twice on the same page or was it a mistake? Though I am not sure it is the cause of the problem
<% request.getAttribute("recordStart"); %>
<% String recStart = (String)request.getAttribute("recordStart"); %>
If you did; try using only
<% String recStart = (String)request.getAttribute("recordStart"); %>
and let us know if it works
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that your String recStart is actually pulling a non-null value to being with? What else are you doing in the JSP?
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<% request.getAttribute("recordStart"); %>
I didn't even notice that. What good does that do?
 
Eric Sexton
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry that first line of code is redundant and was a pasting accident.
Well... this is kind of intertwined with struts. I'm trying to submit this value back to my action class. I've tried submitting with <html:submit> and <html:link> but when my action class gets the attribute it says null. Here's how my servlet is getting it back:

I do an out on that string and IT says null.
p.s. I slept a whole 3 hours last night and I am seriously off my game today with a deadline looming. OUCH.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are still giving us just one line of code for a problem. And the line of code you are providing is appearntly not the problem as it is pulling a null value out of the request. So the problem must lie elsewhere.
I would suggest providing us with your JSP and the Action class you are submitting to. That way we can see what you are trying to do.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are placing your attribute on the request in your JSP, and then submitting the JSP to a servlet, your attribute is long gone. The request that created the JSP, and the request that gets generated when the JSP is submitted are completely different requests and any attributes placed on the first one will not be available on the second.
 
Eric Sexton
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear. That was what I was looking for. I was afraid that was the case. So setting an attribute on the request in a JSP is basically useless for me then.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
If you are placing your attribute on the request in your JSP, and then submitting the JSP to a servlet, your attribute is long gone. The request that created the JSP, and the request that gets generated when the JSP is submitted are completely different requests and any attributes placed on the first one will not be available on the second.

Aha. I didn't think about that. That's why Bear is the KING!
So would using jsp:setProperty work then? That should make the data available to the HttpServletRequest in the Action right?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66208
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
request.setAttribute() is most useful in the scenario where a request is being forwarded around to various resources in order to compose a single response. This is used most typically in the Model 2 pattern where a request is submitted to a servlet (which performs the core processing) and then forwarded to a JSP page (which handles display formatting).
In order for attributes to stay in scope across requests, they can either be placed on the session (per user) or servlet context (per web app).
Gregg, jsp:setProperty will set a property onto a bean. Therefore the scope and lifetime of the property will be controlled by the scope and lifetime of the bean. If the bean is a request attribute, it too will not survive a request boundary. (P.S. Thanks for the compliment).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!