well i read the article and got understood that we should not mix scripts with JSP .......... but my question here is what if i use bean and that bean has property that is an object...obviously i can not take that object as input, i have to construct it.. so here i want to know that how can i use it with bean ??...
Sorry, I got lost there. Are you asking how to construct an object in Java? Or how to pass it to a setter method of a JavaBean? Or what?
And is this relevant to JSP? If you're using JSTL -- and you should be -- then it can use bean properties if the bean's class is designed with suitable getter methods. But JSTL can't call the setter methods because that isn't what a JSP is for. Was that part of your question?
my question is i want to set an object of some type as the property in my bean and also i want to get it from bean to show it.. [see code above. from that you will come to know what i am trying to achieve]
now here on the other page i am trying to set the property of the bean to that object type .... i know that implicit casting is done in case of String and primitives only while bean's setters and getters are called..but if there is any bean which has another object as a property then how can we use it because here we have to explicitly provide cast....
i think you are not getting what i want to do ... i am doing this to learn this i am not an expert. so i have not used any controller like thing....
here i just want to set and get the property "sample".....sample is an object of Test2 class.
If you are trying to learn, why are you not trying to learn the proper ways of doing in things rather than how things were done 10 years ago? It's the equivalent of trying to learn electronics by studying vacuum tubes.
Scriptlets and <jsp:setProperties> are dinosaurs. Much better to learn modern JSTL and EL.
The first thing to understand is that the only communication between client and server is via strings.
The client sends request parameters to the server.
The server sends back a text response. Can be HTML, XML, JSON, whatever.
In your use case there are two request/response transactions
Request#1: BeanTest.jsp - the server sends back an html page with a form on it.
Request#2: generated from submitting the form on BeanTest.jsp, and goes to ProcessBean.jsp
Do you see your problem yet?
You are setting a request attribute as part of BeanTest.jsp
You are trying to retrieve that request attribute as part of ProcessBean.jsp.
They are two seperate requests, and so the request attribute is not available.
If you want a java object to be retained across multiple requests, you should be using a Session attribute rather than Request.
Otherwise you have to work out a method to translate from Object --> String --> Object and pass it as a request parameter.
That is where a framework can come in handy, as it can do the heavy lifting involved there.
So in short:
To pass a java object in this manner you either need to
- use the session
- convert it to string, retrieve that string as a parameter, and convert back to java object.
subject: setting any object as property in java bean