• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Evaluating Strings

 
Felipe Rocha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,
Is there eval() on JSP or Servlet as there is in PHP and JScript? How can i evaluate a string using JAVA ? The problem is like this:
I use a string ( that defines which function must be called) as parameter, i get it using getRequestParameter() and after that i want to call the function which has the same name as the string got , how can i do that ?
Thanks
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The methods are available: equals(), startsWith(), endsWith(), etc. Check the API.

Bosun
 
James Swan
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to do this is to use the reflection APIs (java.lang.reflect), these let you interrogate a class to get info about its members.
eg.

I have just started looking at the reflection APIs myself, so there might be better ways of doing this.
James.
 
sundari swamy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I feel the code given by James is a good start to work on with your requirements.
Few points I noted w.r.t James code , you also have to Prefix the Package name. And I guess the typecasting is also not required .(you have to hardcode the className for typecasting in this case. This wouldn't possibly help you to execute dynamically)
 
Kris Nelson
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many possible values can this parameter have ( i.e. how many functions are there )? If there's not too many, why couldn't you use an if statement with the necessary else if blocks?

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com
 
Felipe Rocha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot everybody . I used the reflect API and it worked great.
Thanks again
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic