Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS 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

Issue with setting an attribute in a JSP

 
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.
 
Marshal
Posts: 67446
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67446
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic