• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Issue with setting an attribute in a JSP

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hidden parameters solved the problem.
Thanks Bibeault.
 
Forget Steve. Look at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic