aspose file tools*
The moose likes JSP and the fly likes scope attributes forwarding problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "scope attributes forwarding problem" Watch "scope attributes forwarding problem" New topic
Author

scope attributes forwarding problem

Mohammad Akon
Greenhorn

Joined: Jul 17, 2007
Posts: 19
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
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
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

Joined: Jul 17, 2007
Posts: 19
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

Joined: Jan 04, 2006
Posts: 109
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.
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
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 ]

Regards,
Durga Prasad
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: scope attributes forwarding problem