• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

scope attributes forwarding problem

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

My codes are as follows:

page1.jsp



Its printing the values request attributes

in my someservelet code I am trying to get the request attributes but its giving "null":



output comes....

First Name: null

. Apparently, the "request" attribute are not created at all.

Where am I making mistake? How to solve it?

Thanks in advance.

Regards,

Mohammad
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohammad,

Use request.getParameter() when you are planning to fetch values from a html/jsp page, and use request.getAttribute() when you want to fetch attribute values which are set using request.setAttribute() IN A SERVLET.

In your case, in page1.jsp are you trying to retrieve the firstName,lastName, etc from html/jsp?? If so you need to use request.getParameter() but not request.getAttribute();

Regards,
Kiran.
 
Mohammad Akon
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello kiran,

Thanks. In page1.jsp the attributes (firstName, lastName,....) these attributes are set by a servelet (e.g. previousServelet). SO I guess using "request.getAttribute();" to retrieve the values, in such case, is OK.

Then again, I want the attribute values should be alive or available to the forwarded servelet (someservelet).

Thanks again.

Mohammad
[ May 14, 2008: Message edited by: Mohammad Akon ]
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohammad,

It is better to store the values(firstname, lastname,etc.) in session instead of setting them again and again using request.setAttribute() in each page. You can use the following:

HttpSession session=request.getSession();
session.setAttribute("name","obj");

Once you feel that you don't need the values you have set in session, you can use session.removeAttribute() method to remove those values to increase the application's performance.

Regards,
Kiran.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohammad,

The concept here is the scope of the attribute. Your code shows that you are getting some values from your previous servlet and setting them again in to the same attribute (which is not necessary).

No matter how many times you set the attributes, they have to be available for someServlet. Only then you will be able to retrieve them in the someServlet.

In your case, the atttibutes are of request scope and they will be available until your request is alive.

All that made difference in your case is the following snippet of code


<c:redirect> is nothing but you are redirecting it to some other servlet. That means, you are making a new request to some other servlet. In this case you request will be dead and new request will be sent.

So, obviously the value should be null.

instead of redirect, why dont you use reqeustDispatcher to redirect the to your someServlet... That will solve your problem.

Also, you don't need to set the attributes again. Just dispatch the request, the container will take care of making your attributes available to someServlet.

Otherwise, as kiran said, you can set your attributes at the session level to make them available through out the user session.

Hope this helps.....
[ May 17, 2008: Message edited by: Prasad Tamirisa ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic