• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object reference not working with Bean method

 
Joseph Smithern
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP action page that works where it calls a Bean (boolean) Validate method, and also all the getter and setter methods work great. Now I want to create a Servlet to be the action page but I cant seem to get the Validate method to work so I assume I am doing something wrong with my object instantiation.

Here is my current JSP action page that works:

My attempt with the Servlet where it goes keeps ignoring the Validate method and goes to the retry.jsp page everytime so I assume I am not creating my reference correctly with the Bean class:
 
ramprasad madathil
Ranch Hand
Posts: 489
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My attempt with the Servlet where it goes keeps ignoring the Validate method and goes to the retry.jsp page everytime so I assume I am not creating my reference correctly with the Bean class:


Not quite. You are missing doing something in your servlet that you do in your jsp.



Since you have put that in your jsp, I assume you are familiar with what it does. Just to confirm, it picks up the request params from the request and calls the setter methods which have identical setXXX() names. And then you call validate.

You should do that in your Servlet too before invoking the validate() method. And for that, you either hardcode the method calls or use reflection to look up the method based on the param names (apache has a good library that accomplishes the same).

cheers,
ram.
 
Joseph Smithern
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,
How would I call the setter methods in the Servlet?

My attempt (just showing two of my fields) wont compile saying there is an issue with reference from a static context:

 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you please post the validate method as well as the compile error that you get?

Thanks
 
Joseph Smithern
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile error messages:


 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it doesn't like this line of code

boolean formHandler = new FormBean().validate();

you have to do it in 2 steps

FormBean bean = new FormBean();
boolean formHandler = bean().validate();
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joseph Smithern:
Compile error messages:



Yes, that's what happens when you try to call an instance method as if it were a static method. You do understand the difference, no?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should probably make your instance first, and then set its values :
 
Joseph Smithern
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks it now works.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!