File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes setting any object as property in java bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "setting any object as property in java bean" Watch "setting any object as property in java bean" New topic
Author

setting any object as property in java bean

ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

hello ... I am trying to set an object(DOG) as parameter in my bean ... but i am getting NullPointerException.. here is the code
// BeanTest.JSP


//ProcessBean.JSP


//test package and inside tht two classes named Test and Test1




now at runtime i am getting NullPointerException.... i have read in book that for only Strings and primitives automatically casting occurs not for any others....but what if i would like to do that ???


OCPJP(83%)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Why is there Java code in your JSP?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

because there is no other way to put instance of the class Test2 in the request..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Please read this article for information on properly structuring web apps. Any data prep should happen in the page's servlet controller. Java code in JSPs has been discredited for over 9 years now.
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

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 ??...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

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?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

One correction: the JSTL <c:set> tag can be used to set bean property values, but it's certainly something that's a rare need in a proper JSP page.
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

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....

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

Why do you need a cast? If the bean setter accepts a certain type, and the object is of that type, what's the problem?

And again, why isn't this something being handled in the controller before the page is invoked?
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160




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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61648
    
  67

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.
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Ok.

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting any object as property in java bean