Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Issue with setting an attribute in a JSP  RSS feed

 
Augustin Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hey All,

I am trying to set an attribute in request scope in my first JSP(index.jsp)



But in my Servlet if I say



I get the list object as null.

I read some where that request.setAttribute() must be used while using RequestDispatcher only. How far is that true?

Can't I succeed in doing what I did, like setting a attribute in JSP and retreiving it in the Servlet.

Please help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Backwards. You generally set the "attribute" (properly called a scoped variable) into the request in the servlet controller for passing to the JSP.

You didn't mention the relationship between the servlet and JSP in question, but I'm guessing that the JSP generates an HTML page that then submits a request to the servlet. Correct?

If so, then of course that's not going to work. You are setting the scoped variable in one request, and trying to retrieve it from a different request.

What are you really trying to accomplish? Your question is raising a huge red flag emblazoned with "design flaw".
 
Augustin Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bibeault for your prompt reply.

The scenario is like this

I have this in my JSP

..using GET here.
But I need to send other data say like comments which is > 1024 characters.
So I thought sending the data by adding it to the request object.


But it turns out that sendRedirect() sends a new request every time wiping out the previous requests values.
So I loose out the comments I had stored in the request object.

The solution for this could be to use RequestDispatcher.forward().

If I need to do as I want I need to store the comments in session object.
But its expensive and not adivsable right, storing huge data in session object.

Any other suggestions!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Augustin Smith wrote:I have this in my JSP ...

This is 2009. Why is there Java code in your JSP? Scriptlets have been discredited since 2002. One of the biggest problem-makers in web apps, is inappropriate placement of code,

If I need to do as I want I need to store the comments in session object.
But its expensive and not adivsable right, storing huge data in session object.

Wrong. It's not inadvisable to use the session; it's inadvisable to use it unwisely. Placing data in the session and then removing it when no longer needed is fine.

Why aren't you just passing it as a hidden parameter?
 
Augustin Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hidden parameters solved the problem.
Thanks Bibeault.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!