Forums Register Login

setAttribute & getAttibute doubt

+Pie Number of slices to send: Send
hi, i have difficulty to understand of concept SetAttribute and getAttribute , can anyone please show me some sample code how is it work ? thank you very much for your time !!
+Pie Number of slices to send: Send
class MyObject {
String name;
int count;
MyObject(String n, int c) { name = n; count = c; }
}

in your servlet:
MyObject myObj = new MyObject(one, 1);
request.setAttribute("MyObj", myObj); //now myObj is available in request
//get the request dispatcher and forward or include to your jsp


in your jsp, something like this:

//may need to import MyObj class definition...

<% MyObj myObj = (MyObj)request.getAttribute("MyObj"); %>

//Now you have access to the myObj object!!
+Pie Number of slices to send: Send
so john, setAttribute is actually set the object to be available , getAttribute is to get object ...



from the above code, is that means we now call the object into page, so that we can now get the MyObj object members, like name and count ?

another question, why normally it use in request not response ?

thank you !!
+Pie Number of slices to send: Send
because we dont have these methods.

response.setAttribute(String str, String str)
response.getAttribute(String str)

read the docs.
Those cherries would go best on cherry cheesecake. Don't put those cherries on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1587 times.
Similar Threads
DOM-problems
HtmlUnit problem: Change the Request
cannot resolve symbol
ApplicationContext
popup form - howto get data ? from newbie
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:20:42.